Thursday, 26 February 2009

Pretty printing Java classpaths using Ant's pathconvert task

This is a blatent reproduction from a friend of mines blog Andrew Beacock for two reasons.

1. I can never find it.
2. A kind sole by the name of Remke provided a very nice enhancement using a macro

Here is the echopath macro


<!-- = = = = = = = = = = = = = = = = =
macrodef: echopath
= = = = = = = = = = = = = = = = = -->
<macrodef name="echopath">
<attribute name="pathid" />
<sequential>
<property name="line.pathprefix" value="| |-- " />
<!-- get given path in a printable form -->
<pathconvert pathsep="${line.separator}${line.pathprefix}"
property="echo.@{pathid}" refid="@{pathid}">
</pathconvert>
<echo>
Path @{pathid}
${line.pathprefix}${echo.@{pathid}}
</echo>
</sequential>
</macrodef>


Call the macro like this:
<path id="quick_test_classpath">
<path refid="test.classpath" />
<pathelement location="${instrumented.classes.dir.dev}"/>
<pathelement location ="${classes.dir.dev}" />
<pathelement location ="${test.config.dir.dev}" />
<pathelement location ="${dfcommon.dir.dev}" />
</path>

<target name="display_my_classpath" >
<echopath pathid="quick_test_classpath"/>
</target>


This will produce some output like:
[echo]
[echo] Path quick_test_classpath
[echo] | |-- C:\projects\foo\bar\test\lib\ant-junit.jar
[echo] | |-- C:\projects\foo\bar\test\lib\dbunit-2.3.0.jar
[echo] | |-- C:\projects\foo\bar\WebRoot\WEB-INF\lib\commons-beanutils.jar
[echo] | |-- C:\projects\foo\bar\WebRoot\WEB-INF\lib\commons-codec-1.3.jar
[echo] | |-- C:\projects\foo\bar\WebRoot\WEB-INF\lib\commons-collections-3.2.1.jar
[echo] | |-- C:\projects\foo\bar\WebRoot\WEB-INF\lib\commons-dbcp-1.2.1.jar
[echo]


Andrew's original bog post can be found here

No comments: