Using pathconvert

Q: I set up a bunch of paths using the path DataType. Now I have this task that only takes paths as an attribute. Do I need to rewrite everything using properties? I thought path solved this problem?

When paths were first introduced, this was a legitimate gripe. Some people wrote their own tasks to handle the conversion while others suffered the pain of maintaining two sets of data, or just stuck with using properties. However, when the pathconvert task came out, all was good.

Look at this example buildfile:

<project name="test" default="test" basedir=".">

    <path id="classpath">
        <pathelement location="lib"/>
        <pathelement location="lib/test.jar"/>
    </path>
    <property name="somepath" value="lib:lib/test.jar"/>
    <target name="test">
        <java classname="org.oreilly.Test" fork="yes" \
          classpath="${somepath}"/>
    </target>

</project>

Assume for the moment that the java task must use the classpath attribute instead of a path DataType.[58] The only way to have both a property and DataType represent a path (without pathconvert) is to define both. In this example, that doesn’t seem like a big deal, but these paths can get long and complex. You only want to define them once. The following example uses pathconvert to convert a path DataType into a property setting:

<project name="test" default="test" basedir="."> <path id="classpath"> <pathelement location="lib"/> <pathelement location="lib/test.jar"/> </path> <target name="test"> <pathconvert targetos="windows" property="somepath" \ ...

Get Ant: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.