50

我试图在一个简单的两模块项目上运行exec-maven-plugin目标exec:java,其中一个模块依赖于另一个模块。到目前为止,我找不到有效的配置。这是一个简化的测试用例:

+ exec-multi-module-test/
    + pom.xml
    + module1/
        + pom.xml
        + src/
            + main/
                + java/
                    + HelloPrinter.java
    + module2/
        + pom.xml
        + src/
            + main/
                + java/
                    + MyMain.java

这是父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的绒球:

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

此项目从顶部编译成功,但运行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查看的正确方法是什么MyMain

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

编辑:澄清:我知道有可能mvn install然后exec:javamodule2's 目录运行或使用-pl顶部的标志。但是,我想避免运行mvn install. 无需修改我的本地存储库即可在多模块项目中运行此目标。就像mvn compile多模块项目“正常工作”一样,其他目标/阶段也应该如此。

4

3 回答 3

46

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

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

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

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

于 2012-06-18T21:47:26.503 回答
12
  • 您应该将 绑定exec-maven-plugin到 Maven 生命周期目标,例如verify.
  • 由于您希望插件仅针对module2 执行,因此请在父 pom 中定义插件配置pluginManagement。仅在module 2.
  • 然后运行以下命令:

mvn verify -Dexec.mainClass=MyMain.

家长会

<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>
于 2012-06-19T04:43:05.293 回答
4

请参阅此答案以获取不带的单命令替代方案mvn install

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

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

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

于 2017-03-20T12:48:11.427 回答