Thursday, June 19, 2014

How to extend a Classpath entry in Ant

When dealing with Ivy in an Ant build script I wanted to simply add Ivy's classpath to the buildpath of my app instead of copying all the JARs around. The challenge boiled down to overriding, i.e. extending the default script's classpath entry named project.all.jars.path.

Here's what you can not do:

<path id="project.all.jars.path">
    <path refid="project.all.jars.path" />
    <path refid="ivy.build.path" />
</path>

Defining a path by referencing itself doesn't work because both these references would point to the same object. Here's what does work:

<pathconvert property="classpathPropBefore" refid="project.all.jars.path" />

<path id="project.all.jars.path">
    <path path="${classpathPropBefore}" />
    <path refid="ivy.build.path" />
</path>

It's simple if you think about it. All I did was write the old content of the classpathentry into the property classpathPropBefore and then redefined the classpath entry with it's old content plus my addition.

No comments:

Post a Comment