Search code examples
mavenmaven-3

How to execute maven plugin before resources are filtered when using war packaging


Summary

The first phase for <packaging>war</packaging> in the Default Lifecycle is for process-resources which immediately calls the maven-resources-plugin.

How can I execute a plugin before that runs?

I was hoping there is an easy way to change the ordering when running process-resources to let the another plugin run first.

Or perhaps force one of the earlier phases to be included in the lifecycle, without going as far as Creating a custom lifecycle

(I'm actually surprised that war packaging doesn't use validation or initialization since they seem to make sense for any type of build)

Detail

I'm trying to use the buildnumber-maven-plugin to generate generate the ${buildNumber} and use filtering to save it in the *.properties file which usually runs as part of the validate or initialize phase. So I tried to bind it to the process-resources phase for execution since it was the first.

The problem is that maven-resources-plugin is the very first thing to execute before the buildnumber-maven-plugin has a chance to generate ${buildNumber}. All other properties are filtered fine. (I'm aware I can probably do something in a later step with maven-war-plugin using the web.xml, or in the manifest, but that's not what I'm asking)

If they can't be reordered within process-resources, I was hoping I could force the initialization phase to be included in the lifecycle with some kind of easy configuration in the existing pom.xml

Perhaps something that looks like the components.xml when defining the custom lifecycle.

<configuration>
            <phases>
              <initialization>org.codehaus.mojo:buildnumber-maven-plugin:create</initialization>
            </phases>
</configuration>

Solution

  • If maven plug-ins are bound with same phase, then they executes in order of their listings within pom maven-resources-plugin is by default bounded to process-resources but it is declared in super pom and when you build, super pom, parent poms and current pom make up a single pom known as effective pom ( you can check your effective pom via mvn help:effective-pom)

    Now coming towards the solution, to change the order of execution of plugins.

    1. First add buildnumber-maven-plugin and bind it to process-resources
    2. Beneath buildnumber-maven-plugin add maven-resources-plugin and bind it to process-resources

    Now when you build both will be executed during process_resources phase but buildnumber-maven-plugin will be executed before maven-resources-plugin.