Thursday, 25. January 2007, 14:06:10
For many years Ant was a de-facto standard building framework. Now Maven2 is going to replace it. I like its idea of build lifecycle, dependency management, but it is lacking documentation and some plugins.
Since Maven 2.0.1 plugins can be written in Ant script. So we can reuse Ant tasks for rapid development of Maven plugins

. How to write a simple Ant based plugin is described on the
Maven's site.
To use an external Ant task just add dependencies on the required JARs to your POM and define <taskdef> in your Ant script.
If you need access to the MavenProject object, e.g. to add generated files to project sources, do the following:
1) Open the *.mojos.xml and add the MavenProject object as a parameter of the Ant plugin (Ant will see it as a reference, the way described on the Maven site didn't work for me

):
<pluginMetadata>
<mojos>
<mojo>
...
<parameters>
<parameter>
<name>mvnproject</name>
<required>true</required>
<defaultValue>${project}</defaultValue>
<type>org.apache.maven.project.MavenProject</type>
<description>Current Maven project</description>
</parameter>
</parameters>
</mojo>
</mojos>
</pluginMetadata>
2) Create a Task which will use the MavenProject (put it into the java folder of your plugin):
package my.org;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Reference;
public class MavenTask extends Task {
private Reference mavenProjRef;
// The method executing the task
public void execute() throws BuildException {
MavenProject proj = (MavenProject) mavenProjRef.getProject().getReference(mavenProjRef.getRefId());
System.out.println("Maven project name is " + proj.getName());
}
public void setMavenProject(Reference ref) {
this.mavenProjRef = ref;
}
}
3) Pass the reference on the MavenProject to your Task in the *.build.xml:
<project>
<target name="hello">
<taskdef name="mvntask" classname="my.org.MavenTask"/>
<mvntask mavenProject="mvnproject"/>
</target>
</project>
4) Add the following dependency to your POM file:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency>
Adding a componentIf you dig into the source code of org.apache.maven.plugin.tools.model.io.xpp3.PluginMetadataXpp3Reader and
org.apache.maven.tools.plugin.generator.PluginDescriptorGenerator you'll find that they do not work as it is described on the Maven's site.
So the only way to configure a requirement for a component is to make a parameter in the following format:
<parameter>
<name>projectHelper</name>
<expression>${component.component_name#component_role}</expression>
</parameter>
Other parameter properties (e.g. property, required, readonly) are ignored by Maven parsers!
Example, this parameter in a *.mojos.xml:
<parameter>
<name>projectHelper</name>
<expression>${component.org.apache.maven.project.MavenProjectHelper}</expression>
</parameter>
will add a requirement to the META-INF\maven\plugin.xml:
<requirements>
<requirement>
<role>org.apache.maven.project.MavenProjectHelper</role>
<field-name>projectHelper</field-name>
</requirement>
</requirements>
In an Ant task this object can be found using:
getProject().getReference("org.apache.maven.project.MavenProjectHelper_null")