Maven Tutorial - Maven Create Project








In this chapter we will use Maven plugin to create a new project. After creating a Maven project we will add some Java source code in.

Maven uses archetype and maven-archetype-quickstart plugin plugins to create projects.

Go to in C:\mvn_test folder and execute the following mvn command.

C:\mvn_test>mvn archetype:generate -DgroupId=com.java2s.ide -DartifactId=xmlFileEditor -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Here is the output.


c:\mvn_test>mvn archetype:generate -DgroupId=com.java2s.ide -DartifactId=xmlFileEditor -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/2.2/archetype-catalog-2.2.pom
...
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.jar (5394 KB at 1501.4 KB/sec)
[INFO] Generating project in Batch mode
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: c:\mvn_test
[INFO] Parameter: package, Value: com.java2s.ide
[INFO] Parameter: groupId, Value: com.java2s.ide
[INFO] Parameter: artifactId, Value: xmlFileEditor
[INFO] Parameter: packageName, Value: com.java2s.ide
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: c:\mvn_test\xmlFileEditor
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.178 s
[INFO] Finished at: 2014-11-03T15:58:42-08:00
[INFO] Final Memory: 26M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test>

The folders generated by the Maven.

null

It also created two source file for us, one with the main method another is the unit test case.

package com.java2s.ide;

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

Maven also created test cases for the generated app.

package com.java2s.ide;

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 );
    }
}

The generated pom.xml file is listed as follows.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java2s.ide</groupId>
  <artifactId>xmlFileEditor</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>xmlFileEditor</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>




Note

Maven already added Junit as test framework. By default Maven adds a source file App.java and a test file AppTest.java in its default directory structure.