Skip to content. | Skip to navigation

You are here: Home Projects Software Engineering Java Webapp Writing a Maven Plugin
Personal tools

Writing a Maven Plugin

A step beyond the basics of writing a custom Maven2 plugin.

Getting the Basics Sorted

Firstly when looking to write your own Maven Plugins I would recommend reading the following:

Maven: The Definitive Guide

http://www.sonatype.com/book/

Your First Plugin

http://maven.apache.org/guides/plugin/guide-java-plugin-development.html

Mojo API Specification

http://maven.apache.org/developers/mojo-api-specification.html

Mojo Developer Cookbook

http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook

MyAbstractMojo

The abstract base class with some useful properties.

public abstract class MyAbstractMojo extends AbstractMojo {
    
    /**
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    private ArtifactRepository localRepository;

    /**
     * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
     * @required
     * @readonly
     */
    private ArtifactResolver artifactResolver;
    
    /** 
     * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
     * @required
     * @readonly
     */
    private ArtifactFactory artifactFactory;

    /**
     * @component role="org.apache.maven.project.MavenProjectBuilder"
     * @required
     */
    private MavenProjectBuilder mavenProjectBuilder;

    /**
     * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
     * @required
     */
    private ArtifactMetadataSource artifactMetadataSource;

    /**
     * @parameter expression="${project.remoteArtifactRepositories}"
     * @required
     * @readonly
     */
    private List remoteArtifactRepositories;

    /**
     * @parameter expression="${basedir}/src/main"
     * @required
     */
    private File sourceDirectory;

    /**
     * @parameter expression="${project.build.directory}"
     * @readonly
     */
    private File outputDirectory;

    /**
     * @parameter expression="${project.build.finalName}"
     * @readonly
     */
    private String finalName;

    /**
     * @parameter expression="${project}"
     * @readonly
     * @required
     */
    private MavenProject mavenProject;

    protected File getSourceDirectory() {
        return sourceDirectory;
    }

    protected File getOutputDirectory() {
        return outputDirectory;
    }

    protected String getFinalName() {
        return finalName;
    }

    protected MavenProject getMavenProject() {
        return mavenProject;
    }

    protected ArtifactResolver getArtifactResolver() {
        return artifactResolver;
    }

    protected List getRemoteArtifactRepositories() {
        return remoteArtifactRepositories;
    }

    protected ArtifactRepository getLocalRepository() {
        return localRepository;
    }

    protected ArtifactFactory getArtifactFactory() {
        return artifactFactory;
    } 
    
    protected ArtifactMetadataSource getArtifactMetadataSource() {
        return artifactMetadataSource;
    }
  
    protected MavenProjectBuilder getMavenProjectBuilder() {
        return mavenProjectBuilder;
    }
}

Useful Functions

Resolving the dependcies for a given Maven Project can be very helpful. The following functions seem to work quite well so far in conjunction with the above abstract base class.

    private Set getDependencyArtifacts(MavenProject aProject) throws MojoExecutionException {
        
        try {
            Set artifacts = aProject.createArtifacts( getArtifactFactory(), null, null);
            ArtifactResolutionResult arr = getArtifactResolver().resolveTransitively(artifacts, aProject.getArtifact(), getLocalRepository(), getRemoteArtifactRepositories(), getArtifactMetadataSource(), null);
            Set result = arr.getArtifacts();
            aProject.setArtifacts(result);
            
            getLog().info("artifacts for project(" + aProject + ") are:" + artifacts);
            
            Set results = new HashSet();

            // Now check each artifact for its own dependencies
            for (Iterator artifactIterator = artifacts.iterator(); artifactIterator.hasNext();) {
                Artifact artifact = (Artifact) artifactIterator.next();
                getLog().info(artifact.getArtifactId() + " " + artifact.getGroupId());

                results.add(artifact);
                    
                results.addAll( getDependencyArtifacts(artifact) );
            }
            return results;
        } catch (Exception ex) {
            throw new MojoExecutionException("Failed to get dependencies", ex);
        }
        
    }
    
    private Set getDependencyArtifacts(Artifact artifact) throws MojoExecutionException {
        
        try {
            // Make sure the artifact is resolved before we do anything with it
            getArtifactResolver().resolve(artifact, getRemoteArtifactRepositories(), getLocalRepository());
            
            MavenProject subProject = getMavenProjectBuilder().buildFromRepository(artifact, getRemoteArtifactRepositories(), getLocalRepository());
            
            // Now we have our subProject we need to resolve its stuff
            getLog().info("subProject=" + subProject);
            
            Set artifacts = subProject.createArtifacts( getArtifactFactory(), null, null);
            ArtifactResolutionResult arr = getArtifactResolver().resolveTransitively(artifacts, artifact, getLocalRepository(), getRemoteArtifactRepositories(), getArtifactMetadataSource(), null);
            Set result = arr.getArtifacts();
            subProject.setArtifacts(result);
            
            return getDependencyArtifacts(subProject);
        } catch (Exception ex) {
            throw new MojoExecutionException("Failed to get dependencies", ex);
        }
    }

 

Document Actions