Skip to content

Integrating Apache Ivy with Netbeans

Axel Howind edited this page Mar 29, 2015 · 4 revisions

When uploading my first project to Github, I had to decide what to do with third party libraries. For me, it is sometimes frustrating trying to hunt down all the required libraries just get started with something. So, this was the time to get started with automatic dependency management.

Evaluating both Maven and Ivy, I settled for Ivy for the following reasons:

  • Working under Netbeans, ant is the default tool, and I already had the ant scripts in place
  • Reading several tutorials, Ivy seemed easier to set up and integrate with Netbeans
  • It turned out I can use Ivy without installing any Netbeans plugins at all by just editing some files in my project.

Note: This post describes setting up Ivy for Netbeans projects.

Now, here's what to do:

Create an ivy.xml file

The file ivy.xml contains the dependencies for your project. Search for the libraries you need at the Maven Central Repository and enter the data into your ivy.xml file.

  • For each library, create a <dependency> tag.
  • For the GroupId displayed in the Maven repository, create an attribute org.
  • For the ArtifactId, create an attribute name.
  • The attribute rev is for the required version of the library.

Now ivy.xml should look something like this:

<ivy-module version="2.0">
    <info organisation="com.dua3" module="meja"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.apache.poi" name="poi-excelant" rev="3.11" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.apache.poi" name="poi-excelant" rev="3.11" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="org.apache.poi" name="poi-excelant" rev="3.11" conf="test->default"/>
    </dependencies>
</ivy-module>

Edit your build.xml file

  • Add the Ivy Namespace by changing this:

<project name="meja" default="default" basedir=".">

into

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

  • Tell Ant to automatically fetch Ivy if it is not installed by adding this to your build.xml:
    <!-- Download ivy -->
    <property name="ivy.install.version" value="2.1.0-rc2" />
    <condition property="ivy.home" value="${env.IVY_HOME}">
      <isset property="env.IVY_HOME" />
    </condition>
    <property name="ivy.home" value="${user.home}/.ant" />
    <property name="ivy.jar.dir" value="${ivy.home}/lib" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
    <target name="download-ivy" unless="offline">
        <mkdir dir="${ivy.jar.dir}"/>
        <!-- download Ivy from web site so that it can be used even without any special installation -->
        <get src="https://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="init-ivy" depends="download-ivy">
      <!-- try to load ivy here from ivy home, in case the user has not already dropped
              it into ant's lib dir (note that the latter copy will always take precedence).
              We will not fail as long as local lib dir exists (it may be empty) and
              ivy is in at least one of ant's lib dir or the local lib dir. -->
        <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>    
  • Make sure the init-ivy task is executed during build

<target name="-pre-init" depends="init-ivy"/>

  • Overwrite the pre-compile task in your build.xml to fetch required libraries by adding this (the original task definition is in another file which is autogenerated by Netbeans):
    <!-- Automatically retrieve dependencies using ivy -->
    <target name="-pre-compile">
        <ivy:retrieve />
    </target>
Clone this wiki locally