17.08.2016 Views

Apache Maven 3 Cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9<br />

How to do it...<br />

We start by generating an <strong>Apache</strong> <strong>Maven</strong> plugin project using an archetype. Archetypes, as<br />

discussed earlier, are project templates, and the <strong>Maven</strong> community has made an archetype<br />

available for generating a basic <strong>Maven</strong> plugin project.<br />

1. Start your console and execute the following command:<br />

$ mvn archetype:generate -DgroupId=net.srirangan.packt.maven<br />

-DartifactId=maven-plug101-plugin -DarchetypeGroupId=org.apache.<br />

maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo<br />

2. Now build and install this project in your local repository with the following command:<br />

$ mvn clean install<br />

3. Open the MyMojo.java file in any text editor. If you are using an IDE (such as IntelliJ<br />

IDEA or Eclipse), you can start by importing the project and then opening MyMojo.<br />

java for editing.<br />

You will see that the source code for the MOJO is already populated. We will replace<br />

that code with an even simpler example, as shown in the following code:<br />

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

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

/**<br />

* @goal helloworld<br />

*/<br />

public class MyMojo extends AbstractMojo {<br />

public void execute() throws MojoExecutionException {<br />

getLog().info("Hello, world.");<br />

}<br />

}<br />

4. Let's now modify the POM file (pom.xml) to include this plugin:<br />

...<br />

<br />

<br />

<br />

net.srirangan.packt.maven<br />

maven-plug101-plugin<br />

1.0-SNAPSHOT<br />

<br />

<br />

<br />

...<br />

189

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

Saved successfully!

Ooh no, something went wrong!