Search code examples
mavenmaven-resources-plugin

How to trigger a specific execution of a Maven plugin?


I am trying to figure out whether it is possible to define executions of a Maven plugin in a <pluginManagement> section of a parent pom and pick a specific execution, and only that one, in a child project.

To be more specific, I have several multi-module projects that inherit from our company-wide parent pom. In the <pluginManagement> section of the parent pom, I have several executions of maven-resources-plugin using the copy-resources goal, all bound to phase validate but using different configurations:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <inherited>true</inherited>
    <executions>
        <execution>
            <id>copy-codedeploy</id>
            <phase>validate</phase>
            ...
        </execution>
        <execution>
            <id>copy-settings</id>
            <phase>validate</phase>
            ...
        </execution>

        <execution>
            <id>copy-logback</id>
            <phase>validate</phase>
            ...
        </execution>
    </executions>
<plugin>

After experimenting many hours on a child project, I would like to:

  1. Avoid having needless executions of copy-logback in all modules, which happens when I define execution copy-logback in the parent POM and has the drawback of creating the destination folder hierarchy in all modules.
  2. Avoid, in case that execution is not defined in the parent POM, defining it in multiple cloned copies in all modules where it is needed.

An example child project has these modules, where the first one is the only module where copy-logback is really needed:

  • webapp
  • rest
  • services
  • persistence

The best I have arrived at now is to leave common executions in the parent POM, which gives me item #1 in the first list above, but still leaves me cloning the exact same plugin configuration in multiple modules. The configuration that I'm cloning is this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <!-- Copy logback.xml from the parent to the main resources folder, filtering the application name used in log file names. -->
        <execution>
            <id>copy-logback-file</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.basedir}/../logback-templates</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>logback.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

This unfortunately diminishes the usefulness of a <pluginManagement> section in the parent POM as the identical configuration cannot be defined only once. I have to define it in each module of our Portfolio that uses logback, which amounts to tens of times.

To solve my duplication problem, I am thinking that maybe, it's possible to define an execution of a plugin that can be selectively triggered where it's needed. Is it possible?


Solution

  • You can add a skip parameter to the configuration of each execution like:

    <skip>${copy.logback.skip}</skip>
    

    And then you can choose in each module whether to use or skip the execution by setting the property <copy.logback.skip> to true or false.