Configuring Maven to run your Java application

Recently I was working on a project using Maven, and I really wanted to be able to run the project easily without needing to worry about all the classpath entries.

Turns out it is relatively easy to set up Maven to run your project for you and to automatically handle providing the right classpath for your code and all the dependencies.  Here’s how:

I created a simple Maven JAR project using an archetype as shown below:

$mvn archetype:create 
  -DarchetypeGroupId=org.apache.maven.archetypes 
  -DgroupId=com.redstack 
  -DartifactId=myproject

Then I edited the pom.xml file in the myproject directory.  I added the entries shown in red.  These tell Maven to compile the project using Java 1.6, and the name of the main class for the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <groupId>com.redstack</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 <name>myproject</name>
  <url>http://maven.apache.org</url>
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
      <mainClass>com.redstack.App</mainClass>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Having done this, I can then build and then run the project by simply typing these two commands:

$ mvn package
$ mvn exec:java
...
Hello World!
...

There will be a bunch of Maven messages too, but in the middle there you can see the output from the project – “Hello World!” in this case.  This example is just running the App.java that was generated by Maven.  In your project, this class might start up a User Interface, or run any number of tasks.  It probably does a little more than printing “Hello World!”

[Updated Jan 7, 2011] This approach will run your application in the same process that Maven is running in, which may or may not be acceptable.  If you want to run it in a different process, you can modify the plugin configuration section to something more like this:

     <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>-Xms512m</argument>
            <argument>-Xmx512m</argument>
            <argument>-XX:NewRatio=3</argument>
            <argument>-XX:+PrintGCTimeStamps</argument>
            <argument>-XX:+PrintGCDetails</argument>
            <argument>-Xloggc:gc.log</argument>
            <argument>-classpath</argument>
            <classpath/>
            <argument>com.redstack.App</argument>
          </arguments>
        </configuration>
      </plugin>

Then use the goal:

mvn exec:exec

This will execute the JVM in a new process and allow you to pass in whatever arguments you like.  Notice the empty classpath tag.  This will insert the correct runtime classpath for you based on the dependencies in the pom.xml.

About Mark Nelson

Mark Nelson is a Consulting Solution Architect in the Fusion Middleware Architects Team (known as ”The A-Team”) in Oracle Development. Their mission is to supply deep technical expertise to support customers deploying Oracle Fusion Middleware, and to collect real world feedback to continuously improve the product set. Mark spends most of his time working on development lifecycle, SOA and BPM. Before joining Oracle Development in 2010, Mark worked in Sales Consulting at Oracle since 2006 and various roles at IBM since 1994, including several in Software Group and System/390 Group across Asia Pacific.
This entry was posted in group2, Uncategorized and tagged . Bookmark the permalink.

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 )

Connecting to %s