2012-12-16

Maven: Order of execution of plugins in profiles

I was curious in which order Maven executes the plugins: Whether it uses POM profile appearance order, or uses the ordering from the command line's -P param value.

So I put this in a pom.xml:

01.<profiles>
02.    <profile>
03.      <id>test1</id>
04.      <build>
05.        <plugins>
06.          <plugin>
07.            <groupId>org.apache.maven.plugins</groupId>
08.            <artifactId>maven-antrun-plugin</artifactId>
09.            <executions>
10.              <execution>
11.                <id>test1ex</id>
12.                <phase>initialize</phase>
13.                <goals>
14.                  <goal>run</goal>
15.                </goals>
16.                <configuration>
17.                  <tasks>
18.                    <echo>test1 ECHO.</echo>
19.                  </tasks>
20.                </configuration>
21.              </execution>
22.            </executions>
23.          </plugin>
24.        </plugins>
25.      </build>
26.    </profile>
27. 
28.    <profile>
29.      <id>test2</id>
30.      <build>
31.        <plugins>
32.          <plugin>
33.            <groupId>org.apache.maven.plugins</groupId>
34.            <artifactId>maven-antrun-plugin</artifactId>
35.            <executions>
36.              <execution>
37.                <id>test2ex</id>
38.                <phase>initialize</phase>
39.                <goals>
40.                  <goal>run</goal>
41.                </goals>
42.                <configuration>
43.                  <tasks>
44.                    <echo>test2 ECHO.</echo>
45.                  </tasks>
46.                </configuration>
47.              </execution>
48.            </executions>
49.          </plugin>
50.        </plugins>
51.      </build>
52.    </profile>
53. 
54.</profiles>

Then I launched it using:

1.mvn initialize -P test2,test1

And the result?

01.[INFO] Scanning for projects...
02.[INFO] ------------------------------------------------------------------------
03.[INFO] Building pohlidame-backend
04.[INFO]    task-segment: [install]
05.[INFO] ------------------------------------------------------------------------
06.[INFO] [antrun:run {execution: test1ex}]
07.[INFO] Executing tasks
08.     [echo] test1 ECHO.
09.[INFO] Executed tasks
10.[INFO] [antrun:run {execution: test2ex}]
11.[INFO] Executing tasks
12.     [echo] test2 ECHO.
13.[INFO] Executed tasks
14....

Plugin execution order follows the order in pom.xml. The only way to get test2 run first is to swap them in pom.xml.

Executed twice?

Another interesting thing is what happens when when launched using:

1.mvn clean install -Dmaven.test.skip=true -P test1,test2

In this case, the AntRun plugin is executed four times:

01.mvn install -Dmaven.test.skip=true -P test1,test2
02.[INFO] Scanning for projects...
03.[INFO] ------------------------------------------------------------------------
04.[INFO] Building pohlidame-backend
05.[INFO]    task-segment: [install]
06.[INFO] ------------------------------------------------------------------------
07.[INFO] [antrun:run {execution: test1ex}]
08.[INFO] Executing tasks
09.     [echo] test2 ECHO.
10.[INFO] Executed tasks
11.[INFO] [antrun:run {execution: test2ex}]
12.[INFO] Executing tasks
13.     [echo] test1 ECHO.
14.[INFO] Executed tasks
15.[INFO] [resources:resources]
16.[INFO] Using default encoding to copy filtered resources.
17.[INFO] [compiler:compile]
18.[INFO] Nothing to compile - all classes are up to date
19.[INFO] [resources:testResources]
20.[INFO] Using default encoding to copy filtered resources.
21.[INFO] [compiler:testCompile]
22.[INFO] Not compiling test sources
23.[INFO] [surefire:test]
24.[INFO] Tests are skipped.
25.[INFO] [jar:jar]
26.[INFO] Preparing source:jar
27.[WARNING] Removing: jar from forked lifecycle, to prevent recursive invocation.
28.[INFO] [antrun:run {execution: test1ex}]
29.[INFO] Executing tasks
30.     [echo] test2 ECHO.
31.[INFO] Executed tasks
32.[INFO] [antrun:run {execution: test2ex}]
33.[INFO] Executing tasks
34.     [echo] test1 ECHO.
35.[INFO] Executed tasks
36.[INFO] [source:jar {execution: attach-sources}]
37.[INFO] Building jar: C:\...\target\isirbackend-sources.jar
38.[INFO] [install:install]
39.[INFO] Installing C:\...\target\isirbackend.jar to C:\...\.m2\repository\cz\...-1.6.03-SNAPSHOT.jar
40.[INFO] Installing C:\...\target\isirbackend-sources.jar to C:\...\.m2\repository\cz\...-1.6.03-SNAPSHOT-sources.jar
41.[INFO] ------------------------------------------------------------------------
42.[INFO] BUILD SUCCESSFUL
43.[INFO] ------------------------------------------------------------------------
44.[INFO] Total time: 7 seconds
45.[INFO] Finished at: Fri May 08 07:02:31 CEST 2009
46.[INFO] Final Memory: 29M/247M
47.[INFO] ------------------------------------------------------------------------

Does that mean that the initialize phase is used before packaging the .jar?


0