Monday, October 24, 2005

Creating a dotnet plugin for Maven

This blog charts my quest to create a dotnet plugin for maven 2 which can be used to compile/distribute/unit-test and build-ndoc-docco for dotnet projects.

I'm using maven 2 (the first formal release)

Creating the plugin from scratch

The command I used to create the plugin was:


C:\SVN\...\..\trunk>mvn archetype:create -DgroupId=engn -DartifactId=engn-maven-plugin-dotnet -DarchetypeArtifactId=maven-archetype-mojo \n
-Duser.home=C:\Work\SCM\CVS\CPDS\trunk\m2.user.home -DarchetypeVersion=1.0-alpha-2


Phew that was a bit of a mouthful. I initially struggled with this as maven started throwing an exception about searching for the RELEASE version of the maven-archetype-mojo archetype.
After some digging I found the only version of this archetype currently published was 1.0 alpha-2 so appended this to command line -DarchetypeVersion=1.0-alpha-2 and everything was hunky dory.

I then used mvn eclipse:eclipse to get my maven eclipse project in order and was ready to start developing......

The First Plugin

To get up to speed with the development cycle of Maven plugins I created a trivial sample which was:


package uk.co.engn.maven.plugin.test;

import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
* Goal which touches a timestamp file.
*
* @goal testmymojo
*
* @phase process-sources
*/
public class TestMojo
extends AbstractMojo
{
/**
* Location of the file.
* @parameter expression="${project.build.directory}"
* @required
*/
private File outputDirectory;


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

public void execute()
throws MojoExecutionException
{
this.getLog().info("Beginning Test Plugin....");

this.getLog().info("Inspecting project....");

List dependencies = project.getDependencies();

Iterator iter = dependencies.iterator();

while(iter.hasNext()){
Dependency d = (Dependency) iter.next();
this.getLog().info("Dependency:" + d.getArtifactId() + " " + d.getGroupId() + " v=" + d.getVersion() + "");
}

this.getLog().info("Done.");
}
}


All this does really is write out the names of the dependencies in my pom.

To be able to use this plugin I needed to install it in my local repository like so:


c:\project-dir>mvn install -Duser.home=C:\Work\SCM\CVS\CPDS\trunk\m2.user.home -e


I'm specifying my user.home on the command line as I've always had problems with maven picking up the settings for the proxy server I am behind. This way forces Maven to pick up my settings.xml.

If this succeeds then you should be able to get hold of the plugin in this manner:


c:\sample-project-dir-using-plugin>mvn engn:engn-maven-plugin-dotnet:testmymojo


And there we have it, the (important) output was:


[INFO] [engndotnet:testmymojo]
[INFO] Beginning Test Plugin....
[INFO] Inspecting project....
[INFO] Dependency:junit junit v=3.8.1
[INFO] Dependency:maven-project org.apache.maven v=2.0-alpha-3
[INFO] Dependency:maven-plugin-api org.apache.maven v=2.0-alpha-3
[INFO] Done.


My next entry will be on trying to do something useful with my plugin.