3 ways to run Java main from Maven
1、说明
本文主要是参考原英文博客中的内容同时结合自己的实例,将在Maven中运行Java main函数的三种方法进行介绍。如有雷同,纯属巧合。
在前面译文博客中(博客链接:http://blog.csdn.net/wolfofsiberian/article/details/39295955),介绍了如何用Maven实现Hello World程序,
但是并没有提到怎么运行此项目。下面就开始介绍如何运行main程序:
2、直接在命令行上运行-Running from Command line
在你还没有在maven phase中运行你的代码的时候,首先你要做的是需要编译代码。需要记住:exec:java 是不会自动编译代码的,
使用前都要自己去先编译。
附加pom.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<!-- Following is add for test "Declare Dependencies" -->
<!-- tag::joda[] -->
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<!-- end::joda[] -->
<!-- mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.springframework -DartifactId=gs-maven-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
mvn compile
(下面是本例的编译结果)
C:\apache-tomcat-8.0.9\webapps\gs-maven>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-maven 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\apache-tomcat-8.0.9\webapps\gs-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\apache-tomcat-8.0.9\webapps\gs-maven\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.390 s
[INFO] Finished at: 2014-09-16T19:53:32+08:00
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
一旦完成代码的编译,你就可以按照下面的命令来运行你的类了:
不带参数的运行方法:- Without arguments
(这里mainClass需要写到包含main函数的类。例子当中,hello是类的包名,HelloWorld类是main函数所在的类。)
mvn exec:java -Dexec.mainClass="hello.HelloWorld"
运行结果如下:
C:\apache-tomcat-8.0.9\webapps\gs-maven>mvn exec:java -Dexec.mainClass="hello.HelloWorld"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-maven 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ gs-maven ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
The current local time is: 19:57:12.140
Hello world!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.828 s
[INFO] Finished at: 2014-09-16T19:57:12+08:00
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
带参数的运行方法:- With arguments
mvn exec:java -Dexec.mainClass="hello.HelloWorld" -Dexec.args="arg0 arg1 arg2"
使用在CLASSPATH中的运行时依赖关系:- With runtime dependencies in the CLASSPATH
mvn exec:java -Dexec.mainClass="hello.HelloWorld" -Dexec.classpathScope=runtime
3、在pom.xml中的一个phase中运行- Running in a phase in pom.xml
你也可以在一个maven phase中运行main方法。例如,你可以作为test phase的一部分来运行HelloWorld.main()方法。
需要关注的部分内容:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>hello.HelloWorld</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
修改后的完整pom.xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<!-- Following is add for test "Declare Dependencies" -->
<!-- tag::joda[] -->
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<!-- end::joda[] -->
<!-- mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.springframework -DartifactId=gs-maven-->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>hello.HelloWorld</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
按照上面的配置来运行exec插件,简单的按照相应的phase运行:(可以先执行mvn clean清理一下)执行mvn test命令。
mvn test
C:\apache-tomcat-8.0.9\webapps\gs-maven>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-maven 0.1.0
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.1.1/exec-maven-plugin-1.1.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.1.1/exec-maven-plugin-1.1.1.pom (5 KB at 0.8 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/mojo/mojo-parent/20/mojo-parent-20.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/mojo/mojo-parent/20/mojo-parent-20.pom (19 KB at 17.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.1.1/exec-maven-plugin-1.1.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.1.1/exec-maven-plugin-1.1.1.jar (34 KB at 11.4 KB/sec
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\apache-tomcat-8.0.9\webapps\gs-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\apache-tomcat-8.0.9\webapps\gs-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ gs-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\apache-tomcat-8.0.9\webapps\gs-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ gs-maven ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ gs-maven ---
[INFO] No tests to run.
[INFO]
[INFO] >>> exec-maven-plugin:1.1.1:java (default) > validate @ gs-maven >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.1.1:java (default) < validate @ gs-maven <<<
[INFO]
[INFO] --- exec-maven-plugin:1.1.1:java (default) @ gs-maven ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0/maven-plugin-api-2.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0/maven-plugin-api-2.0.pom (601 B at 1.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom (9 KB at 9.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.pom (6 KB at 6.6 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.12/plexus-1.0.12.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.12/plexus-1.0.12.pom (10 KB at 13.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.6/plexus-utils-1.5.6.jar (245 KB at 82.4 KB/sec)
The current local time is: 20:19:55.234
Hello world!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.032 s
[INFO] Finished at: 2014-09-16T20:19:55+08:00
[INFO] Final Memory: 11M/27M
[INFO] ------------------------------------------------------------------------
4、在pom.xml中的profile中运行 - Running in a profile in pom.xml
主要配置内容:
<profiles>
<profile>
<id>code-generator</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>hello.HelloWorld</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
修改后完整的pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<!-- Following is add for test "Declare Dependencies" -->
<!-- tag::joda[] -->
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<!-- end::joda[] -->
<!-- mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.springframework -DartifactId=gs-maven-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>code-generator</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>hello.HelloWorld</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
<argument>arg2</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
调用上面的profile,使用下面的命令来运行:(由于<profiles><profile><id>code-generator</id>...,因此下面-P code-generator)
mvn test -Pcode-generator
执行结果:
C:\apache-tomcat-8.0.9\webapps\gs-maven>mvn test -Pcode-generator
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-maven 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\apache-tomcat-8.0.9\webapps\gs-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\apache-tomcat-8.0.9\webapps\gs-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ gs-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\apache-tomcat-8.0.9\webapps\gs-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ gs-maven ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ gs-maven ---
[INFO] No tests to run.
[INFO]
[INFO] >>> exec-maven-plugin:1.1.1:java (default) > validate @ gs-maven >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.1.1:java (default) < validate @ gs-maven <<<
[INFO]
[INFO] --- exec-maven-plugin:1.1.1:java (default) @ gs-maven ---
The current local time is: 20:36:40.984
Hello world!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.937 s
[INFO] Finished at: 2014-09-16T20:36:41+08:00
[INFO] Final Memory: 10M/24M
[INFO] ------------------------------------------------------------------------
以下部分直接来自原始blog内容:
5、Advanced Options
You can get a list of all available parameters by typing:
- mvn exec:help -Ddetail=true -Dgoal=java
arguments (exec.arguments)
classpathScope (exec.classpathScope, Default: compile)
cleanupDaemonThreads (exec.cleanupDaemonThreads)
commandlineArgs (exec.args)
daemonThreadJoinTimeout (exec.daemonThreadJoinTimeout, Default: 15000)
executableDependency
includePluginDependencies (exec.includePluginDependencies, Default: false)
includeProjectDependencies (exec.includeProjectDependencies, Default: true)
mainClass (exec.mainClass)
sourceRoot (sourceRoot)
stopUnresponsiveDaemonThreads (exec.stopUnresponsiveDaemonThreads)
systemProperties
testSourceRoot (testSourceRoot)
FAQ and Errors
Why do I get this error when specifying arguments to my main method:
- [ERROR] BUILD ERROR
- [INFO] ------------------------------------------------------------------------
- [INFO] Failed to configure plugin parameters for: org.codehaus.mojo:exec-maven-plugin:1.1.1
- on the command line, specify: '-Dexec.arguments=VALUE'
- Cause: Cannot assign configuration entry 'arguments' to 'class [Ljava.lang.String;' from '${exec.arguments}',
- which is of type class java.lang.String
- [INFO] ------------------------------------------------------------------------
- [INFO] Trace
- org.apache.maven.lifecycle.LifecycleExecutionException: Error configuring: org.codehaus.mojo:exec-maven-plugin.
- Reason: Unable to parse the created DOM for plugin configuration
- at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:588)
- at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:513)
- at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:483)
- at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:331)
- at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:292)
- at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:142)
- at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:336)
- at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:129)
- at org.apache.maven.cli.MavenCli.main(MavenCli.java:301)
Solution
exec.arguments was used before version 1.1 of the exec plugin, it did not support conversion of command line String to String[] array.
- If possible upgrade to 1.1 or later and use exec.args instead of exec.arguments.
- If you can't upgrade the plugin version, you can still use command line arguments with a profile and use multiple <argument> tags associated in the pom.xml
References
Related posts:
- The plugin ‘org.codehaus.mojo:selenium-maven-plugin’ does not exist or no valid version could be found
- Tweet your builds with Maven Twitter Plugin
- How to automate project versioning and release with Maven
- JAXB code snippets for beginners
- How to display maven project version in your webapp
Original Link:http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/