The contents of this document are a work in progress

Implementing the Monitor Mojo

If you would like to understand what a Maven Mojo is, please refer to the documentation here on the Maven website.

Monitor Mojo

We noticed that the archetype creator filled in the maven plugin project with some reasonable defaults. It also created a MyMojo placeholder implementation of a Mojo for us, but it doesn't do much for us.

So,

  1. Delete MyMojo implementation from the project sources.
  2. Create a MonitorMojo that extends org.apache.maven.plugin.AbstractMojo.

    The Mojo source should like this:

    public class MonitorMojo
        extends AbstractMojo
    {
    
        public void execute()
            throws MojoExecutionException, MojoFailureException
        {
            // TODO Auto-generated method stub
    
        }
    
    }
    
  3. Add a Javadoc @goal annotation in Javadoc comments for a Class to bind a goal name that runs this Mojo.
    /**
     * A Mojo that monitors a given list of websites.
     * 
     * @version $Id$
     * @goal monitor
     */
    public class MonitorMojo
        extends AbstractMojo
    {
    
        public void execute()
            throws MojoExecutionException, MojoFailureException
        {
            // TODO Auto-generated method stub
    
        }
    
    }
    

Wiring the Website Monitor Component

  1. Declare a field of type org.codehaus.plexus.tutorial.WebsiteMonitor for MonitorMojo.
  2. Add a Javadoc annotation @component on the field. This indicates to Plexus runtime to lookup and populate the field with an implementation with a Component ROLE = 'org.codehaus.plexus.tutorial.WebsiteMonitor'
    /**
     * A Mojo that monitors a given list of websites.
     * 
     * @version $Id$
     * @goal monitor
     */
    public class MonitorMojo
        extends AbstractMojo
    {
    
        /**
         * The website monitor component instance that will be injected 
         * by the Plexus runtime.
         * @component
         */
        private WebsiteMonitor monitor;
    
        public void execute()
            throws MojoExecutionException, MojoFailureException
        {
            // TODO Auto-generated method stub
            
        }
    
    }
    
  3. (optional) We can get choosy if we know there are more than one implementations of org.codehaus.plexus.tutorial.WebsiteMonitor ROLE available. This can done by adding an attribute 'role-hint' to the Javadoc annotation that we added in the previous step.

    Say, for instance we had a different website monitor implementation to monitor FTP websites, the Javadoc annotation for that Mojo field will look like the snippet below.

    Of course, the value of role-hint provided in the code snippet reproduced below should have been defined for the website monitor's implementation's component descriptor that monitors FTP sites.

    Again the Plexus runtime does the magic of injecting the appropriate implementation.

    /**
     * A Mojo that monitors a given list of websites.
     * 
     * @version $Id$
     * @goal monitor
     */
    public class MonitorMojo
        extends AbstractMojo
    {
    
        /**
         * The website monitor component instance that will be injected 
         * by the Plexus runtime.
         * @component role-hint="ftp"
         */
        private WebsiteMonitor monitor;
    
        public void execute()
            throws MojoExecutionException, MojoFailureException
        {
            // TODO Auto-generated method stub
            
        }
    
    }
    

Next: Adding configuration for the Mojo