The Mvn Task

Note: This task is available since version 2.0.10 of the Maven Ant Tasks

The Maven Ant Tasks have some limited support for calling a full Maven build from Ant. The mvn task is a subclass of the Ant java task and supports all of its options such as args, fork, resultproperty, etc.

If Maven is already installed on the local system, the Maven build can be called using this local installation by specifying the mavenHome parameter.

  <artifact:mvn mavenHome="/path/to/maven-2.0.x">
    <arg value="install"/>
  </artifact:mvn>

Maven will search for a pom.xml file in the current directory and run the install goal.

If the pom file is not located in the current directory, an alternate path to the pom can be specified.

  <artifact:mvn pom="path/to/my-pom.xml" mavenHome="/path/to/maven-2.0.x">
    <arg value="install"/>
  </artifact:mvn>

Running the Mvn Task without a Maven Installation

If no local Maven installation is available, the mvn task will attempt to resolve (download) the necessary jar files from the central Maven repository and run the Maven build using these jar files.

When the mavenHome attribute is not set, the mvn task will attempt to automatically resolve the required jar files.

  <artifact:mvn pom="path/to/my-pom.xml">
    <arg value="install"/>
  </artifact:mvn>

Note: this will use version 2.2.1 of the core Maven libraries, contained in the Maven Ant Tasks jar

Using the Java Task

The java task can be used directly without any need for the Maven Ant Tasks. However, this method requires that Maven is already installed on the system. A property called maven.home must be set to point to the local Maven installation, then an Ant macro can be defined for calling Maven.

  <macrodef name="maven">
    <attribute name="options" default="" />
    <attribute name="goal" />
    <attribute name="basedir" />
    <attribute name="resultproperty" default="maven.result" />
    <element name="args" implicit="true" optional="true" />
    <sequential>
      <java classname="org.codehaus.classworlds.Launcher" fork="true"
            dir="@{basedir}" resultproperty="@{resultproperty}">
        <jvmarg value="-Xmx512m"/>
        <classpath>
          <fileset dir="${maven.home}/boot">
            <include name="*.jar" />
          </fileset>
          <fileset dir="${maven.home}/lib">
            <include name="*.jar" />
          </fileset>
        </classpath>
        <sysproperty key="classworlds.conf" value="${maven.home}/bin/m2.conf" />
        <sysproperty key="maven.home" value="${maven.home}" />
        <arg line="--batch-mode @{options} @{goal}" />
      </java>
    </sequential>
  </macrodef>

This example defines an Ant macro called maven. The macro can then be used in the build like this:

      <maven basedir="${basedir}"
             options="${maven.opts}"
             goal="install"
             resultproperty="maven.build.result"/>