Performing multi-module plugin management

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.

How to do it...

  1. Open a multi-module project (simple-multi-module).
  2. Add a configuration for the Maven build helper plugin in the 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>
  3. Run the following command to build the project:
        mvn clean test 
    

    Note that the additional resources are not getting copied in the child project.

  4. Now, use the corresponding plugin in the child project:
        <build>
           <plugins>
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>build-helper-maven-plugin</artifactId>
               </plugin>
           </plugins>
       </build>
  5. Build the project again.
  6. Observe the output:
    [INFO] Copying 2 resources to xml
    [INFO] Copying 1 resource to json
    

How it works...

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.

There's more...

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:

  1. Define the Maven compiler plugin in 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>
  2. Without adding the plugin to the child pom, run the following command using Java 7:
    mvn clean test
    
  3. Observe the error:
    [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:

  1. Update the preceding Maven Compiler plugin configuration as follows:
      <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>
  2. Now run the following using Java 7:
    mvn clean package
    
  3. Observe that the project compiles without errors, though the plugin configuration specified Java 8.

This is because the configuration was not inherited to the child module as we set the inherited element to false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.218.221