Ant – How to print classpath in Ant script

The below target print_classpath in build xml demonstrates how to print the project classpath in ant script, this gives the ability to check loaded classpath at runtime.

<?xml version="1.0" encoding="UTF-8"?>
<project name="lms" default="print_classpath" basedir="D:/workspace/lms">
	<property environment="env" />
	<property name="catalina.home" value="D:/developer/apache-tomcat-6.0.32" />
	<property name="ant.dir" value="D:/developer/apache-ant-1.8.3" />
	<property name="webRoot.dir" value="${basedir}/webapp" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />

	<!-- init classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		<!-- add tomcat lib -->
		<fileset dir="${catalina.home}/lib">
			<include name="*.jar" />


	<!-- show classpath jars -->
	<target name="print_classpath">
		<property name="myclasspath" refid="project.classpath"/>
		<echo message="Classpath = ${myclasspath}"/>


I executed the build.xml and got the following output, the all classes are printed in one line.


[echo] Classpath = D:\workspace\lms\webapp\WEB-INF\lib\antlr-2.7.7.jar;D:\workspace\lms\webapp\WEB-INF\lib\aopalliance-1.0.jar;D:\workspace\lms\webapp\WEB-INF\lib\asm-3.3.1.jar; …


The below is the example to print the formatted the classpath:

	<!-- get the source compile classpath in a printable form -->

	<pathconvert pathsep="${line.separator}|   |-- "

	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>

The output is as below, it is much better good-looking than previous one.

[echo] |– compile classpath
[echo] | |
[echo] | |– D:\workspace\lms\webapp\WEB-INF\lib\antlr-2.7.7.jar
[echo] | |– D:\workspace\lms\webapp\WEB-INF\lib\aopalliance-1.0.jar
Оцените статью
Добавить комментарий

Your email address will not be published. Required fields are marked *