Configuring Maven to search for plugins

You will recall that in the section on using the Maven JaCoCo plugin, to generate code coverage we had to explicitly specify the projectId and artifactId values of the plugin to it from the command line. However, for most other plugins, we specified the plugin name without additional information.

We will see why we had to do this and how to avoid it.

How to do it...

  1. Open the settings file (specifically the settings.xml file in your home directory).
  2. Add the following section:
    <pluginGroups>
        <pluginGroup>org.jacoco</pluginGroup>
    </pluginGroups>
  3. Run the following command on the same project for which you ran JaCoCo earlier:
    mvn clean jacoco:prepare-agent test jacoco:report
    
  4. Observe the output:
    [INFO]
    [INFO] --- jacoco-maven-plugin:0.7.2.201409121644:report (default-cli) @ project-with-tests ---
    [INFO] Analyzed bundle 'Project with Tests' with 1 classes
    

How it works...

There are two types of Maven plugins, which are as follows:

  • Plugins maintained by the Maven team itself (let us call them official plugins). These are in the default plugin groups org.apache.maven.plugins and org.codehaus.mojo.
  • All other plugins (let's say third-party plugins).

All official plugins have the same groupId, namely org.apache.maven.plugins. They also have a convention for artifactId: maven-${prefix}-plugin, where prefix stands for the plugin prefix, the short name to refer to the plugin.

The prefix used to reference the plugin can be customized as well. The prefix can be specified directly through the goalPrefix configuration parameter on the Maven-plugin-plugin of the plugin's pom file.

So, when we run mvn clean, Maven looks for the maven-clean-plugin in the org.apache.maven.plugins group.

What about third-party plugins? pluginGroups lets Maven know the groupId where it should search for additional plugins. So in the earlier case, Maven searched for plugins in the org.jacoco group.

Third-party plugins should be named differently from official plugins. The conventional way to define the artifactId for third-party plugins is ${prefix}-maven-plugin. When specified in this way, Maven automatically identifies the shortcut name for the plugin. In the earlier case, as the artifactId is jacoco-maven-plugin, the shortcut is jacoco.

There's more...

Maven will always search specified pluginGroups before it searches the following default groups:

  • org.apache.maven.plugins
  • org.codehaus.mojo

Maven takes the first match for the shortcut that it finds. For instance, if there is a clean shortcut in a user-specified plugin in pluginGroups, it will take precedence over a Maven Clean plugin.

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

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