More Readable Classpaths for Ant Builds…

Its always a bit painful when you hit the inevitable ClassNotFound exception during development and you end up double checking an Ant classpath variable against the jars you think should be there. The way I typically debug this is to assign the particular ant classpath to a property, then echo the property value, e.g. :

<property name="current.classpath" refid="dao.compile.classpath"/>
<echo level="info" message="current.classpath=${current.classpath}"/>

Which will end up producing hard to read output that looks something like this (a big, semicolon delimited list of jars) :

[echo] current.classpath=d:\work\proto\dist\foo-toolkit.jar;d:\work\proto\lib\hibernate-2.1.8\lib\dom4j-.4.jar;d:\work\proto\lib\commons-discovery-0.2\commons-discovery.jar;d:\work\proto\lib\hibernate-.1.8\hibernate2.jar;d:\work\proto\lib\hibernate-2.1.8\lib\c3p0-0.8.4.5.jar;d:\work\proto\lib\hibernate-2.1.8\lib\cglib-full-2.0.2.jar;d:\work\proto\lib\hibernate-2.1.8\lib\ehcache-0.9.jar;d:\work\proto\lib\hibernate-2.1.8\lib\jta.jar;d:\work\proto\lib\hibernate-2.1.8\lib\odmg-3.0.jar;d:\work\proto\lib\hibernate-2.1.8\lib\oscache-2.0.jar;d:\work\proto\lib\hibernate-2.1.8\lib\swarmcache-1.0rc2.jar;d:\work\proto\lib\oracle-10g\classes12-10g.jar;d:\work\proto\lib\jakarta-struts-1.2.4\lib\commons-beanutils.jar;d:\work\proto\lib\commons-lang-2.0\commons-lang.jar;d:\work\proto\build\dao\classes;d:\work\proto\lib\jakarta-struts-1.2.4\lib\commons-logging.jar;d:\work\proto\lib\commons-collections-3.1\commons-collections.jar;d:\work\proto\lib\commons-io-1.0\commons-io.jar;d:\work\proto\lib\commons-dbutils-1.0\commons-dbutils.jar;d:\work\proto\lib\spring-1.2\acegi-security-0.8.2.jar;d:\work\proto\lib\spring-1.2\commons-codec.jar;d:\work\proto\lib\spring-1.2\ehcache-1.1.jar;d:\work\proto\lib\spring-1.2\spring-aop.jar;d:\work\proto\lib\spring-1.2\spring-beans.jar;d:\work\proto\lib\spring-1.2\spring-context.jar;d:\work\proto\lib\spring-1.2\spring-core.jar;d:\work\proto\lib\spring-1.2\spring-dao.jar;d:\work\proto\lib\spring-1.2\spring-hibernate.jar;d:\work\proto\lib\spring-1.2\spring-jdbc.jar;d:\work\proto\lib\spring-1.2\spring-mock.jar;d:\work\proto\lib\spring-1.2\spring-orm.jar;d:\work\proto\lib\spring-1.2\spring-remoting.jar;d:\work\proto\lib\spring-1.2\spring-support.jar;d:\work\proto\lib\spring-1.2\spring-web.jar;d:\work\proto\lib\spring-1.2\spring-webmvc.jar;d:\work\proto\lib\spring-1.2\spring.jar;d:\work\proto\lib\util.concurrent-1.0\util.concurrent.jar

That format gets a little painful to scan with the human eye, since all the paths typically begin with the same root. So here’s a better way to do this : We’ll use the ant task to replace the semicolons in our debug variable with a newline and some whitespace.


<property name="current.classpath" refid="dao.compile.classpath"/>

<propertyregex property="current.classpath"
               override="true"
               input="${current.classpath}"
               regexp=";"
               replace="&#13;&#10;&#9;"
               casesensitive="false"/>

<echo level="info">
   Classpath is :

      ${current.classpath}
</echo>
  

Now when we run the aforementioned Ant snippet, it will produce the following output (much easier on the eyes) :

[echo]
[echo] d:\work\proto\dist\foo-toolkit.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\dom4j-1.4.jar
[echo] d:\work\proto\lib\commons-discovery-0.2\commons-discovery.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\hibernate2.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\c3p0-0.8.4.5.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\cglib-full-2.0.2.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\ehcache-0.9.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\jta.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\odmg-3.0.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\oscache-2.0.jar
[echo] d:\work\proto\lib\hibernate-2.1.8\lib\swarmcache-1.0rc2.jar
[echo] d:\work\proto\lib\oracle-10g\classes12-10g.jar
[echo] d:\work\proto\lib\jakarta-struts-1.2.4\lib\commons-beanutils.jar
[echo] d:\work\proto\lib\commons-lang-2.0\commons-lang.jar
[echo] d:\work\proto\build\dao\classes
[echo] d:\work\proto\lib\jakarta-struts-1.2.4\lib\commons-logging.jar
[echo] d:\work\proto\lib\commons-collections-3.1\commons-collections.jar
[echo] d:\work\proto\lib\commons-io-1.0\commons-io.jar
[echo] d:\work\proto\lib\commons-dbutils-1.0\commons-dbutils.jar
[echo] d:\work\proto\lib\spring-1.2\acegi-security-0.8.2.jar
[echo] d:\work\proto\lib\spring-1.2\commons-codec.jar
[echo] d:\work\proto\lib\spring-1.2\ehcache-1.1.jar
[echo] d:\work\proto\lib\spring-1.2\spring-aop.jar
[echo] d:\work\proto\lib\spring-1.2\spring-beans.jar
[echo] d:\work\proto\lib\spring-1.2\spring-context.jar
[echo] d:\work\proto\lib\spring-1.2\spring-core.jar
[echo] d:\work\proto\lib\spring-1.2\spring-dao.jar
[echo] d:\work\proto\lib\spring-1.2\spring-hibernate.jar
[echo] d:\work\proto\lib\spring-1.2\spring-jdbc.jar
[echo] d:\work\proto\lib\spring-1.2\spring-mock.jar
[echo] d:\work\proto\lib\spring-1.2\spring-orm.jar
[echo] d:\work\proto\lib\spring-1.2\spring-remoting.jar
[echo] d:\work\proto\lib\spring-1.2\spring-support.jar
[echo] d:\work\proto\lib\spring-1.2\spring-web.jar
[echo] d:\work\proto\lib\spring-1.2\spring-webmvc.jar
[echo] d:\work\proto\lib\spring-1.2\spring.jar
[echo] d:\work\proto\lib\util.concurrent-1.0\util.concurrent.jar

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: