[英]Maven exec:java goal on a multi-module project
I'm trying to run exec-maven-plugin
's exec:java
goal on a simple two-module project where one module depends on the other.我正在尝试在一个简单的双模块项目上运行
exec-maven-plugin
的exec:java
目标,其中一个模块依赖于另一个。 So far I can't find a configuration that works.到目前为止,我找不到有效的配置。 Here's a boiled-down test case:
这是一个简化的测试用例:
+ exec-multi-module-test/
+ pom.xml
+ module1/
+ pom.xml
+ src/
+ main/
+ java/
+ HelloPrinter.java
+ module2/
+ pom.xml
+ src/
+ main/
+ java/
+ MyMain.java
Here's the parent pom:这是父pom:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkscrg.sandbox</groupId>
<artifactId>exec-multi-module-test</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
</plugin>
</plugins>
</build>
</project>
module1
's pom: module1
的 pom:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>exec-multi-module-test</artifactId>
<groupId>com.mkscrg.sandbox</groupId>
<version>1.0</version>
</parent>
<artifactId>module1</artifactId>
</project>
module2
's pom: 4.0.0 module2
的 pom: 4.0.0
<parent>
<artifactId>exec-multi-module-test</artifactId>
<groupId>com.mkscrg.sandbox</groupId>
<version>1.0</version>
</parent>
<artifactId>module2</artifactId>
<dependencies>
<dependency>
<groupId>com.mkscrg.sandbox</groupId>
<artifactId>module1</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
This project compiles successfully from the top, but running mvn exec:java -Dexec.mainClass=myMain
fails:该项目从顶部成功编译,但运行
mvn exec:java -Dexec.mainClass=myMain
失败:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] exec-multi-module-test
[INFO] module1
[INFO] module2
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building exec-multi-module-test 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ exec-multi-module-test >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ exec-multi-module-test <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ exec-multi-module-test ---
[WARNING]
java.lang.ClassNotFoundException: MyMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:680)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] exec-multi-module-test ............................ FAILURE [0.363s]
[INFO] module1 ........................................... SKIPPED
[INFO] module2 ........................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.566s
[INFO] Finished at: Mon Jun 18 14:11:54 PDT 2012
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project exec-multi-module-test: An exception occured while executing the Java class. MyMain -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
What's the right way to configure this project to allow exec:java
to see MyMain
?配置此项目以允许
exec:java
查看MyMain
的正确方法是什么?
EDIT: Here's a gist if you'd like to try this yourself: https://gist.github.com/2950830编辑:如果您想自己尝试,这里有一个要点: https : //gist.github.com/2950830
EDIT: Clarification: I know it's possible to mvn install
and then either run exec:java
from module2
's directory or use the -pl
flag from the top.编辑:澄清:我知道可以
mvn install
然后从module2
的目录运行exec:java
或使用顶部的-pl
标志。 However, I'd like to avoid running mvn install
.但是,我想避免运行
mvn install
。 It shouldn't be necessary to modify my local repository in order to run this goal in a multi-module project.为了在多模块项目中运行这个目标,没有必要修改我的本地存储库。 Just as
mvn compile
"just works" with a multi-module project, so should other goals/phases.正如
mvn compile
在多模块项目中“正常工作”一样,其他目标/阶段也应该如此。
Goals in a multi-module project, when run from the parent, will run against all modules.多模块项目中的目标,当从父模块运行时,将针对所有模块运行。 I don't think that's what you want.
我不认为那是你想要的。 You can try:
你可以试试:
mvn exec:java -pl module2 -Dexec.mainClass=MyMain
That might work?那可能有用吗? More info:
更多信息:
However, I think it's more intuitive to change directory to the sub-module containing the executable before running it.但是,我认为在运行之前将目录更改为包含可执行文件的子模块更直观。
exec-maven-plugin
to a maven lifecycle goal, say verify
.exec-maven-plugin
绑定到 Maven 生命周期目标,例如verify
。module
2, define the plugin configurations in the parent pom within pluginManagement
.module
2 执行,请在pluginManagement
中的父 pom 中定义插件配置。 Use the same only in module 2
.module 2
使用相同的内容。 mvn verify -Dexec.mainClass=MyMain
. mvn verify -Dexec.mainClass=MyMain
。
parent pom父 pom
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
module 2模块 2
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
See this answer for a single-command alternative without mvn install
:有关没有
mvn install
的单命令替代方案,请参阅此答案:
https://stackoverflow.com/a/26448447/1235281 https://stackoverflow.com/a/26448447/1235281
By using skip
in the parent pom.xml
, you can tell Maven to only run exec:java
on a specific submodule.通过在父
pom.xml
使用skip
,您可以告诉 Maven 仅在特定子模块上运行exec:java
。
GitHub: https://github.com/Oduig/mavenfiddle GitHub: https://github.com/Oduig/mavenfiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.