August 29, 2009

Create new Maven Plugin

« Spring Quartz Example | Main | Spring Mail Example »

The creation of new Maven plugins is easy and straight forward. The basic class of Maven plugins is the Mojo class. This class is extended from AbstractMojo. The execute() method of the Mojo class is being executing during the Maven build process. Configuration parameters will be injected directly to the members of the Mojo class.

The following example describes the steps of how to create a Maven plugin:
First Step - create Maven plugin artifact:

mvn archetype:create -DgroupId=org.developers.blog -DartifactId=maven-hello-world-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-mojo
Second Step - Implement your Mojo
package org.developers.blog;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.File;

/**
 * Goal which touches a timestamp file.
 *
 * @goal sayHello
 * 
 * @phase process-sources
 */
public class HelloWorldMojo extends AbstractMojo {
    /**
     * add standard maven environment parameters
     * @parameter expression="${project.build.directory}"
     * @required
     */
    private File outputDirectory;

    /**
     * a paramter which will be injected from configuration/myName in pom.xml
     * @parameter
     * @required
     */
    private String myName;
    
    //the main logic method of your plugin
    public void execute() throws MojoExecutionException {
        getLog().info("Hello Mister " + myName);
    }
}
After that your build your project and configures it at another project pom.xml file.

Third Step - Usage of the Maven plugin
<build>
    <plugins>
        <plugin>
            <groupId>org.developers.blog</groupId>
            <artifactId>maven-hello-world-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <configuration>
                <myName>Rafael Sobek</myName>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>sayHello</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
After that you can call the plugin with the following command.
mvn org.developers.blog:maven-hello-world-plugin:1.0-SNAPSHOT:sayHello
The result:
[INFO] [hello-world:sayHello]
[INFO] Hello Mister Rafael Sobek
The full example you can download here.

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 2:43 PM in Maven

 

[Trackback URL for this entry]

Comment: deepali at Di, 15 Nov 9:41 AM

Hi while following above steps, I got following error message:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error building POM (may not be this project's POM).


Project ID: org.developers.blog:maven-hello-world-plugin

Reason: POM 'org.developers.blog:maven-hello-world-plugin' not found in repository:
Unable to download the artifact from any repository

org.developers.blog:maven-hello-world-plugin:pom:1.0-SNAPSHOT

from the specified remote repositories:
central (http://repo1.maven.org/maven2)

for project org.developers.blog:maven-hello-world-plugin


[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Nov 15 14:52:19 IST 2011
[INFO] Final Memory: 1M/4M
[INFO] ------------------------------------------------------------------------

how to resolve this?

Comment: Tim at Mi, 14 Mrz 10:23 AM

Use 'mvn install' to build your plugin and install it in your local repository?

(Unfortunately, most maven folks seem to assume everyone already knows how maven works.)

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
 
test