Integrating Ant

Apache Ant is one of the most important automation build tools. It is based on XML, it has been written in Java, and the concept is similar to Make.

Despite Maven and Ant being complementary, with the introduction of Maven, Ant has lost its importance and popularity; Ant was developed to compile, test, and package Java applications (or other applications), and Maven shares the same purpose. Ant provides a wide range of plugins such as Maven. To conclude, most Maven plugins are now outperforming Ant. Ant, however, is precious when we need to orchestrate some Maven executions or modulate Maven parameters.

Installing Ant

Ant is a command-line tool. To install Ant, you can download the binary from http://ant.apache.org/bindownload.cgi. Unzip the distribution file into a folder.

Set the JAVA_HOME environmental variable in your Java environment, set Ant_HOME to the directory you uncompressed Ant to, and add ${Ant_HOME}/bin (Unix) or %Ant_HOME%in (Windows) to your PATH.

Run the following command for help to be displayed:

$ <Ant_HOME>/bin/ant --help 

Understanding Ant

Ant uses a simple XML file called build.xml located in the running directory. The build.xml file must contain a set of tasks called target. The following code defines a global property called dist and a simple task creating the dist directory:

<project name="MyProject" default="dist-task" basedir=".">

  <!-- set global properties for this build -->
  <property name="dist-task"  location="dist"/>

  <target name="dist-task">
<!-- Create the dist directory -->
    <mkdir dir="${dist}"/>
  </target>
</project>

You can execute the build.xml file by running the following command:

$ <Ant_HOME>/bin/ant

You can also use the following command explicitly:

$ <Ant_HOME>/bin/ant –f build.xml dist-task

Ant custom tasks

Like Maven, Ant provides a set of APIs to create a custom plugin called task.

A list of the official Ant tasks can be found at http://ant.apache.org/manual/tasksoverview.html.

Through these tasks, we can package (WAR, JAR, and EAR), zip, compile, make a Javadoc, work with a filesystem, SSH and SFTP, work with CVS, read a properties file, and so on.

Other unofficial tasks have been developed by the community; the following points show the most popular Ant tasks contributed by the community:

  • if, then, else: These are used to control Ant tasks
  • Svnant: This is used to operate with Subversion
  • maven-ant: This is used to integrate Maven with Ant

Maven-Ant integration

If you plan to integrate Maven and Ant, you probably need the Maven-ant task.

Download the plugin from https://maven.apache.org/ant-tasks/download.html and copy the JAR file into the <Ant_HOME>/lib directory.

The next step is to customize the build.xml file to use the plugin. The following code snippet is a minimal Ant project:

<project name="MyProject" 
   default=" maven-task " 
      basedir="."
      xmlns:artifact="antlib:org.apache.maven.artifact.ant">
  <target name="maven-task">
  <artifact:mvn pom="path/to/my-pom.xml" 
       mavenHome="/path/to/maven-3.0.x">
        <arg value="install"/>
    </artifact:mvn>
  </target>
</project>

The namespace definition of the project tag declares the plugin to Ant. The Maven task, artifact:mvn, executes the install goal of the my-pom.xml file. The mavenHome attribute is optional, but it is good practice to provide the Maven installation directory.

The maven-ant plugin supports other tasks. The following code snippet reads the my-pom.xml file and prints the POM version number.

<project name="MyProject" 
   default=" maven-task " 
      basedir="."
     xmlns:artifact="antlib:org.apache.maven.artifact.ant">

  <target name="maven-task">
     <artifact:pom id="mypom" file=" path/to/my-pom.xml" />
     <echo>The version is ${mypom.version}</echo>
  </target>
</project>.

Ant-Maven integration

If your organization has an old library of Ant procedures that are not easy to refactor, you can use the Maven AntRun Plugin to call Ant from Maven. The following code snippet calls the my-task task during the installation phase:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>install</phase>
            <configuration>
              <target>my-task</target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Tip

Despite this plugin appearing very useful, it is strongly encouraged to use the Maven plugin instead of a custom Ant task. It is good practice to prepare the Maven environment through Ant, without interfering with Maven.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset