FaqIvy

(Difference between revisions)
(Can I use Ivy to download libraries for my project from a Maven repository?)
Line 2: Line 2:
===Can I use Ivy to download libraries for my project from a Maven repository?===
===Can I use Ivy to download libraries for my project from a Maven repository?===
-
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:
+
(First of all there is an [http://code.google.com/p/ivybeans/ Ivy module for NetBeans] which may be all you need.)
-
There is a [http://code.google.com/p/ivybeans/ Ivy module for NetBeans]
+
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):
1. Download Ivy (at least 1.3 RC2, '''not''' 1.2 which has a bug affecting NetBeans):
Line 155: Line 155:
http://www.jaya.free.fr/ivy/doc.html
http://www.jaya.free.fr/ivy/doc.html
 +
 +
[[IvyIntegrationNotes]] has more discussion.
------
------
Applies to: NetBeans 5.0, 5.5.1
Applies to: NetBeans 5.0, 5.5.1

Revision as of 16:34, 12 January 2012

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.)

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

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