Thursday, November 10, 2011

Ivy in Ant

You have to do 3 things.

One, create an xmlns:ivy name space in your build.xml
Two, create a ivy:retrieve target
Three, create a file ivy.xml and define your dependencies

See this HelloWorld build.xml updated to use Ivy - note that we have made the 'init' target dependent on the 'resolve' target:

 <project name="HelloWorld" xmlns:ivy="antlib:org.apache.ivy.ant" >  
     
   <target name="resolve" description="--> retrieve dependencies with ivy">  
     <ivy:retrieve />  
   </target>  
   
      <target name="init" depends="resolve">  
           <mkdir dir="classes"/>  
           <mkdir dir="target"/>  
      </target>  
   
      <target name="compile" depends="init">  
           <javac srcdir="." destdir="classes">  
                <classpath>  
                 <pathelement location="lib/log4j-1.2.16.jar"/>  
                </classpath>  
           </javac>  
      </target>  
   
      <target name="dist" depends="compile">  
           <jar jarfile="target/HelloWorld.jar" basedir="classes">  
                <manifest>  
                     <attribute name="Main-Class" value="HelloWorld"/>  
                     <attribute name="Class-Path" value="HelloWorld.jar ../lib/log4j-1.2.16.jar"/>  
                </manifest>  
           </jar>  
      </target>  
 </project>  



Here is the ivy.xml to download the log4j.jar - notice how the log4j co-ordinates are specified using the org, name and rev attributes.

 <ivy-module version="2.0">  
   <info organisation="org.confucius" module="helloworld"/>  
   <dependencies>  
     <dependency org="log4j" name="log4j" rev="1.2.16"/>  
   </dependencies>  
 </ivy-module>  


Now if you run Ant, it will call Ivy to do the log4j download (see below)

 C:\users\LavanniM\confucius>ant dist  
 Buildfile: C:\users\LavanniM\confucius\build.xml  
   
 resolve:  
 [ivy:retrieve] :: Ivy 2.2.0 - 20100923230623 :: http://ant.apache.org/ivy/ ::  
 [ivy:retrieve] :: loading settings :: file = C:\users\LavanniM\confucius\ivysettings.xml  
 [ivy:retrieve] :: resolving dependencies :: org.confucius#helloworld;working@LavanniM-01  
 [ivy:retrieve] confs: [default]  
 [ivy:retrieve] found log4j#log4j;1.2.16 in default  
 [ivy:retrieve] :: resolution report :: resolve 93ms :: artifacts dl 0ms  
     ---------------------------------------------------------------------  
     |         |      modules      ||  artifacts  |  
     |    conf    | number| search|dwnlded|evicted|| number|dwnlded|  
     ---------------------------------------------------------------------  
     |   default   |  1  |  0  |  0  |  0  ||  1  |  0  |  
     ---------------------------------------------------------------------  
 [ivy:retrieve] :: retrieving :: org.confucius#helloworld  
 [ivy:retrieve] confs: [default]  
 [ivy:retrieve] 1 artifacts copied, 0 already retrieved (470kB/15ms)  
   
 init:  
   
 compile:  
   [javac] C:\users\LavanniM\confucius\build.xml:13: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds  
   
 dist:  
   
 BUILD SUCCESSFUL  
 Total time: 0 seconds  
   
 C:\users\LavanniM\confucius>  

No comments: