转自:http://redstack.wordpress.com/2010/12/22/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> &lt;project.build.sourceencoding&gt;UTF&#45;8&lt;/project.build.sourceencoding&gt; </properties> <build> &lt;plugins&gt; &amp;lt;plugin&amp;gt; &amp;amp;lt;artifactid&amp;amp;gt;maven&amp;amp;#45;compiler&amp;amp;#45;plugin&amp;amp;lt;/artifactid&amp;amp;gt; &amp;amp;lt;version&amp;amp;gt;2.0.2&amp;amp;lt;/version&amp;amp;gt; &amp;amp;lt;configuration&amp;amp;gt; &amp;amp;lt;pre &amp;amp;gt;1.6&amp;amp;lt;/pre&amp;amp;gt; &amp;amp;amp;lt;target&amp;amp;amp;gt;1.6&amp;amp;amp;lt;/target&amp;amp;amp;gt; &amp;amp;lt;/configuration&amp;amp;gt; &amp;lt;/plugin&amp;gt; &amp;lt;plugin&amp;gt; &amp;amp;lt;groupid&amp;amp;gt;org.codehaus.mojo&amp;amp;lt;/groupid&amp;amp;gt; &amp;amp;lt;artifactid&amp;amp;gt;exec&amp;amp;#45;maven&amp;amp;#45;plugin&amp;amp;lt;/artifactid&amp;amp;gt; &amp;amp;lt;configuration&amp;amp;gt; &amp;amp;amp;lt;mainclass&amp;amp;amp;gt;com.redstack.App&amp;amp;amp;lt;/mainclass&amp;amp;amp;gt; &amp;amp;lt;/configuration&amp;amp;gt; &amp;lt;/plugin&amp;gt; &lt;/plugins&gt; </build> <dependencies> &lt;dependency&gt; &amp;lt;groupid&amp;gt;junit&amp;lt;/groupid&amp;gt; &amp;lt;artifactid&amp;gt;junit&amp;lt;/artifactid&amp;gt; &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt; &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt; &lt;/dependency&gt; </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] 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.