17.08.2016 Views

Apache Maven 3 Cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Extending <strong>Apache</strong> <strong>Maven</strong><br />

4. In this step, we included our plugin into a <strong>Maven</strong> project by appending to the<br />

element in the pom.xml.<br />

5. In the fifth step, we executed our plugin on the command line. Note that we used the<br />

fully qualified plugin name, which included the complete package structure.<br />

You have just created your first plugin, specified a custom goal, included the plugin in<br />

a project, and executed it from the command line. Not a bad start! :-)<br />

Making your Java <strong>Maven</strong> plugin useful<br />

This recipe is a continuation of the preceding recipe in which we saw how to create an <strong>Apache</strong><br />

<strong>Maven</strong> plugin in Java.<br />

We ran an archetype:generate, a <strong>Maven</strong> command to create a <strong>Maven</strong> plugin project and<br />

modified the MyMojo.java source and the POM file. We then built, installed, and executed<br />

the plugin from the command line.<br />

However, in a real-world scenario, in addition to what we explored in the preceding recipe, your<br />

plugin may need:<br />

ff<br />

ff<br />

ff<br />

ff<br />

Support for multiple goals<br />

External configuration<br />

Short commands<br />

Execution in build phase<br />

In this recipe, we take this to its logical conclusion. We have a fully-functional plugin and will<br />

understand how to tweak, reconfigure, and enhance it further.<br />

Getting ready<br />

A prerequisite for the recipe is the preceding recipe, Creating a <strong>Maven</strong> plugin using Java.<br />

You need to complete that recipe, create the <strong>Maven</strong> plugin project, and then continue with<br />

this recipe.<br />

How to do it...<br />

Follow the ensuing steps to support multiple goals in your <strong>Apache</strong> <strong>Maven</strong> plugin:<br />

1. In our maven-plug101-plugin project, let us create a new file called<br />

MyMojo2.java:<br />

package net.srirangan.packt.maven;<br />

192<br />

import org.apache.maven.plugin.AbstractMojo;<br />

import org.apache.maven.plugin.MojoExecutionException;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!