Wednesday, March 23, 2011

Migrating NetBeans jar references to Ivy dependencies

Search on http://mvnrepository.com for the library

Sometimes mvnrepository doesn't have the library version you want. In this case I go to the library's website and see if they have a maven repository URL listed there. If they do, you have to add it to your ivysettings.xml file. If your NetBeans project doesn't yet have/use a ivysettings.xml file, you'll have to add one:
  • Add ivysettings.xml file to your project's root
  • Set the ivysettings.xml file in Project Properties/Ivy

<?xml version="1.0" encoding="UTF-8"?>

<ivysettings>
<settings defaultResolver="myChain"/>
<caches checkUpToDate="true"/>
<resolvers>
<chain name="myChain">
<ibiblio name="lombok" m2compatible="true"
root="http://projectlombok.org/mavenrepo/"/>
<ibiblio name="jetty" m2compatible="true"
root="http://oss.sonatype.org/content/groups/jetty/"/>
<ibiblio name="ibiblio" m2compatible="true"/>
</chain>
</resolvers>
</ivysettings>

Tip: Start by replacing the libraries that might have dependencies themselves, e.g., hibernate, and remove all the jar refs they bring in. This will avoid redundant dependency specification for common libraries, e.g., commons-logging. Also, you might find that you were importing jars you didn't even need. (In truth, though, you might get some new ones you don't need. The lib you are depending on might not have an Ivy configuration or Maven scope specific enough to your usage, in which case you could get some unnecessary jars.)

I had another problem where all my tests would be run twice, the second run crashing with no indication as to why. Turns out commons-jxpath 1.2 was bringing in ant-optional-1.5.1.jar, which screws up NetBeans test runs. Changing to reference jxpath 1.3 and deleting build/jar solved the problem.

Autodownloading Ivy & Cleaning build/jar

Being a fan of automation, I figured out how to autodownload Ivy and it's dependencies. Also, I discovered that IvyBeans calculates the classpath based on all the jars it finds in build/jar, but doesn't clean it, which can lead to problems (e.g., http://groups.google.com/group/usr-ivybeans/browse_thread/thread/629c30771ec8192d). So, I also added code to clean build/jar on every build. Add this to your NetBeans build.xml:



<!-- Ivy -->
<property name="ivy.install.version" value="2.2.0" />
<property name="ivy.home" value="${user.home}/.ant/lib" />
<property name="ivy.jar.file" value="${ivy.home}/ivy-${ivy.install.version}.jar" />
<property name="ant-contrib.version" value="1.0b3" />
<property name="ant-contrib.lib" value="${ivy.home}/ant-contrib-${ant-contrib.version}.jar" />
<target name="-pre-init" depends="download-ivy,clean-build-libs"/>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
        uri="antlib:org.apache.ivy.ant" classpath="${ivy.jar.file}"/>
<target name="download-ivy">
<mkdir dir="${ivy.home}"/>
<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" ignoreerrors="true" maxtime="15"/>
<get src="http://repo1.maven.org/maven2/ant-contrib/ant-contrib/${ant-contrib.version}/ant-contrib-${ant-contrib.version}.jar"
          dest="${ant-contrib.lib}" usetimestamp="true" ignoreerrors="true" maxtime="15"/>
</target>
<!-- Clean build libs so Ivy doesn't include stale jars in classpath -->
<target name="clean-build-libs">
<delete dir="${basedir}/${build.dir}/${lib.dir}"/>
</target>


0 Comments:

Post a Comment

<< Home