Skip to main content

Pretty printing Java classpaths using Ant's pathconvert task

Lance Hankins posted on Saturday about more readable classpaths using the <propertyregex> task. It jogged my memory back about 4 years when I was trying to solve the same problem. This was back in the days of Ant 1.4.1 and just before the existence of the optional Ant-Contrib package. Back then I was considered a bit of an Ant Meister by my colleagues as I managed to spend hours tweaking the smallest of things in my lovingly created Ant build environment searching for perfection...

So how did I solve this particular issue? A clue is in the title of this post, I used the <pathconvert> task, which is normally used to convert lists of files, directories, etc. and convert them to a different platform's style. So path separators using Windows ';' get converted to Unix ':', etc. But you can also use it to convert the path separators to something else, in my case a nicely formatted string for displaying in a printable form.

Here's the code:

<!-- get the source compile classpath in a printable form -->
<pathconvert pathsep="${line.separator}| |-- "

The original classpath is referenced in 'path.classpath.compile', and the newly formatted classpath is saved in 'echo.path.compile'. The magic is the pathsep statement. This says that the path separator (the ';' or ':') is to be replaced with a new line (CR or CRLF depending on operating system) followed by some characters that make the listing look nice.

To display your pretty printed classpath just use:

<echo message="|-- compile classpath"/>
<echo message="| |"/>
<echo message="| |-- ${echo.path.compile}"/>

This gives the following output:

[echo] |-- compile classpath
[echo] | |
[echo] | |-- /tomcat/common/lib/jsp-api.jar
[echo] | |-- /tomcat/common/lib/ant.jar
[echo] | |-- /tomcat/common/lib/jmx.jar
[echo] | |-- /tomcat/common/lib/commons-collections-3.1.jar
[echo] | |-- /tomcat/common/lib/commons-dbcp-1.2.1.jar
[echo] | |-- /tomcat/common/lib/commons-el.jar
[echo] | |-- /tomcat/common/lib/commons-lang-2.1.jar
[echo] | |-- /tomcat/common/lib/commons-pool-1.2.jar
[echo] | |-- /tomcat/common/lib/tools.jar
[echo] | |-- ...

So there you have it, nicely formatted classpaths using only standard Ant tasks.

Technorati Tags: , ,


Anonymous said…
Awesome! The "best" ant tip I've run across in ages...

-- Andre
Anonymous said…
Thank you so much! I was looking for how to do this. Perfect.
Anonymous said…
Great tip, very useful. Cheers!
Anonymous said…
Great tip. I was breaking my head over ways to print the classpath.
Anonymous said…
Great thanks!
Remke said…
I doubt if anyone will read this, since your post is a year old already, but nevertheless...
I wrote a little macrodef based on your idea:
<!-- = = = = = = = = = = = = = = = = =
macrodef: echopath
= = = = = = = = = = = = = = = = = -->
<macrodef name="echopath">
<attribute name="pathid"/>
<property name="line.pathprefix" value="| |-- "/>
<!-- get given path in a printable form -->
<pathconvert pathsep="${line.separator}${line.pathprefix}"
<echo>Path @{pathid}

To be used as:
<echopath pathid="mypath"/>
abeacock said…
Remke, That is a very nice clean solution, I've not used the macrodef stuff yet, is that available in Ant 1.6?
Anonymous said…
Appreciate your sharing this tip.
It was very useful. Thanks much.
Watsh said…
Very useful. Appreciate it. Thank you.
Anonymous said…
Thanks a bunch... very useful tip

Anonymous said…
Very useful for printing the classpathref to be used by javac.
Anonymous said…
Suresh Nariya said…
Thanks for Great Article.
--Suresh Nariya
Anonymous said…
All ooohs and ahhhs goes to Andrew Beacock ! Excellent tip !
abeacock said…
Thank you all very much for your recent thanks, it's nice to know that one of my older posts is still useful!
Anonymous said…
It is wonderful and it works! Thank you

Anonymous said…
It is wonderful and it works! Thank you

Anonymous said…
ultimate tip.
Anonymous said…
nice little info, thanks
( and the macrodef too )

-- fasteez2 at hotmail dot com
Anonymous said…
Trop cool, gracias!
Very useful information i appreciate it. thank for sharing
Anonymous said…
To the point in explaining the pathseparator. Thanks.