The Maven Cookbook

4.2. Running an Inline Ant Script in a Maven Build

4.2. Running an Inline Ant Script in a Maven Build

4.2.1. Task

You need to run an inline Ant script as a part of your Maven build.

4.2.2. Action

Configure the run goal of the Maven AntRun plugin to execute an inline Ant script. The POM shown in configures the run goal to execute during the prepare-package phase of the build. The Ant script is contained in the tasks configuration parameter for the ant-magic execution.

Example 4.1. Executing an Inline Ant Script in 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</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ant-script</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"/>
                <echo file="${project.build.outputDirectory}/deps.txt"
                      message="compile classpath: ${compile_classpath}"/>

                <copy todir="${project.build.outputDirectory}">
                  <fileset dir="${project.build.sourceDirectory}"/>
                </copy>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

You can run this script, by executing the package phase.

~/examples/ant/ant-script $ mvn package
[INFO] Scanning for projects...
...
[INFO] [antrun:run {execution: ant-magic}]
[INFO] Executing tasks
     [copy] Copying 1 file to ~/examples/ant/ant-script/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/target/classes