In multi-module projects, pluginManagement
allows you to configure plugin information that can be used as required by child projects. The parent pom can define the configurations for various plugins used by different child projects. Each child project can chose the plugins that it needs for the build.
simple-multi-module
).pluginManagement
section to copy additional resources:<pluginManagement> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9.1</version> <executions> <execution> <id>add-resource</id> <phase>generate-resources</phase> <goals> <goal>add-resource</goal> </goals> <configuration> <resources> <resource> <directory>src/resources/xml</directory> <targetPath>xml</targetPath> </resource> <resource> <directory>src/resources/json</directory> <targetPath>json</targetPath> <includes> <include>include.json</include> </includes> <excludes> <exclude>exclude.json</exclude> </excludes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </pluginManagement> </build>
mvn clean test
Note that the additional resources are not getting copied in the child project.
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> </plugin> </plugins> </build>
[INFO] Copying 2 resources to xml [INFO] Copying 1 resource to json
We defined the Maven build helper plugin to copy resources from additional folders in the pluginManagement
section of the parent pom. It is not available to the child pom until the child uses the plugin. When the child project did not define the plugin, the plugin definition in the parent pom had no effect. When the child project defined the plugin, it took effect and the additional resources got copied over.
If a plugin is used as part of the build lifecycle, then its configuration in the pluginManagement
section will take effect, even if not explicitly defined by the child. Let us see how this happens:
pluginManagement
of the parent pom:<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> </pluginManagement>
mvn clean test
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3. 2:compile (default-compile) on project child: Fatal error compiling: invalid tar get release: 1.8 -> [Help 1]
What happened here? Even though the child pom did not define the Maven Compiler plugin, the configuration for the Maven Compiler plugin in the pluginManagement
section of the parent pom took effect because the compile
goal was part of the build lifecycle. As the configuration stipulated a Java 8 target, the compilation failed.
What if we do not want to inherit specific plugin configurations? Maven provides a way to do this. Let us see how:
<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <inherited>false</inherited> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> </pluginManagement>
mvn clean package
This is because the configuration was not inherited to the child module as we set the inherited
element to false
.
13.58.220.83