How do I create a simple project using Maven?
Author: Deron Eriksson
Description: This tutorial describes how to create a simple project using maven.
Tutorial created using: Windows Vista || JDK 1.6.0_04


Page:    1 2 >

Creating a mavenSW project is extremely easy using the maven archetype plugin. Information about archetypes can be found at http://maven.apache.org/guides/introduction/introduction-to-archetypes.html. We can create a maven project using the "archetype:create" goal in the following manner.

mvn archetype:create -DgroupId=com.test -DartifactId=mytest

This command states to create a project called "mytest" specified by the artifactId. The project belongs to the "com.test" group, specified by the groupId. This group specifies essentially where the project is located hierarchically in a maven repository, and it allows for the grouping of similar projects into similar groups.

Now, I'll run the "archetype:create" goal at the command prompt.

'mvn archetype:create -DgroupId=com.test -DartifactId=mytest' at command prompt

This is the first time I've run the command, so maven will download the resources that it needs from the central repository to my local maven repository. When the goal finishes, we can see that the "mytest" project was successfully created.

'mytest' project successfully created

If we go to Windows Explorer, we can examine the project that we just created. I ran the goal in C:\dev\test, so the "mytest" project was created at C:\dev\test\mytest.

'mytest' project created at C:\dev\test

Notice that mytest contains a set of directories. The src/main/java directory is the default JavaSW source directory for maven projects. This is where you typically place your source code. This directory location is configurable, but in general it's best to stay with the maven defaults unless you have a very compelling reason to change them. Notice that the com.test package structure was created within src/main/java, and that an App.java file was created in src/main/java/com/test. This is a simple HelloWorld-style class.

Sample JUnit test created

The src/test/java directory is the default location for JUnit tests, which are integrated into the maven lifecycle. The com.test package structure was created within src/test/java, and a simple JUnit test class called AppTest.java was created in src/test/java/com/test.

(Continued on page 2)

Page:    1 2 >