How to execute profile last in Maven

Viewed 145

I have three different plugins. The execution of one of them is wrapped in a profile and disabled by default.

<profiles>
    <profile>
        <id>profile-exec</id>
        <build>
        ...
        <phase>generate-sources</phase>
        ....
        </build>
        <activation>
            <activeByDefault>false<activeByDefault>
        </activation>
    </profile>
    <profile>
</profiles>
<build>
    <plugins>
        <plugin>
        ...
        <phase>generate-sources</phase>
        <id>test1</id>
        </plugin>
        <plugin>
        ...
        <phase>generate-sources</phase>
        <id>test2</id>
        </plugin>
    </plugins>
</build>

When I run mvn generate-sources -Pprofile-exec, I see that the order of execution is the following:

test1
profile-exec
test2

what I am looking for is to obtain this order:

test1
test2
profile-exec

so that the profile is executed last. Is this possible? If so, how can it be achieved?

1 Answers

Plugins are invoked in the order they are defined. So:

  1. Define all 3 of them in the right order inside pluginManagement section.
  2. Turn on default plugins in build/plugins section.
  3. Turn on the 3d plugin in the profile

If you had 3 executions of the same plugin and you wanted to set the order, you'd apply an additional trick:

  1. Define all executions in the right order inside pluginManagement section.
  2. For the one that's supposed to be turned off use <phase>none</phase> so that it's not actually run.
  3. Then in profile declare the turned off execution with the same id and bind it to the appropriate phase.
Related