Strictly Programming

Using Maven 2 to create a project

Posted in Java, Maven by sqllyw on 03/28/2008

A short and quick guide to setup a maven project

1. First create a project, cd to a directory, then type:

mvn archetype:create -DgroupId=my-test -DartifactId=my-test

you will see a directory my-test created, cd to that directory.
2. Compile the sample program
A sample program is created under my-test/src/main/java/org/abc/mytest, file name is, to compile, just type:

mvn compile

and you will see “BUILD SUCCESSFUL”, that means the program is ready to run
3. running the program
There are several ways to run the program, first the basic one:cd target/classes first, then type:

Hello World!

Another way is, use maven to run the program, first let’s go back to the root directory of the project, that is my-test in this case, you should see pom.xml in the directory. then type:

mvn exec:java
[INFO] Building my-test[INFO]    task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[INFO] [exec:java]Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

You might find it tedious to type the long main class name, to make it the default main class, you can modify the pom.xml, here is the pom.xml file so far:

<project xmlns=&quot;; xmlns:xsi=&quot;;

Add following to the pom.xml, just right after the </dependencies>:


now you can run the program by:

mvn exec:java

3. Create an executable jar
You might want to create a single jar that contains all the dependencies of the project so it can run anywhere, there are several solutions that works very well, maven-assembly-plugin is one, put following into your pom.xml:

to be added

then mvn assembly:assembly, you should see a jar file that you can run.
Another one is onejar-maven-plugin, put following into pom.xml:

to be added

then type:
mvn package

Some maven commands:
to skip test: Add the parameter -Dmaven.test.skip=true in the command line.

Tagged with: ,

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: