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:
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:
This gives the following output:
So there you have it, nicely formatted classpaths using only standard Ant tasks.
Technorati Tags: Ant, Lance Hankins, Andrew Beacock
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}| |-- "
property="echo.path.compile"
refid="path.classpath.compile">
</pathconvert>
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: Ant, Lance Hankins, Andrew Beacock
Comments
-- Andre
I wrote a little macrodef based on your idea:
<!-- = = = = = = = = = = = = = = = = =
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>
To be used as:
<echopath pathid="mypath"/>
It was very useful. Thanks much.
Suresh.
--Suresh Nariya
Ravi
12/8/08
Ravi
12/8/08
( and the macrodef too )
-- fasteez2 at hotmail dot com