Like most of us often hear, “its another framework or dependency management tool”. But what exactly differentiates Ivy from Maven?
Ivy is much easier to use. The library is simpler since it doesn’t do so much. It only focuses on dependency management and use the already famous and powerful ant to build the application. In short, ant and ivy is used side by side.
Why I Choose Ivy?
The main thing I like about Ivy is its simplicity. You can use Ivy with only ant installed on your system (and of course, java). This means that you don’t need to download any Ivy related libraries or when using eclipse, no plug-in is needed. You can run everything using ant, from installing the ivy library to downloading the dependencies.
Ivy is a dependency management tool that can use other repository such as maven repository.
Installing Ivy
Before continuing, I’ll assume that you are using ant version 1.7. You can choose to run the ant script in either console or from inside eclipse (It depends on your preference).
Here is a build.xml file that contains several targets that will be used in this post:
<?xml version="1.0" encoding="UTF-8"?> <project name="ivytest" basedir="." default="usage" xmlns:ivy="antlib:org.apache.ivy.ant"> <property name="lib.dir" value="lib" /> <property name="build.dir" value="build" /> <property name="src.dir" value="src" /> <path id="lib.path.id"> <fileset dir="${lib.dir}" /> </path> <path id="run.path.id"> <path refid="lib.path.id" /> <path location="${build.dir}" /> </path> <property name="ivy.install.version" value="2.0.0-beta1" /> <property name="ivy.jar.dir" value="${basedir}/ivy" /> <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" /> <target name="usage" description="Displays the command used with this build script"> <echo message="Usage" /> <echo message="-------------------------------------------" /> <echo message="Available targets are:" /> <echo message="download-ivy --> Download ivy library" /> <echo message="install-ivy --> Install ivy library" /> <echo message="clean-libs --> Delete lib files" /> <echo message="clean-ivy --> Delete the ivy files" /> <echo message="resolve --> Resolve/Download ivy dependencies" /> </target> <target name="download-ivy" unless="skip.download" description="Download ivy.jar"> <mkdir dir="${ivy.jar.dir}"/> <echo message="installing ivy..."/> <get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/ ${ivy.install.version}/ivy-${ivy.install.version}.jar" dest="${ivy.jar.file}" usetimestamp="true"/> </target> <target name="install-ivy" depends="download-ivy" description="Install ivy library"> <path id="ivy.lib.path"> <fileset dir="${ivy.jar.dir}" includes="*.jar"/> </path> <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/> </target> <target name="clean-libs" description="Delete all libraries/dependencies"> <echo message="Deleting ${lib.dir}" /> <delete dir="${lib.dir}" /> </target> <target name="clean-ivy" depends="clean-libs" description="Delete the all folders/files that was generated by ivy"> <echo message="Deleting ${ivy.jar.dir}" /> <delete dir="${ivy.jar.dir}" /> </target> <target name="resolve" depends="install-ivy" description="Resolve the dependencies"> <ivy:retrieve/> </target> <target name="run" depends="resolve" description="Compile and run the project"> <mkdir dir="${build.dir}" /> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.path.id" /> <java classpathref="run.path.id" classname="Main"/> </target> </project>
Invoke “ant install-ivy” and the ivy jars will be downloaded. A new folder named “ivy” will be created with the ivy.jar in it.
Dowloading Dependencies
By default ivy uses maven 2 repository but you can certainly define other repository (It will be covered on later post).
The build.xml above contains a target named “resolve”. Running the target will cause the Ivy to download defined dependencies on ivy.xml (ivy.xml is needed since all the dependencies are defined on that file). Here is an example of ivy.xml:
<?xml version="1.0" encoding="UTF-8"?> <ivy-module version="2.0"> <info organisation="ideyatech" module="ivytest"/> <dependencies> <dependency org="log4j" name="log4j" rev="1.2.8"/> </dependencies> </ivy-module>
The code above downloads the log4j and its dependencies from the defined repository (in our case, maven repo). I chose to use log4j because in the next section, I’ll be demonstrating to compile a simple java class that uses a Ivy managed library. Invoke “ant resolve” now and you will see a message saying that the library is being downloaded.
Compiling The Application
Main.java:
import org.apache.log4j.Logger; public class Main { private static final Logger log = Logger.getLogger(Main.class); public static void main(String[] args) { System.out.println("Printed using system.out.println"); log.info("Printed using log4j logger"); } }
The sample java file won’t really print the message being logged. The purpose of this example is to test compiling the application with the Ivy libraries being referenced. The idea is to simply add the “lib” dir to the classpath on compilation.
Try invoking “ant run” and the application will be compiled and executed. Note that invoking the “ant clean-libs” and removing the “resolve” dependency on run target will cause the compilation to throw some error.
References:
http://ant.apache.org/ivy/index.html
Note:
- Ivy logo is property of Apache Ivy and is not connected with Ideyatech.
The post Apache Ivy : An Agile Dependency Manager appeared first on Ideyatech - Java Development Outsourcing Philippines.