I currently have ANT_HOME
located at /home/<myuser>/ant/1.8.4/ant-1.8.4
.
I just downloaded the Apache Ivy tarball that includes its dependencies. I extracted it to /home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1
.
I then copied /home/<myuser>/ivy/2.3.0-rc1/ivy-2.3.0-rc1/lib/*.jar
to ANT_HOME/lib
. If my understanding of how Ant works with plugins/extensions is correct, then Ant should now be able to access all of Ivy's tasks at runtime.
My next question is, how do I define Ivy tasks inside my Ant buildfile? Say I want to use ivy-retrieve
, ivy-resolve
and ivy-publish
tasks. What are all the configurations I need to do (in the XML) to get these tasks working when I run my Ant build from the command-line (I will not be building through the Ant-Eclipse plugin). Thanks in advance!
First, you have to define a
<taskdef>
to point to the Ivy tasks.That will give you access to the Ivy tasks. You'd use these tasks like this:
The problem is that your Ivy tasks names might clash with other Ant tasks. For example, there's an Ivy task
<report>
. To solve this, you can create an Ivy namespace. To do that, you put a reference in your namespace in the<project>
entity like this:Now, when you define the Ivy tasks, you can use that
antlib:org.apache.ivy.ant
reference to yourivy
namespace. Same taskdef as before, but with auri
field:By the way, there's nothing special about that
uri
. I could have done this:The point is now you can prefix your task names with
ivy:
. Instead of this:You can now do this:
And that's how you gain access to your Ivy Ant tasks.
Now, you have access to your Ivy Ant tasks, you need to define an
ivysettings.xml
file and use the<ivy:settings/>
task to point there:There is a default
ivysettings.xml
file embedded in Ivy that will point you to the world wide Maven repository system. If you don't have a company wide Maven repository, then you can use the defaultivysettings.xml
file:That's pretty simple.
Once you've done that, you need to read in and resolve your
ivy.xml
file which usually sits in the root of your project in the same directory as yourbuild.xml
file.Basically, your
ivy.xml
file contains references to the third party jars you want to bring into your project. For example:What this is saying is that I need the
log4j.jar
(revision 1.2.17) for compilation (and for compiling tests too) and I needjunit.jar
(revision.4.10) for compilation of my test code.The
compile->default
is a mapping of mycompile
configuration to Maven'sdefault
configuration (which says I just want the Jar and any other jars that it might depend upon.Where's does my
compile
configuration come from? I define it in myivy.xml
. There are ten standard configurations. This also goes into yourivy.xml
file:You can use any configuration name you want, but these map to the default Maven configurations and are widely used.
Once you have your
ivy.xml
file defined, you can use<ivy.resolve>
to resolve your dependencies:So, we have the following:
<taskdef>
in yourbuild.xml
to incorporate the Ivy Ant tasks into your build.<ivy:settings>
to configure Ivy.<ivy:resolve/>
to read in yourivy.xml
file and resolve your third party jar dependencies.Now, you probably want to actually use those jar files. There are three ways to do this:
The
<ivy:cachepath/>
task will create a classpath (in this case called main.classpath) that points to the jars you have in yourivy.xml
file'scompile
configuration. This is used most of the time.If you need a fileset, you can use this:
In this case, it will create a fileset with a refid of
compile.fileset
.Sometimes you have to bring the jars into your project. For example, if you create a war or ear file, you want to enclose your jars. In that case, you can use this:
That will fetch your jars into the
${lib.dir}
directory, so you can include them in wars or ears.Sorry for the long answer, but there are a lot of steps to cover. I highly recommend Manning's book Ant in Action which has a whole chapter on Ivy.