Monday, January 5, 2009

Creating your own maven plugin

Maven plugin:
Project->New->Maven Project->Next then select:


Your Mojo class will look like:
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
* Goal which touches a timestamp file.
*
* @goal ddw
*
* @phase process-sources
*/
public class MyMojo extends AbstractMojo {
/**
* Location of the file.
* expression says which parameter to use
* @parameter expression="${outputDirectory}"

* @required
*/
private File outputDirectory;

public void execute() throws MojoExecutionException {
File f = outputDirectory;

if (!f.exists()) {
f.mkdirs();
}

File touch = new File(f, "touch.txt");

FileWriter w = null;
try {
w = new FileWriter(touch);

w.write("touch.txt");
} catch (IOException e) {
throw new MojoExecutionException("Error creating file " + touch, e);
} finally {
if (w != null) {
try {
w.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
This plugin just creates file. Pay attention that comments are really a part of function.
Then you can run it via command line:
mvn groupId:artifactId:version:goal

In this case go to your project directory
mvn com.ddw.plugin:ddw-plugin:0.0.1-SNAPSHOT:ddw -DoutputDirectory="C:\tmp"

No comments:

Post a Comment