Pablo Fernandez
Pablo Fernandez

Reputation: 287390

Specify the order in which Maven plugins' executions run

I'm using the copy-rename-maven-plugin during the package phase to first copy the jar that I just generated to another directory, then using launch4j-maven-plugin I'm generating exes that wrap the jar and then I need to rename one of the exes (to scr), so, I'm using copy-rename-maven-plugin again.

The problem is that all copy-rename-maven-plugin executions are run together, before launch4j-maven-plugin, so, the second execution fails.

How do define the order of executions? I'm happy creating more phases if that's what's necessary, but creating a Maven plugin seemed a bit of an overkill.

A simplified example of what's going with my pom.xml would look like this:

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

    <groupId>tech.projecx</groupId>
    <artifactId>projecx</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution> <!-- Copy the just-built projecx jar to targte/win32/jars -->
                        <id>copy-jar-for-exe</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <sourceFile>${project.build.directory}/${project.build.finalName}.jar</sourceFile>
                            <destinationFile>${project.build.directory}/win32/jars/${project.build.finalName}.jar
                            </destinationFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin> <!-- Make the exes -->
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.7.21</version>
                <executions>
                    <execution> <!-- Make the screensaver exe -->
                        <id>wrap-screensaver-as-exe</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>gui</headerType>
                            <outfile>${project.build.directory}\win32\${screensaverExe}.exe</outfile>
                            <jar>jars\${project.build.finalName}.jar</jar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution> <!-- Copy the screensaver from the exe to the proper scr -->
                        <id>rename-screensaver-to-scr</id>
                        <phase>package</phase>
                        <goals>
                            <goal>rename</goal>
                        </goals>
                        <configuration>
                            <sourceFile>${project.build.directory}/win32/${screensaverExe}.exe</sourceFile>
                            <destinationFile>${project.build.directory}/win32/${screensaverExe}.scr</destinationFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The order in which executions need to run is this:

  1. copy-jar-for-exe
  2. wrap-screensaver-as-exe
  3. rename-screensaver-to-scr

Any other order doesn't work, but because, I think, copy-jar-for-exe and renamer-screensaver-to-scr are executions from the same plugin, Maven runs it like this:

  1. copy-jar-for-exe
  2. rename-screensaver-to-scr
  3. wrap-screensaver-as-exe

so, it fails.

Upvotes: 0

Views: 2758

Answers (1)

Gerald M&#252;cke
Gerald M&#252;cke

Reputation: 11122

You could run the copy-jar-for-exe in the prepare-package phase. I beleive you could define both executions in the same plugin configuration but declare the plugin after the launch4j plugin.

Basic idea is, the plugins with executions in the same phase are executed in the order of appearance in the pom. If you bind a single execution to another (earlier) phase, it should be executed before.

I haven't tested this, but I think it should work

<plugin> 
    <groupId>com.akathist.maven.plugins.launch4j</groupId>
    <artifactId>launch4j-maven-plugin</artifactId>
    <version>1.7.21</version>
    <executions>
        <execution> <!-- Make the screensaver exe -->
            <id>wrap-screensaver-as-exe</id>
            <phase>package</phase>
            <goals>
                <goal>launch4j</goal>
            </goals>
            <configuration>
                <headerType>gui</headerType>
                <outfile>${project.build.directory}\win32\${screensaverExe}.exe</outfile>
                <jar>jars\${project.build.finalName}.jar</jar>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>com.coderplus.maven.plugins</groupId>
    <artifactId>copy-rename-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution> 
            <id>copy-jar-for-exe</id>
            <phase>prepare-package</phase> <!-- run this execution before package phase -->
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <sourceFile>${project.build.directory}/${project.build.finalName}.jar</sourceFile>
                <destinationFile>${project.build.directory}/win32/jars/${project.build.finalName}.jar
                </destinationFile>
            </configuration>
        </execution>
        <execution> 
            <id>rename-screensaver-to-scr</id>
            <phase>package</phase>
            <goals>
                <goal>rename</goal>
            </goals>
            <configuration>
                <sourceFile>${project.build.directory}/win32/${screensaverExe}.exe</sourceFile>
                <destinationFile>${project.build.directory}/win32/${screensaverExe}.scr</destinationFile>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 1

Related Questions