Maven plugins configuration can be done by specifying a <configuration> element where the child elements of the <configuration> element are mapped based on plugin fields.

Types of Plugin

In Maven, there are 2 types of plugin available,

  • Build plugins will be executed during the build and then, they should be configured in the <build/> element.
  • Reporting plugins will be executed during the site generation and they should be configured in the <reporting/> element.

All plugins should have minimal required informationgroupIdartifactId and version.

Control Flow

The Maven Plugin framework handles processing of parameters from pom.xml or the command line, and calls execute().

Configuring Build Plugins

The following is only to configure Build plugins in the <build> element.

Using the <executions> Tag

You can also configure a mojo using the <executions> tag.

Sample build and executions element:

<project>
...
<build>
 <plugin>
   <artifactId>maven-mytest-plugin</artifactId>
   <version>1.8.1</version>
   <executions>
     <execution>
       <id>execution1</id>
       <phase>test</phase>
       <configuration>
         <url>http://www.test.com/id</url>
         <version>4.0.0</version>
       </configuration>
       <goals>
         <goal>mytest</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
</build>
...
</project>

The execution with id “execution1” binds this configuration to the test phase. If the goal has a default phase binding then it will execute in that phase. But if the goal is not bound to any lifecycle phase then it simply won’t be executed during the build lifecycle.

Note: Configurations inside the <executions> element used to differ from those that are outside <executions> in that they could not be used from a direct command line invocation because they were only applied when the lifecycle phase they were bound to was invoked. So you had to move a configuration section outside of the executions section to apply it globally to all invocations of the plugin. Since Maven 3.3.1 this is not the case anymore as you can specify on the command line the execution id for direct plugin goal invocation. Hence if you want to run the above plugin and it’s specific execution1’s configuration from the command-line, you can execute:

mvn mytestplugin:[email protected]

Using the <dependencies> Tag

You could configure the dependencies of the Build plugins, commonly to use a more recent dependency version.

For instance, the Maven Antrun Plugin version 1.2 uses Ant version 1.6.5, if you want to use the latest Ant version when running this plugin, you need to add <dependencies> element like the following:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.2</version>
        ...
        <dependencies>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.1</version>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-launcher</artifactId>
            <version>1.7.1</version>
          </dependency>
         </dependencies>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Using the <inherited> Tag In Build Plugins

By default, plugin configuration should be propagated to child POMs, so to break the inheritance, you could use the <inherited> tag:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.2</version>
        <inherited>false</inherited>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

Configuring Reporting Plugins

The following is only to configure Reporting plugins in the <reporting> element.

Using the <reporting> Tag VS <build> Tag

Configuring a reporting plugin in the <reporting> or <build> elements in the pom does NOT have the same behavior!

mvn site
It uses only the parameters defined in the <configuration> element of each reporting Plugin specified in the <reporting> element, i.e. site always ignores the parameters defined in the <configuration> element of each plugin specified in <build>.
mvn aplugin:areportgoal
It uses firstly the parameters defined in the <configuration> element of each reporting Plugin specified in the <reporting> element; if a parameter is not found, it will look up to a parameter defined in the <configuration> element of each plugin specified in <build>.

Using the <reportSets> Tag

You can configure a reporting plugin using the <reportSets> tag. This is most commonly used to generate reports selectively when running mvn site. The following will generate only the project team report.

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.1.2</version>
        <reportSets>
          <reportSet>
            <reports>
              <report>project-team</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

Notes:

  1. To exclude all reports, you need to use:
<reportSets>
   <reportSet>
     <reports/>
   </reportSet>
 </reportSets>
  1. Refer to each Plugin Documentation (i.e. plugin-info.html) to know the available report goals.

Using the <inherited> Tag In Reporting Plugins

Similar to the build plugins, to break the inheritance, you can use the <inherited> tag:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.1.2</version>
        <inherited>false</inherited>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

Conclusion

Hence we saw about types of plugin, build plugin configuration, reporting plugin configuration.

Please follow and like us:
error