Friday, October 3, 2008

How to develop your first maven plugin

Few weeks ago, i participated in a hackathon and my project was to write my first maven plugin ever. Unfortunately, i was not able to finish my hack in the same day and that was because i missed some small steps in the process, so i thought it would be worthwile to share what i discovered with the community. Here are the steps that i followed to create my first maven plugin.
  1. creating the folder structure for our project by typing this maven comand: mvn archetype:create \ -DgroupId=sample.plugin \ -DartifactId=maven-hello-plugin \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-mojo
  2. creating the project: cd to the folder that was generated by the command above and type this command to generate th project files. mvn idea:idea or mvn eclipse:eclipse
  3. open the project using your favorite IDE and then replace Mymojo.java content by the following code: package sample.plugin; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import java.io.IOException; /** * Goal which displays a hello message. * * @goal hello-message * * @phase package */ public class MyMojo extends AbstractMojo { /** * the hello message. * @parameter * @required */ private String message; public void execute() throws MojoExecutionException { getLog().info("Hello from :" + message); } }
  4. installing the plugin: mvn clean install
  5. executing the plugin: create another pom with different name, we'll call it here pom1.xml and then put this content in there.
       1:  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       2:    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       3:    <modelVersion>4.0.0</modelVersion>
       4:    <groupId>sample.plugin</groupId>
       5:    <artifactId>maven-hello-plugin</artifactId>
       6:    <packaging>maven-plugin</packaging>
       7:    <version>1.0-SNAPSHOT</version>
       8:    <name>maven-hello-plugin Maven Mojo</name>
       9:    <url>http://maven.apache.org</url>
      10:    <build>
      11:      <plugins>
      12:          <plugin>
      13:              <groupId>sample.plugin</groupId>
      14:              <artifactId>maven-hello-plugin</artifactId>
      15:              <version>1.0-SNAPSHOT</version>
      16:              <executions>
      17:                  <execution>
      18:                      <phase>package</phase>
      19:                      <configuration>
      20:                          <message>Jupiter</message>
      21:                      </configuration>
      22:                      <goals>
      23:                          <goal>
      24:                              hello-message
      25:                          </goal>
      26:                      </goals>
      27:                  </execution>
      28:              </executions>
      29:          </plugin>
      30:      </plugins>
      31:      </build>
      32:  
    
      33:  
    
      34:  
    
      35:      <dependencies>
      36:      <dependency>
      37:        <groupId>org.apache.maven</groupId>
      38:        <artifactId>maven-plugin-api</artifactId>
      39:        <version>2.0</version>
      40:      </dependency>
      41:      <dependency>
      42:        <groupId>junit</groupId>
      43:        <artifactId>junit</artifactId>
      44:        <version>3.8.1</version>
      45:        <scope>test</scope>
      46:      </dependency>
      47:    </dependencies>
      48:  </project>
    and then execute it: mvn -f pom1.xml package you'll see the message that we put in pom1.xml displayed in the logs.
You can download the project here.

No comments: