Create a Simple Maven Project
Take the following the steps to create a simple Maven project and run a few Maven goals.
- Create the project directory:
c:\daven
- Create a directory for java source code:
c:\daven\src\java
- Create directories for your package structure (smartsoft.daven.*):
c:\daven\src\java\smartsoft\daven
- Create a simple class called Box:
File name:
c:\daven\src\java\smartsoft\daven\Box.java
Content:
package smartsoft.daven;
public class Box {
private int length;
private int width;
public Box(int length, int width) {
this.length = length;
this.width = width;
}
public int getArea(){
return length * width;
}
}
- Create a minimal POM:
File name:
C:\daven\project.xml
Content:
<project>
<id>daven</id>
<currentVersion>1.0</currentVersion>
<build>
<sourceDirectory>
${basedir}/src/java
</sourceDirectory>
</build>
</project>
Now, run one of Maven's built-in goals, java:compile. Open a command prompt, switch into your project directory (C:\daven), and type:
maven java:compile
Your screen output should look like this:
C:\daven>
maven java:compile
java:prepare-filesystem:
java:compile:
[echo] Compiling to C:\daven/target/classes
BUILD SUCCESSFUL
Total time: 33 seconds
Note that the java:compile goal has, as a prerequisite, the java:prepare-filesystem goal. Maven's prerequisite is analogous to Ant's depends.
When you look at your file system, you'll see that Maven created an output directory structure for your class files and placed the compiled class in it. It also created some directories related to unit testing, which I will discuss shortly. Here is what your directory structure looks like now:
c:\daven
src
java
smartsoft
daven
Box.java
target
classes
smartsoft
daven
Box.class
test-classes
test-reports
Try a few more goals. At the command prompt, type:
maven jar
Your screen output should look like this:
C:\daven>
maven jar
java:prepare-filesystem:
java:compile:
[echo] Compiling to C:\daven/target/classes
java:jar-resources:
test:prepare-filesystem:
test:test-resources:
test:compile:
[echo] No test source files to compile
test:test:
[echo] No tests to run
BUILD SUCCESSFUL
Total time: 6 seconds
Note that the jar goal has a number of other goals as prerequisites. Namely:
java:prepare-filesystem
java:compile
java:jar-resources
test:prepare-filesystem
test:test-resources
test:compile
test:test
Also, note that Maven created a jar file in the target directory called daven-1.0.jar. It derived the file name from the projectId and currentVersion elements in your project.xml file. Now try the clean goal. At the command prompt, type:
maven clean
Your screen output should look like this:
C:\daven>
maven clean
clean:clean:
[delete] Deleting directory C:\daven\target
BUILD SUCCESSFUL
Total time: 7 seconds
If you examine your file system, you'll see that the target folder has been deleted.
Understanding Goals
You've now seen three of Maven's goals: java:compile, jar, and clean. Remember that although Maven goals are the equivalent of Ant Targets, you don't need to create the goals yourself. Maven provides most of the goals you'll need. As of this writing, Maven shipped about 312 goals.
Also keep in mind that Maven's goals are organized into plug-ins. In fact, the Maven Web site describes the tool as a "small core that works with a satellite of plug-ins." Most of Maven's functionality comes in the form of plug-ins. Plug-ins even provide the prefixes in the goal names. For example, java:compile refers to the compile goal of the java plug-in. And kodo:enhance refers to the enhance goal of the kodo plug-in. If you run a plug-in without a goal name, you'll invoke the default goal for that plug-in. For example, maven jar invokes the jar plug-in's default goal.