In a multi-module project, how can you specify that you want to execute a plugin goal in all the child-modules, but not on the parent project? There is <pluginManagement>
, but that only defines the configuration for the execution -- the child modules would still need to reference the plugin to get the goal executed:
[...] However, this only configures plugins that are actually referenced within the plugins element in the children. (POM Reference)
Any other way to achieve this?
UPDATE: I've tried this according to Pascal's advice:
<!-- ... --> <packaging>pom</packaging> <modules> <module>child</module> </modules> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>integration-test</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <!-- ... -->
This will still generate a .jar for the parent project, even though the jar
goal is bound to the integration-test
phase.
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.
Plugin executions are ordered according to their phases. See https://maven.apache.org/ref/current/maven-core/lifecycles.html for the order of phases. For example, here mavin-plugin-1 is executed before maven-plugin-2 because the process-resources phase is defined as taking place before the compile phase.
A plugin is an extension to Maven, something used to produce your artifact (maven-jar-plugin for an example, is used to, you guess it, make a jar out of your compiled classes and resources). A dependency is a library that is needed by the application you are building, at compile and/or test and/or runtime time.
According to the Default Lifecycle Bindings, the bindings for a packaging pom
are:
Default Lifecycle Bindings - Packaging pom
package site:attach-descriptor install install:install deploy deploy:deploy
So if your parent POM has a <packaging>pom<packaging>
(this should be the case as pointed out in a comment) and if you bind your plugins to other phases than those above (see the Lifecycle Reference for a comprehensive list), they won't be executed during the build of the parent POM.
(EDIT: My initial answer is just wrong. If you bind a plugin goal to a particular phase, it will be triggered during that phase, regardless of the packaging of the project. The Default Lifecycle Bindings don't have anything to do with that, they are just default lifecycle bindings. All what matters is if the phase to which the plugin is bound is part of the build lifecyle.)
As you pointed out, you can use the pluginManagement
in the parent pom for the configuration of the plugin but if you really want to execute a plugin goal in children modules and not in the parent (you might have good reasons to do this but most of time, plugins won't have much effet on a module with a pom
packaging that doesn't have any content), you'll have to reference plugins in the plugins
element in the children.
Applied to your example, the parent pom.xml could define the following specifications:
<project> <packaging>pom</packaging> ... <modules> <module>child</module> </modules> ... <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>my-execution-id</id> <phase>integration-test</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> ... </plugins> </pluginManagement> </build> ... </project>
And in every child pom.xml
, only the following is required:
<project> ... <build> ... <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> </plugin> </plugins> ... </build> </project>
The described solution with plugin management is certainly correct, but in certain cases it does not fit. Suppose you would like to run several jar:jar goals in the child module each configured with its own settings (configuration) per execution. Or in general, when you don't want to force child poms to explicitly trigger the plugin(s).
In this case the solution that worked for me was to define the executions in the parent pom under a specific profile, and have it activated only in child poms for example by checking for existence of some file or property:
<profile> <id>generate-dc</id> <activation> <file> <exists>src/main/assembly/some.xml</exists> </file> </activation>
Then plugins won't be executed in the parent, but will be executed in all children if those contain the file, or set some property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With