FaqIvy

Can I use Ivy to download libraries for my project from a Maven repository?

(First of all there is an Ivy module for NetBeans which may be all you need. Or another option)

Yes, it is possible, though you have to edit some configuration files to set it up (the IDE's GUI does not support it directly). Try this as an example:

1. Download Ivy (at least 1.3 RC2, not 1.2 which has a bug affecting NetBeans):

http://www.jaya.free.fr/ivy/download.html

2. Create a new Java application project.

3. Create a file ivy.xml in the top directory of your project:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="1.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation=
                   "http://www.jayasoft.org/misc/ivy/samples/ivy.xsd">
    <info organisation="myself" module="ivytest"/>
    <dependencies>
        <dependency org="apache" name="dom4j" rev="1.6"/>
    </dependencies>
</ivy-module>

Note that you can verify the syntax using Alt-Shift-F9.

4. Add to your build.xml some custom logic:

    <target name="-check-for-ivy">
        <available property="have.ivy" resource="fr/jayasoft/ivy/ant/antlib.xml"/>
    </target>
    <target name="-ivy-define" depends="-check-for-ivy" unless="have.ivy">
        <taskdef resource="fr/jayasoft/ivy/ant/antlib.xml" uri="antlib:fr.jayasoft.ivy.ant">
            <classpath>
                <fileset dir="${ivy.home}">
                    <include name="ivy*.jar"/>
                    <include name="lib/*.jar"/>
                </fileset>
            </classpath>
        </taskdef>
    </target>
    <target name="-ivy-retrieve" depends="-ivy-define" xmlns:ivy="antlib:fr.jayasoft.ivy.ant">
        <ivy:resolve/> <!-- Tell Ivy to resolve dependencies -->
        <ivy:retrieve/> <!-- Load dependencies to the project -->
        <pathconvert property="ivy.classpath.computed" dirsep="/" pathsep=":">
            <path>
                <fileset dir="lib" includes="*.jar"/>
            </path>
            <map from="${basedir}${file.separator}" to=""/>
        </pathconvert>
        <propertyfile file="nbproject/project.properties">
            <entry operation="=" key="ivy.classpath" value="${ivy.classpath.computed}"/>
        </propertyfile>
    </target>
    <target name="-pre-compile" depends="-ivy-retrieve"/>
    <target name="-pre-compile-single" depends="-ivy-retrieve"/>
    <target name="-post-clean">
        <delete dir="lib"/>
    </target>

Watch out! In case of NB 6.1 (and probably later). The project.properties is read earlier, the modifications of by -ivy-retrieve will not take effect. You will need to run the build twice to get the right classpath or plugin the -ivy-retrieve target in an earlier step.

5. Edit nbproject/private/private.properties to say where Ivy is installed, e.g.

ivy.home=/opt/ivy-1.3-RC2

(If someone checks out your project from CVS they will need to either make a matching definition in their private.properties or include Ivy and its libraries in the Ant classpath.)

(An alternative is that you can add the above line to the .netbeans/5.5/build.properties to access it from all of the NetBeans projects.)

6. Edit nbproject/project.properties and change one definition:

javac.classpath=${ivy.classpath}

7. Try using the new libraries, e.g.

package ivytest;
import org.dom4j.tree.DefaultElement;
public class Main {
    public static void main(String[] args) {
        System.out.println(new DefaultElement("hello").asXML());
    }
}

8. If you click Run Main Project you should see something like

init:
deps-jar:
no configuration file found, using default...
:: configuring :: url = jar:file:/usr/share/ant-core/lib/ant-ivy.jar!/fr/jayasoft/ivy/conf/ivyconf.xml
:: resolving dependencies :: [Ivytest|Working@Starbase1]
        confs: [Default]
        found [Dom4j|1.6] in public
downloading http://www.ibiblio.org/maven/dom4j/jars/dom4j-1.6.jar ...
.........................................
...........................................
..........................................
..........................................
...........................................
..... (306kB)
.. (0kB)
        [[SUCCESSFUL  | SUCCESSFUL ]] [[Dom4j|1.6 |  apache ]]/dom4j.jar[Jar] (11196ms)
:: resolution report ::
        ---------------------------------------------------------------------
        |                  |            modules            ||   artifacts   |
        |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
        ---------------------------------------------------------------------
        |      default     |   1   |   1   |   0   |   0   ||   1   |   1   |
        ---------------------------------------------------------------------
:: retrieving :: [Ivytest]
        confs: [Default]
        1 artifacts copied, 0 already retrieved
Updating property file: /home/jules/NetBeans55Projects/ivyTestingApplication/nbproject/project.properties
Compiling 1 source file to /home/jules/NetBeans55Projects/ivyTestingApplication/build/classes
compile:
run:
BUILD SUCCESSFUL (total time: 15 seconds)

9. Now note that if you just edit ivy.xml and do a clean build, your classpath will be updated for you automatically, including IDE code completion! You can still add classpath entries from the Properties dialog (e.g. subprojects or libraries from Library Manager) but these are separate from the Ivy dependencies. Example: add

<dependency org="apache" name="commons-collections" rev="3.1"/>

and add to your source code e.g.

System.out.println(new org.apache.commons.collections.ArrayStack());

Try to Run again and you should see output like

<hello/>
[]

That was easy!

Further documentation on ivy can be found at

http://www.jaya.free.fr/ivy/doc.html

IvyIntegrationNotes has more discussion.


Applies to: NetBeans 5.0, 5.5.1


Update for Netbeans 7.1.2

JavaEE

  • UPDATE - try setting the following property in your build.xml file and it will download the dependencies to web/WEB-INF/lib
    <property name="ivy.lib.dir" value="web/WEB-INF/lib/"/>
  • I don't think this is the best solution but as a work-around it will work What's written below is the original text.
  • The below doesn't seem to work for Java EE projects due to the following reasons. I posted to the netbeans forum hoping someone with more knowledge can provide an answer. See thread at [1](Ivy, JavaEE and Netbeans).
    • To deploy to Glassfish (or any web app server I guess), NetBeans needs to copy third party libs (jars) to build/web/WEB-INF/lib/ so that Glassfish can see them

For this to happen ....

    • The jars need to be in the <project>/lib dir (ivy will do this)
    • AND The project.xml needs to show the same lib information. This file is ordinarily updated when the project properties are modified and saved by the user.
    • The actual copying is done through the build-impl.xml and this gets updated when the project.xml is modified and saved.

You can still use Ivy to organise your dependencies (in ivy.xml file) and to pull them down from a public repository, but you'll need to add them as Libraries in the project. Saves a bit of effort (like you don't need to manually download the .jar and create a global library out of it).

JavaSE

So for JavaSE projects follow this.

  • Follow the above but:

1. Ivy is now an Apache project and its at version 2 so the tasks are different (see below)

http://ant.apache.org/ivy

Save it to ~/.ant/lib (there are a number of lib dirs for Ant but this seems to be the only one that works).

3. See step 7 below.

4. build.xml is slightly different:

    <target name="-ivy-retrieve">
        <ivy:retrieve/> <!-- Load dependencies to the project -->
        <pathconvert property="ivy.classpath.computed" dirsep="/" pathsep=":">
            <path>
                <fileset dir="lib" includes="*.jar"/>
            </path>
            <map from="${basedir}${file.separator}" to=""/>
        </pathconvert>
        <propertyfile file="nbproject/project.properties">
            <entry operation="=" key="ivy.classpath" value="${ivy.classpath.computed}"/>
        </propertyfile>
    </target>
    <target name="-pre-compile" depends="-ivy-retrieve"/>
    <target name="-pre-compile-single" depends="-ivy-retrieve"/>
    <target name="-post-clean">
        <delete dir="lib"/>
    </target>

4.1. The following targets are no longer needed:

    <target name="-check-for-ivy">
        <available property="have.ivy" resource="fr/jayasoft/ivy/ant/antlib.xml"/>
    </target>
    <target name="-ivy-define" depends="-check-for-ivy" unless="have.ivy">
        <taskdef resource="fr/jayasoft/ivy/ant/antlib.xml" uri="antlib:fr.jayasoft.ivy.ant">
            <classpath>
                <fileset dir="${ivy.home}">
                    <include name="ivy*.jar"/>
                    <include name="lib/*.jar"/>
                </fileset>
            </classpath>
        </taskdef>
    </target>

4.2. The <ivy:resolve> no longer exists (so not seen in the above -ivy-retrieve)

4.3. Modify the <project .. element to add the namespace (removed from -ivy-retrieve target):

xmlns:ivy="antlib:org.apache.ivy.ant"

5. No longer necessary to set ivy.home as ivy is now an ant lib

6. As above

7. This dependency doesn't seem to exist. I used the one from the Ivy tutorial - hello-ivy that parts of the Ivy download

According to http://mvnrepository.com/artifact/dom4j/dom4j/1.6.1 you set the dependency in the ivy.xml (see step 3) to:

<dependency org="dom4j" name="dom4j" rev="1.6.1"/>

That should work.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo