Jump to content

How to Create a New Maven Project from the Command Line

+ 3
  tmo9d's Photo
Posted Sep 23 2009 12:44 PM

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

$ mvn archetype:generate -DgroupId=org.sonatype.mavenbook.ch03 \
                                         -DartifactId=simple \
                                         -DpackageName=org.sonatype.mavenbook \
[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 \
Choose archetype:
12: internal -> maven-archetype-mojo (A Maven Java plugin development project)
13: internal -> maven-archetype-portlet (A simple portlet application)
14: internal -> maven-archetype-profiles ()
15: internal -> maven-archetype-quickstart ()
16: internal -> maven-archetype-site-simple (A simple site generation project)
17: internal -> maven-archetype-site (A more complex site project)
18: internal -> maven-archetype-webapp (A simple Java web application)
19: internal -> jini-service-archetype (Archetype for Jini service project creation)
Choose a number:  (...) 15: : 15
Confirm properties configuration:
groupId: org.sonatype.mavenbook.ch03
artifactId: simple
version: 1.0-SNAPSHOT
package: org.sonatype.mavenbook.ch03
 Y: : Y
[INFO] Parameter: groupId, Value: org.sonatype.mavenbook.ch03
[INFO] Parameter: packageName, Value: org.sonatype.mavenbook.ch03
[INFO] Parameter: package, Value: org.sonatype.mavenbook.ch03
[INFO] Parameter: artifactId, Value: simple
[INFO] Parameter: basedir, Value: /private/tmp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT

mvn is the Maven 2 command. archetype:generate is called a Maven goal. If you are familiar with Apache Ant, a Maven goal is analogous to an Ant target; both describe a unit of work to be completed in a build. The -Dname=value pairs are arguments that are passed to the goal and take the form of -D properties, similar to the system property options you might pass to the Java Virtual Machine via the command line. The purpose of the archetype:generate goal is to quickly create a project from an archetype. In this context, an archetype is defined as “an original model or type after which other similar things are patterned; a prototype.”[2]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/main/ *3
      /src/test/ *4

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:

  • The Maven Archetype plugin creates a directory that matches the artifactId. Simple. This is known as the project’s base directory.
  • 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.
  • 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.
  • 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!" );

Maven: The Definitive Guide

Learn more about this topic from Maven: The Definitive Guide.

Written by Maven creator Jason Van Zyl and his team at Sonatype, Maven: The Definitive Guide clearly explains how this popular tool can bring order to your software development projects. The first part of the book demonstrates Maven's capabilities through the development of several sample applications from ideation to deployment, and the second part offers a complete reference guide. Concise and to the point, this is the only guide you need to manage your project.

See what you'll learn

0 Subscribe

0 Replies