Using Maven Plugins

As noted earlier in the chapter, to customize the build for a Maven project, you must include additional Maven plugins, or configure parameters for the plugins already included in the build.

For example, you may want to configure the Java compiler to allow JDK 5.0 sources. This is as simple as adding the following to your POM:

<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>

You’ll notice that all plugins in Maven 2 look very similar to a dependency, and in some ways they are. If it is not present on your local system, this plugin will be downloaded and installed automatically in much the same way that a dependency would be handled. To illustrate the similarity between plugins and dependencies, the groupId and version elements have been shown, but in most cases these elements are not required.

If you do not specify a groupId, then Maven will default to looking for the plugin with the org.apache.maven.plugins or the org.codehaus.mojo groupId label. You can specify an additional groupId to search within your POM, or settings.xml.

If you do not specify a version then Maven will attempt to use the latest released version of the specified plugin. This is often the most convenient way to use a plugin, but you may want to specify the version of a plugin to ensure reproducibility. For the most part, plugin developers take care to ensure that new versions of plugins are backward compatible so you are usually OK with the latest release, but if you find something has changed – you can lock down a specific version.

The configuration element applies the given parameters to every goal from the compiler plugin. In the above case, the compiler plugin is already used as part of the build process and this just changes the configuration.

If you want to find out what the plugin’s configuration options are, use the mvn help:describe command. If you want to see the options for the maven-compiler-plugin shown previously, use the following command:

mvn help:describe -DgroupId=org.apache.maven.plugins \ 
-DartifactId=maven-compiler-plugin -Dfull=true

You can also find out what plugin configuration is available by using the Maven Plugin Reference section at http://maven.apache.org/plugins/ and navigating to the plugin and goal you are using.