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 -DpackageName=org.abc.mytest

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 App.java, 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:

java  org.abc.mytest.App
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 -Dexec.mainClass=org.abc.mytest.App
[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] BUILD SUCCESSFUL
[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;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my-test</groupId>
  <artifactId>my-test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-test</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

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

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>org.abc.mytest.App</mainClass>
  </configuration>
</plugin>

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.

Advertisements
Tagged with: ,

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: