Maven exec:多模块项目的java目标

时间:2012-06-18 21:24:15

标签: java maven exec-maven-plugin

我正在尝试在一个简单的双模块项目中运行exec-maven-plugin的{​​{1}}目标,其中一个模块依赖于另一个模块。到目前为止,我找不到有效的配置。这是一个简单的测试用例:

  • EXEC-多模块检验/
    • 的pom.xml
    • 模块1 /
      • 的pom.xml
      • 的src /
        • 主/
          • 的java /
            • HelloPrinter.java
    • 模块2 /
      • 的pom.xml
      • 的src /
        • 主/
          • 的java /
            • MyMain.java

这是父母pom:

exec:java

<?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> 的pom:

module1

<?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> 的pom:          http://maven.apache.org/xsd/maven-4.0.0.xsd">         4.0.0

module2

此项目从顶部成功编译,但运行 <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> 失败:

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 查看exec:java的正确方法是什么?

编辑:如果你想自己尝试一下,这是一个要点:https://gist.github.com/2950830

编辑:澄清:我知道可以MyMain然后从mvn install目录运行exec:java或使用顶部的module2标记。但是,我想避免运行-pl。没有必要修改我的本地存储库以便在多模块项目中运行此目标。正如mvn install“只适用于”多模块项目一样,其他目标/阶段也是如此。

3 个答案:

答案 0 :(得分:30)

从父项运行时,多模块项目中的目标将针对所有模块运行。我不认为这就是你想要的。你可以尝试:

mvn exec:java -pl module2 -Dexec.mainClass=MyMain

那可能有用吗?更多信息:

但是,我认为在运行目录之前将目录更改为包含可执行文件的子模块更为直观。

答案 1 :(得分:8)

  • 您应该将exec-maven-plugin绑定到maven生命周期目标,例如verify
  • 由于您希望仅为module 2执行插件,请在pluginManagement内的父pom中定义插件配置。仅在module 2中使用相同的内容。
  • 然后运行以下命令:

mvn verify -Dexec.mainClass=MyMain

父母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>

模块2

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

答案 2 :(得分:1)

对于没有mvn install的单命令替代方案,请参阅此答案:

https://stackoverflow.com/a/26448447/1235281

通过在父skip中使用pom.xml,您可以告诉Maven仅在特定子模块上运行exec:java

GitHub的: https://github.com/Oduig/mavenfiddle