【问题标题】:Execute maven ant-run plugin in the end in case of maven parallel builds在 maven 并行构建的情况下,最后执行 maven ant-run 插件
【发布时间】:2017-06-13 11:58:40
【问题描述】:

我有一个 Maven 多模块项目。简化的pom.xml(在full-build 下)如下所示:-

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <groupId>com.eros</groupId>
    <artifactId>full-build</artifactId>
    <version>0.001-SNAPSHOT</version>
    <name>full-build</name>
    <profiles>
        <profile>
            <id>build-only</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <modules>
                <module>../../main</module>
            </modules>
        </profile>
        <profile>
            <id>copy-only</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <configuration>
                                    <tasks>
                                        <!-- collector -->
                                        <copy file="../collector-framework/collector/target/collector-${project.version}.jar"
                                              tofile="./target/collector/collector-${project.version}.jar"/>
                                    </tasks>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

main 内部的简化 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.eros</groupId>
    <artifactId>main</artifactId>
    <version>0.001-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>main</name>
    <modules>
        <module>collector-framework</module>
    </modules>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <id>enforce-versions</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireJavaVersion>
                                    <version>[1.8,)</version>
                                    <message>*** This project requires JDK 1.8/J2SE 8 or later. ***</message>
                                </requireJavaVersion>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

每当我尝试使用 mvn clean install -T 4 执行上述项目时,collector-framework 项目都会被跳过并且构建失败并出现错误

Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (default) on project full-build: An Ant BuildException has occured: Warning: Could not find file /Users/tuk/code/github/eros/main/collector-framework/collector/target/collector-0.001-SNAPSHOT.jar to copy.
[ERROR] around Ant part ...<copy file="../collector-framework/collector/target/collector-0.001-SNAPSHOT.jar" tofile="./target/collector/collector-0.001-SNAPSHOT.jar"/>... @ 4:143 in /Users/tuk/code/github/eros/main/full-build/target/antrun/build-main.xml

如果我只做mvn clean install,一切都会很好。我认为这是因为 ant-run 在 main 模块执行完成之前运行。有人可以告诉我如何让ant-run 在并行构建的情况下等待主模块编译结束吗?

  • Maven 版本 - 3.5.0

【问题讨论】:

    标签: maven maven-3 maven-antrun-plugin


    【解决方案1】:

    为了解决这个问题,我将collector-framework 的依赖项添加到maven-antrun-plugin,如下所示:-

    <build>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.8</version>
            <dependencies>
              <dependency>
                <groupId>a.b.c</groupId>
                <artifactId>collector</artifactId>
                <version>0.001-SNAPSHOT</version>
              </dependency>
             </dependencies>
             <executions>
               <execution>
                 <phase>package</phase>
                 <configuration>
                   <tasks>
                     <!-- collector -->
                     <copy file="../collector-framework/collector/target/collector-${project.version}.jar"                                                  tofile="./target/collector/collector-${project.version}.jar"/>
                   </tasks>
                 </configuration>
                 <goals>
                   <goal>run</goal>
                 </goals>
               </execution>
             </executions>
           </plugin>
         </plugins>
       </build>
    

    上述更改使 maven-antrun 等到收集器 jar 被创建。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2016-02-20
      • 2017-12-08
      • 1970-01-01
      • 2020-01-17
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多