The Maven Cookbook

4.3. Running an External Ant Script in a Maven Build

4.3. Running an External Ant Script in a Maven Build

4.3.1. Task

You need to execute an external Ant script in a Maven build.

4.3.2. Action

Configure the run goal form the Maven AntRun plugin. Define any properties you wish to pass to the external Ant build, and then call the external Ant build with the ant task, specifying the antfile and target you wish to execute.

Example 4.2. Executing an External Ant Script from a Maven Build

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sonatype.mcookbook</groupId>
  <artifactId>ant-script-ex</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ant-script-ex</name>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>ant-magic</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <property name="compile_classpath" 
                          refid="maven.compile.classpath"/>
                <property name="outputDir"
                          value="${project.build.outputDirectory}"/>
                <property name="sourceDir"
                          value="${project.build.sourceDirectory}"/>
                <ant antfile="${basedir}/src/main/ant/create-deps.xml"
                     target="create"/>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

To execute this external Ant build, run mvn package.

~/examples/ant/ant-script Tim$ mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building ant-script-ex
...
[INFO] [antrun:run {execution: ant-magic}]
[INFO] Executing tasks

create:
     [copy] Copying 1 file to ~/examples/ant/ant-script-ex/target/classes
[INFO] Executed tasks
...
[INFO] BUILD SUCCESSFUL

Once this simple Ant script has been completed, the contents of ${basedir}/target/classes/deps.txt should contain the following:

compile classpath: ~/examples/ant/ant-script-ex/target/classes