Jump to content

Running an External Ant Script in a Maven Build

+ 1
  tmo9d's Photo
Posted Oct 01 2009 07:39 AM

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

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.

<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.mhandbook</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


Cover of Maven: The Definitive Guide
Learn more about this topic from Maven: The Definitive Guide. 

Written by Maven creator Jason Van Zyl and his team at Sonatype, Maven: The Definitive Guide clearly explains how this popular tool can bring order to your software development projects. The first part of the book demonstrates Maven's capabilities through the development of several sample applications from ideation to deployment, and the second part offers a complete reference guide. Concise and to the point, this is the only guide you need to manage your project.

Learn More Read Now on Safari


Tags:
1 Subscribe


0 Replies