Search code examples
javamavenmaven-3maven-plugin

Maven changes the order of plugins of different profiles


I have a pom.xml where I define the same plugin (same groupId and artifactId, different execution :-) ) in two different profiles. The executions are defined in the same phase, so the order is calculated by the order from the xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>echo</groupId>
    <artifactId>test</artifactId>
    <name>echo-test</name>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <profiles>
        <profile>
            <id>1st-profile</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>1st-antrun-echo</id>
                                <phase>test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>1st antrun plugin</echo>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>2nd-profile</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.soebes.maven.plugins</groupId>
                        <artifactId>maven-echo-plugin</artifactId>
                        <version>0.1</version>
                        <executions>
                            <execution>
                                <id>1st-soebes-echo</id>
                                <phase>test</phase>
                                <goals>
                                    <goal>echo</goal>
                                </goals>
                                <configuration>
                                    <echos>
                                        <echo>1st echo-plugin</echo>
                                    </echos>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>2nd-antrun-echo</id>
                                <phase>test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>2nd antrun plugin</echo>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

All the plugin executions are defined in the test phase, therefore I would expect the following order:

1st antrun plugin
1st echo-plugin
2nd antrun plugin

However, since the antrun-plugins are merged, I get this output:

1st echo-plugin    
1st antrun plugin    
2nd antrun plugin

This command explains why is this happening: mvn help:effective-pom

Is there any other solution to preserve the order other than to introduce a new phases? Our project is really big and this is a very simplified example.

Why is this limitation of maven to merge the plugins into one with multiple executions?


Solution

  • In my experience this is one of the biggest bugs in Maven. If you have more than one configuration for the same plugin in different profiles, the order is simply unpredictable. I even observed, that I had some plugin order in project B in a given phase, and as soon as some of the same plugins got a config in a parent project (not even in the same phase), the order was ruined.

    There is an obviously falsely closed bug related to this at https://issues.apache.org/jira/browse/MNG-2258.

    Possible workarounds

    • Try to shift some of the plugins to previous phase if possible (prepare-test used for some of the plugins and test for the rest)
    • Try to replace the functionality of multiple plugins with a groovy-maven-plugin script (quite handy with the ant integration, and you reach the list of the active profiles in the script)
    • Write your own mojo and call the plugins in the correct order (See https://github.com/TimMoore/mojo-executor)
    • Give a try to gradle. Maybe it fits your needs, and it comes with a lot of good things from Maven

    I do not think that there is any more you can do right now.