Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven creates the project


May 26, 2021 Maven


Table of contents


Maven - Create a project

Maven uses archetype plug-ins to create projects. T o create a simple Java app, we'll use the maven-archetype-quickstart plug-in. In the following example, we'll create a maven-based java application project under the C:\MVN folder.

Let's open the command console, jump to the C:-MVN directory, and execute the following mvn command.

C:\MVN>mvn archetype:generate
-DgroupId=com.companyname.bank 
-DartifactId=consumerBanking 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false

Maven will begin processing and will create the completed java application engineering structure.

INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] -------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] -------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------
[INFO] Using following parameters for creating project 
 from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.companyname.bank
[INFO] Parameter: packageName, Value: com.companyname.bank
[INFO] Parameter: package, Value: com.companyname.bank
[INFO] Parameter: artifactId, Value: consumerBanking
[INFO] Parameter: basedir, Value: C:\MVN
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\consumerBanking
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 14 seconds
[INFO] Finished at: Tue Jul 10 15:38:58 IST 2012
[INFO] Final Memory: 21M/124M
[INFO] ------------------------------------------------------------------

Now jump to the C:/MVN directory. Y ou'll see a java application project called consumerBanking (as set in artifactId). Maven uses a standard set of directory structures, like this:

Apache Maven creates the project

Using the example above, we can learn the following key concepts:

The folder structure Describe
consumerBanking Contains the src folder and pom .xml
src/main/java contains The java code file is under the package structure (com/companyName/bank).
src/main/test contains The test code file is under the package structure (com/companyName/bank).
src/main/resources Contains picture / property file (in the example above, we need to create this structure manually).

Maven also created a simple Java source file and a Java test file. You can see the app and other files by opening the C:\MVN\consumerBanking\src\main\java\com/companyname.java folder.

package com.companyname.bank;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

You can see the AppTest folder by opening the C:\MVN\consumerBanking\src\test\java\com/companyname.java.

package com.companyname.bank;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase 
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Developers need to put their files in the structure mentioned in the table above, and Maven will take care of all the complex tasks associated with building.

In the next section, we'll discuss how to use maven to build and test engineering: Maven - Build and test engineering.