Use Apache Ivy to Manage Jar Dependencies

Install Ivy

* Download Ivy from http://ant.apache.org/ivy/download.cgi
* Add to ANT_HOME/lib directory

ivy-2.1.0.jar
jsch-0.1.31.jar # Needed for ssh and sftp resolvers

* For Eclipse, add Ivy jars to Ant runtime classpath:
Window -> Preferences -> Ant -> Runtime -> Classpath -> Ant Home Entries -> Add External Jars

Use Ivy

Create a dependency file named ivy.xml

ivy.xml file defines your project dependencies.

<ivy-module version="1.0">
 <info organisation="my.com" module="myproject" />
  <dependencies>
    <dependency name="junit" rev="3.8.1" />
    <dependency name="commons-lang" rev="2.0"/>
 </dependencies>
</ivy-module>

Create a settings file named ivysettings.xml

ivysettings.xml file defines your repositories.

<ivysettings>
  <settings defaultResolver="chained" />
  <resolvers>
    <chain name="chained">
      <filesystem name="libraries">
        <artifact pattern="C:/repository/[artifact]-[revision].[ext]" />
      </filesystem>
      <ssh keyFile="C:/Documents and Settings/me/.ssh/id_rsa"
        keyFilePassword="">
        <artifact
	    pattern="ssh://user:me@my.com:22/path/to/repository/[organisation]/[module]/[artifact]-[revision].[ext]" />
      </ssh>
      <ibiblio name="ibiblio" m2compatible="true" />
    </chain>
  </resolvers>
</ivysettings>

In Ant build file build.xml

Add Ivy namespace to project

<project name="myproject" basedir="."
	xmlns:ivy="antlib:org.apache.ivy.ant">

Define Ivy settings file

<ivy:settings file="ivysettings.xml" />

Define ivy.lib.dir

This is the directory where retrieved jar files will reside.

<property name="ivy.lib.dir" value="${lib.dir}" />

Define resolve target

<target name="resolve" 
  description="--> resolve and retrieve dependencies with ivy">
    <ivy:retrieve/>
</target>

Use Ivy

<target name="report" depends="resolve" 
  description="--> generates a report of dependencies">
    <ivy:report todir="${build.dir}"/>
</target>
 
<path id="build.classpath">
 <fileset dir="${lib.dir}">
  <include name="**/*.jar"/>
 </fileset>
</path>

Sample build.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project name="myproject" basedir="."
	xmlns:ivy="antlib:org.apache.ivy.ant">
 
    <property name="lib.dir" location="./lib"/>
    <property name="build.dir" location="./build"/>
    <property name="ivy.lib.dir" value="${lib.dir}" />
 
    <ivy:settings file="ivysettings.xml" />
 
    <target name="resolve" 
    	description="--> resolve and retrieve dependencies with ivy">
        <ivy:retrieve/>
    </target>
 
    <target name="report" depends="resolve" 
    	description="--> generates a report of dependencies">
        <ivy:report todir="${build.dir}"/>
    </target>
 
    <path id="build.classpath">
    <fileset dir="${lib.dir}">
    <include name="**/*.jar"/>
    </fileset>
    </path>
 
    <target name="clean.cache">
    	<ivy:cleancache />
    </target>
</project>
This entry was posted in java. Bookmark the permalink.