【Maven】在Maven中运行java main函数的方法(3 ways to run Java main from Maven)
2014-09-16 17:43
567 查看
3 ways to run Java main from Maven
在前面译文博客中(博客链接:http://blog.csdn.net/wolfofsiberian/article/details/39295955),介绍了如何用Maven实现Hello World程序,
但是并没有提到怎么运行此项目。下面就开始介绍如何运行main程序:
在你还没有在maven phase中运行你的代码的时候,首先你要做的是需要编译代码。需要记住:exec:java 是不会自动编译代码的,
使用前都要自己去先编译。
附加pom.xml文件内容:
执行编译命令:
(下面是本例的编译结果)
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函数所在的类。)
运行结果如下:
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
使用在CLASSPATH中的运行时依赖关系:- With runtime dependencies in the CLASSPATH
你也可以在一个maven phase中运行main方法。例如,你可以作为test phase的一部分来运行HelloWorld.main()方法。
需要关注的部分内容:
修改后的完整pom.xml文件为:
按照上面的配置来运行exec插件,简单的按照相应的phase运行:(可以先执行mvn clean清理一下)执行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] ------------------------------------------------------------------------
主要配置内容:
修改后完整的pom.xml文件:
调用上面的profile,使用下面的命令来运行:(由于<profiles><profile><id>code-generator</id>...,因此下面-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!
[WARNIN
121f3
G] 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内容:
view plaincopy
to clipboardprint?
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)
view plaincopy
to clipboardprint?
[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
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/
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>...,因此下面-Pcode-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!
[WARNIN
121f3
G] 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:view plaincopy
to clipboardprint?
mvn exec:help -Ddetail=true -Dgoal=java
arguments (exec.arguments)
The class arguments.
classpathScope (exec.classpathScope, Default: compile)
Defines the scope of the classpath passed to the plugin. Set to compile, test, runtime or system depending on your needs
cleanupDaemonThreads (exec.cleanupDaemonThreads)
Wether to interrupt/join and possibly stop the daemon threads upon quitting. If this is false, maven does nothing about the daemon threads. When maven has no more work to do, the VM will normally terminate any remaining daemon threads. In certain cases (in particular if maven is embedded), you might need to keep this enabled to make sure threads are properly cleaned up to ensure they don't interfere with subsequent activity. In that case, see daemonThreadJoinTimeout and stopUnresponsiveDaemonThreads for further tuning.
commandlineArgs (exec.args)
Arguments for the executed program
daemonThreadJoinTimeout (exec.daemonThreadJoinTimeout, Default: 15000)
This defines the number of milliseconds to wait for daemon threads to quit following their interruption. This is only taken into account if cleanupDaemonThreads is true. A value <=0 means to not timeout (i.e. wait indefinitely for threads to finish). Following a timeout, a warning will be logged. Note: properly coded threads should terminate upon interruption but some threads may prove problematic: as the VM does interrupt daemon threads, some code may not have been written to handle interruption properly. For example java.util.Timer is known to not handle interruptions in JDK <= 1.6. So it is not possible for us to infinitely wait by default otherwise maven could hang. A sensible default value has been chosen, but this default value may change in the future based on user feedback.
executableDependency
If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class. This will have the affect of only including plugin dependencies required by the identified ExecutableDependency. If includeProjectDependencies is set to true, all of the project dependencies will be included on the executable's classpath. Whether a particular project dependency is a dependency of the identified ExecutableDependency will be irrelevant to its inclusion in the classpath.
includePluginDependencies (exec.includePluginDependencies, Default: false)
Indicates if this plugin's dependencies should be used when executing the main class. This is useful when project dependencies are not appropriate. Using only the plugin dependencies can be particularly useful when the project is not a java project. For example a mvn project using the csharp plugins only expects to see dotnet libraries as dependencies.
includeProjectDependencies (exec.includeProjectDependencies, Default: true)
Indicates if the project dependencies should be used when executing the main class.
mainClass (exec.mainClass)
The main class to execute.
sourceRoot (sourceRoot)
This folder is added to the list of those folders containing source to be compiled. Use this if your plugin generates source code.
stopUnresponsiveDaemonThreads (exec.stopUnresponsiveDaemonThreads)
Wether to call Thread.stop() following a timing out of waiting for an interrupted thread to finish. This is only taken into account if cleanupDaemonThreads is true and the daemonThreadJoinTimeout threshold has been reached for an uncooperative thread. If this is false, or if Thread.stop() fails to get the thread to stop, then a warning is logged and Maven will continue on while the affected threads (and related objects in memory) linger on. Consider setting this to true if you are invoking problematic code that you can't fix. An example is Timer which doesn't respond to interruption. To have Timer fixed, vote for this bug.
systemProperties
A list of system properties to be passed. Note: as the execution is not forked, some system properties required by the JVM cannot be passed here. Use MAVEN_OPTS or the exec:exec instead. See the user guide for more information.
testSourceRoot (testSourceRoot)
This folder is added to the list of those folders containing source to be compiled for testing. Use this if your plugin generates test source code.
FAQ and Errors
Why do I get this error when specifying arguments to my main method:
view plaincopyto clipboardprint?
[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
Official Maven Exec Plugin DocumentationRelated 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/
相关文章推荐
- 3 ways to run Java main from Maven
- 3 ways to run Java main from Maven
- 3 ways to run Java main from Maven
- 3 ways to run Java main from Maven
- 在maven中运行java main函数的方法
- idea创建一个java的maven,运行main方法找不到路径
- 使用Maven运行Java main的方法(转)
- maven项目 无法运行main方法的问题。java.lang.ClassNotFoundException
- 使用maven运行Java Main的三种方法解析
- maven - 使用maven运行java的main方法
- maven使用exec插件运行Java main方法,以下是3种不同的操作方式。
- 《Maven 在项目中运行java中的main方法》---利用插件跑java程序
- 在maven工程里运行java main方法
- maven 项目,运行main方法时java找不到或无法加载主类
- How to run Java main class and pass application arguments in Maven?
- 用Maven运行main方法出现java.lang.ClassNotFoundException
- jar 打包java class文件后,运行jar 出现 Failed to load Main-Class manifest attribute from 错误
- maven编译生成的jar包运行出现 "Failed to load Main-Class manifest attribute from"