Search code examples
mavenmaven-3maven-antrun-plugin

Execute maven ant-run plugin in the end in case of maven parallel builds


I am having a maven multi module project. A simplified pom.xml (under full-build) looks like below:-

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

The simplified pom.xml inside main looks like below:-

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

Whenever I try to execute the above project with mvn clean install -T 4 the collector-framework project gets skipped and the build fails with error

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

Everything works fine if I just do mvn clean install. I think this is happening because ant-run is running before the main module execution is finished. Can someone let me know how can I make ant-run wait till main module compilation is over in case of parallel builds?

  • Maven Version - 3.5.0

Solution

  • To resolve this I added the dependency of collector-framework to maven-antrun-plugin like below:-

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

    The above change makes maven-antrun wait till the collector jar is created.