An Ant Task to Maintain Eclipse Classpaths with Ivy Resolve Results

February 13, 2009 – 00:21 | java | Tags: , , ,

I put together an Ant task(download) that can use the result of the ivy:resolve task to maintain Eclipse’s .classpath file. If your ivy:resolve also generates references to the source jars, this task will also attach them to the corresponding classpath entries in the .classpath file.

To use the class, drop the class on you Ant classpath, then declare the task like so:

  1. <taskdef name="ivyeclipse" classname="org.apache.ivy.ant.EclipseClasspath"/>

It’s a standard Ivy post-resolve task. What that means is a) it takes the same common attribute set as any other post-resolve tasks would, and b) it must be run after an ivy:resolve call. For example, every time the following line

  1. <ivyeclipse conf="provided,testtime" />

executes, the entries in .classpath that were generated from the previous run are removed, and new entries added according to the latest ivy:resolve result on ivy conf “provided” and “testtime”. The entries generated by this task are marked with an attribute, ivygen="true", so any entries you manually added are not impacted. Note: if you have added classpath entries manually for the dependencies in ivy.xml, before running this task for the first time ever, it’s a good idea to remove all of them, otherwise you’ll end up with duplicated entries.

To invoke the source jar attachment support, just add to the ivy.xml references to the source jars. For example:

  1. <dependency org="org.hibernate" name="hibernate-core" rev="3.3.1.GA" conf="compile;references">
  2.   <artifact name="hibernate-core" type="sources" ext="jar" m:classifier="sources" conf="references" />
  3. </dependency>

would add hibernate-core’s source jar to ivy conf named “references”. Then make sure to include conf “references” in the ivy:resolve call, such as:

  1. <ivyeclipse conf="provided,testtime,references" />

The classpath entries updated will then also have the corresponding source jars (if available) attached.

Here’s the source code for the task.

Trackback from your site, or follow the comments in RSS.
  1. 2 Responses to “An Ant Task to Maintain Eclipse Classpaths with Ivy Resolve Results”

  2. Nice ant task. I’ve added some extensions:

    By toberl on Jun 17, 2010

  1. 1 Trackback(s)

  2. Jun 17, 2010: Ivy: Attaching sources and docs to libraries in Eclipse

Post a Comment