Here's my standard Java build file that creates an executable jar
.
The objective is to manage project specific stuff via a combination of ANT properties and an ivy.xml
file for the 3rd-party dependencies.
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="demo" default="build">
<property name="src.dir" location="src"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="dist"/>
<property name="dist.jar" location="${dist.dir}/${ant.project.name}.jar"/>
<property name="dist.main.class" value="HelloWorld"/>
<target name="retrieve">
<ivy:resolve/>
<ivy:cachepath pathid="build.path" conf="build"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
</target>
<target name="compile" depends="retrieve">
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" classpathref="build.path"/>
</target>
<target name="build" depends="compile">
<ivy:retrieve pattern="${dist.dir}/lib/[artifact].[ext]"/>
<manifestclasspath property="jar.classpath" jarfile="${dist.jar}">
<classpath>
<fileset dir="${dist.dir}/lib" includes="*.jar"/>
</classpath>
</manifestclasspath>
<jar destfile="${dist.jar}" basedir="${build.dir}/classes">
<manifest>
<attribute name="Main-Class" value="${dist.main.class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</jar>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
</project>
As you've discovered in the Ivy docu, the cachepath
Ivy task is used to manage two ANT paths. One for the build dependencies the other for the run-time dependencies of the executable jar
.
The real power of Ivy is in something called configurations
. I found it difficult to grasp initially until I realised it was simple a logical grouping of jar
s that I can define for my project. This example has two configurations
:
Here's the ivy file demonstrating how dependencies can be associated with configurations
:
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="build" description="Libraries needed to for compilation"/>
<conf name="runtime" extends="build" description="Libraries that need to be included with project jar" />
</configurations>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.0" conf="build->default"/>
<dependency org="commons-cli" name="commons-cli" rev="1.0" conf="runtime->default"/>
</dependencies>
</ivy-module>
In conclusion I hope this example helps in understanding Ivy. I like the way it concentrates on only one thing, the management of 3rd-party dependencies.