Posts tagged ‘maven plugin step-by-step guide java’

step by step maven 2 simple plugin creation

Here’s a rough step by step maven 2 plugin creation mini howto.
It’s here, because I couldn’t find a page with all those command line instructions in one place.
We assume you have a suitable JDK, maven2 and an IDE ready.

1 – Generate the plugin skeleton
We ask maven to generate a sample plugin skeleton.
The whole projects layout along with directories, sample mojo java code and default configuration will be created for us :

 mvn archetype:create -DgroupId=net.morlhon -DartifactId=hello-plugin DpackageName=net.morlhon  -DarchetypeArtifactId=maven-archetype-mojo

2 – Write the plugin logic
We need to write the actual logic of our plugin, we need to edit the Mojo generated to suits our need.
But first, we ask maven to generate the project files for your favorite IDE (example with eclipse, but this works also with idea)

 cd hello-plugin mvn eclipse:eclipse

Launch eclipse, in the java perspective right click and choose “import” then “Existing project from workspace”.
Select the location of your project.
Remove the MyMojo class and create a new class with the same name and add your plugin logic :


package net.morlhon;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
/**
 * Simple goal which prints a nice greeting message on console.
 *
 * @goal hello-world
 * @description  Simple goal which prints a nice greeting message on console.
 */
public class MyMojo extends AbstractMojo {
     public void execute() throws MojoExecutionException {
         System.out.println("=================");
         System.out.println("> Hello world ! <");
         System.out.println("=================");
     }
}

Notice the @goal annotation determining the goal under which this execute method will be called.
The core body of the execute method is the code which will be executed under our project.
Thus actullay binding the plugin goal to the phase of the project.
3 – Install plugin

To install the plugin, making it available to projects you issue the following command :

 mvn -DupdateReleaseInfo=true install

Now that the plugin is installed we could test it, on itself that’s not pretty but anyway :

 $ mvn net.morlhon:hello-plugin:1.0-SNAPSHOT:hello-world [INFO] Scanning for projects... [INFO] --------------------------------------------- [INFO] Building Maven Mojo Archetype [INFO]    task-segment: net.morlhon:hello-plugin:1.0-SNAPSHOT:hello-world [INFO] --------------------------------------------- [INFO] hello:hello-world ================= > Hello world ! < ================= [INFO] --------------------------------------------- [INFO] BUILD SUCCESSFUL [INFO] --------------------------------------------- [INFO] Total time: < 1 second [INFO] Finished at: Tue Nov 15 17:33:16 CET 2005 [INFO] Final Memory: 1M/2M [INFO] ---------------------------------------------

4 – Use the plugin on an external project

Testing in a generated project : We need an external project to test our plugin and check the result.
We ask maven to generate a simple project layout by issuing the command in an empty directory :

 mvn archetype:create -DgroupId=net.morlhon -DartifactId=test-app

We then edit the pom.xml and add near the end : This tells the current project to execute the hello-world goal (found in net.morlhon:hello-plugin artifact) during the phase named compile of the build lifecycle.

   <build>
     <plugins>
       <plugin>
         <groupId>net.morlhon</groupId>
         <artifactId>hello-plugin</artifactId>
         <executions>
           <execution>
             <phase>compile</phase>
             <goals>
               <goal>hello-world</goal>
             </goals>
           </execution>
         </executions>
       </plugin>
     </plugins>
   </build>

then running the compilation of this project gives :

 $ mvn compile
 [INFO] Scanning for projects...
 [INFO] ---------------------------------------------
 [INFO] Building Maven Quick Start Archetype
 [INFO]    task-segment: compile
 [INFO] ---------------------------------------------
 [INFO] resources:resources
 [INFO] Using default encoding to copy filtered resources.
 [INFO] compiler:compile Compiling 1 source file to /home/jlf/mvn-plugin/test-app/target/classes
 [INFO] hello:hello-world {execution: default}
 ================= > Hello world ! < =================
 [INFO] ---------------------------------------------
 [INFO] BUILD SUCCESSFUL
 [INFO] ---------------------------------------------
 [INFO] Total time: 2 seconds
 [INFO] Finished at: Tue Nov 15 17:41:20 CET 2005
 [INFO] Final Memory: 2M/8M
 [INFO] ---------------------------------------------

Success !
The plugin has been called during the compile phase.