Apache Ivy – Manage dependencies for Ant build Java project

Maven is my favourite Java project management tool. But for legacy Java projects which you have to stick to use Apache Ant, you can consider Apache Ivy.

Apache Ivy installation is simple, download the binary, extract the ivy.jar to your ANT_HOME/lib.

To verify the installation, go to the extracted folder and navigate to src/example/hello-ivy. Run the Ant command and see if the build works.

From the above log, you will find that Apache Ivy helps us to resolve the required dependencies needed for the project just like what Maven does. And the dependencies information is stored in the ivy.xml.

<ivy-module version="2.0">
  <info organisation="org.apache" module="hello-ivy"/>
    <dependency org="commons-lang" name="commons-lang" rev="2.0"/>
    <dependency org="commons-cli" name="commons-cli" rev="1.0"/>


Again, like Maven, there is a local repository your machine. In Windows, that is C:\Users\<username>\.ivy2\cache. If they are not available on your local repository, then it will refer to your repository manager if any, or it will download them from the public repositories.

There is also another file for the Apache Ivy settings called ivysettings.xml but it is not necessary. Without your own ivysettings.xml, Ivy will use the default one which is located inside the ivy.jar. But it maybe a better idea to have the ivysettings.xml for each project. Anyway, here are 2 references which may help.


Now Apache Ivy is ready, the next thing is to configure the Ant build.xml such that before each build, we would invoke Ivy to resolve the dependencies. Let’s get the simple Ant project working first.

1. Create the ivy.xml in the project root folder even though they are not needed for our Hello World project.

<ivy-module version="2.0">
  <info organisation="org.apache" module="hello-ivy"/>
    <dependency org="commons-lang" name="commons-lang" rev="2.0"/>
    <dependency org="commons-cli" name="commons-cli" rev="1.0"/>


2. Edit the build.xml like adding the xmlns:ivy, removing the lib directory in target:clean as well as 2 more targets as highlighted below.

<project name="hello-world-ant" basedir="." default="main" xmlns:ivy="antlib:org.apache.ivy.ant">

  <!-- Ant properties -->
  <property name="src.dir"     value="src"/>
  <property name="lib.dir"     value="lib"/>
  <property name="build.dir"   value="build"/>
  <property name="classes.dir" value="${build.dir}/classes"/>
  <property name="jar.dir"     value="${build.dir}/jar"/>
  <property name="main-class"  value="com.eureka.HelloWorld"/>

  <target name="clean">
    <delete dir="${build.dir}"/>
    <delete dir="${lib.dir}"/>

  <target name="resolve">
  <target name="report" depends="resolve">
    <ivy:report todir="${build.dir}"/>

  <target name="compile" depends="report">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>

  <target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <attribute name="Main-Class" value="${main-class}"/>

  <target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>

  <target name="clean-build" depends="clean,jar"/>

  <target name="main" depends="clean,run"/>



3. Here we go!

Done =)

2 thoughts on “Apache Ivy – Manage dependencies for Ant build Java project”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.