Maven by Example
3.2. Creating a Simple Project

3.2. Creating a Simple Project

To start a new Maven project, use the Maven Archetype plugin from the command line. Run the archetype:generate goal, select archetype #77, and then enter "Y" to confirm and generate the new project:

Warning

At the time of publication, the default "maven-archetype-quickstart" was item #77 in a list of 284 available archetypes. As more and more projects release Maven archetypes, this list will change and the number for the default archetype may change. Don’t be surprised if you need to select a different number. When you run archetype:generate as shown below, the default maven-archetype-quickstart will be selected by default.

$ mvn archetype:generate -DgroupId=org.sonatype.mavenbook.simple \
-DartifactId=simple \
-Dpackage=org.sonatype.mavenbook \
-Dversion=1.0-SNAPSHOT
[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] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart \
(org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
...
77: internal -> maven-archetype-quickstart ()
Choose a number:  (...) 77: : 77
Confirm properties configuration:
groupId: org.sonatype.mavenbook.simple
artifactId: simple
version: 1.0-SNAPSHOT
package: org.sonatype.mavenbook.simple
Y: : Y
...
[INFO] Parameter: groupId, Value: org.sonatype.mavenbook.simple
[INFO] Parameter: packageName, Value: org.sonatype.mavenbook.simple
[INFO] Parameter: package, Value: org.sonatype.mavenbook.simple
[INFO] Parameter: artifactId, Value: simple
[INFO] Parameter: basedir, Value: /private/tmp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] BUILD SUCCESSFUL

mvn is the Maven 2 command. archetype:generate is called a Maven goal. If you are archetype is defined as “an original model or type after which other similar things are patterned; a prototype.” A number of archetypes are available in Maven for anything from a simple Swing application to a complex web application, and the archetype:generate offers a list of approximately 40 archetypes to choose from. In this chapter, we are going to use the most basic archetype to create a simple skeleton starter project. The plugin is the prefix archetype, and the goal is generate.

Once we’ve generated a project, take a look at the directory structure Maven created under the simple directory:

simple/(1)
simple/pom.xml(2)
/src/
/src/main/(3)
/main/java
/src/test/(4)
/test/java

This generated directory adheres to the Maven Standard Directory Layout. We’ll get into more details later in this chapter, but for now, let’s just try to understand these few basic directories:

(1)

The Maven Archetype plugin creates a directory simple/ that matches the artifactId. This is known as the project’s base directory.

(2)

Every Maven project has what is known as a Project Object Model (POM) in a file named pom.xml. This file describes the project, configures plugins, and declares dependencies.

(3)

Our project’s source code and resources are placed under src/main. In the case of our simple Java project this will consist of a few Java classes and some properties file. In another project, this could be the document root of a web application or configuration files for an application server. In a Java project, Java classes are placed in src/main/java and classpath resources are placed in src/main/resources.

(4)

Our project’s test cases are located in src/test. Under this directory, Java classes such as JUnit or TestNG tests are placed in src/test/java, and classpath resources for tests are located in src/test/resources.

The Maven Archetype plugin generated a single class org.sonatype.mavenbook.App, which is a 13-line Java class with a static main function that prints out a message:

package org.sonatype.mavenbook;

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

The simplest Maven archetype generates the simplest possible program: a program which prints "Hello World!" to standard output.