Using Apache Ivy on NetBeans

Sep 24 2013

I'm doing a bit of Java work right now, and I decided to play a bit with Ivy in order to manage my library dependencies.  Yes, I know Maven does that, but I'm just looking to do something a bit more lightweight and focused.  I'm perfectly happy with the Ant build structure/process that NetBeans uses by default and there's nothing that Maven offers to me that I need currently.  But I do want an automated dependency download process and I decided to spend a bit of time playing with Ivy in the NetBeans environment.

There's quite a bit of documentation for using Ivy and NetBeans, but most of it seems to be focused on one of the two NetBeans plugins that are out there (one of which has been abandoned) and I didn't really want a plugin-type of setup. 

In the end, what I wanted was for NetBeans to invoke Ivy as part of the build process to download any missing jar files, and I'd also like Ivy to add the jars it downloads to the overall project classpath for compilation purposes so that I don't have to do it.

For me, the best piece of info about how to use Ivy in NetBeans was this: https://gist.github.com/dblugeon/3070650

I did make a few changes since I have MacPorts installed and that's the Ivy version I use vs a standalone install, and I also added some code to the nbproject/project.properties file in order to have the jar files automatically added to the NetBeans compilation path for my project.

Here's what I did:

  • Created an ivy.xml in the root of my project (at the same level as the build.xml file).  The contents are:
    <ivy-module version="2.0">
        <info organisation="com.jamagination" module="medi" />
        <dependencies>
            <dependency org="commons-lang" name="commons-lang" rev="latest.release"/>
            <dependency org="commons-cli" name="commons-cli" rev="latest.release"/>
            <dependency org="org.hibernate" name="hibernate-core" rev="4.2.5.Final"/>
        </dependencies>
    </ivy-module>

  • Adjusted the build.xml file to invoke Ivy, and add the jar files to the project classpath.  My changes (compared a normal build.xml file) are in blue.  Also, I have my path to the ivy.jar file hardcoded.
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="Test" default="default" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">
        <description>Builds, tests, and runs the project Test.</description>
        <import file="nbproject/build-impl.xml"/>
        
        <property name="ivy.jar.file" value="/opt/local/share/java/ivy.jar" />
       
        <target name="ivy-set-path" description="sets path to ivy file">
            <path id="ivy.lib.path">
                <pathelement location="${ivy.jar.file}"/>
            </path>

            <taskdef resource="org/apache/ivy/ant/antlib.xml"
                     uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
        </target>
        
        <target name="download-deps" depends="ivy-set-path"
                description="-->download dependecy">
            <ivy:resolve/> <!-- Tell Ivy to resolve dependencies -->
            <ivy:retrieve/> <!-- Load dependencies to the project -->
            <!-- update classpath for netbeans/ant properties file -->
            <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="download-deps"/>

        <target name="-pre-compile-single" depends="download-deps"/>

        <target name="clean-cache-ivy" depends="ivy-set-path"
                description="delete all depency">
            <ivy:cleancache/>
            <propertyfile file="${basedir}/nbproject/project.properties">
                <entry operation="=" key="ivy.classpath" value=""/>
            </propertyfile>
        </target>

    </project>
     

  • Lastly, I made two changes to the nbproject/project.properties file.
    1. I added an "ivy.classpath=" property to the bottom of the file.  This is value that the download-deps target sets with the jars for the dependencies
    2. I changed the "javac.classpath" property (which had an empty value to be "javac.classpath=${ivy.classpath}".  This is the part that will automatically make the jars that Ivy downloads available to NetBeans (and makes them show up in the project's library list.

 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.