Popular @ spacer

  • Automated API Testing Helps the...
  • 5 Features in Java 9 that WILL Change How...
  • How JAVA Changed Programming Forever at 20...
  • e Understanding Git terms origin, master,...
  • The most in demand programming languages...
  • Dreamz : HTML5 Responsive Bootstrap...
  • How to monitor a Java EE DataSource
spacer Submit a story  spacer Vote on new stories
Forum Controls
  • spacer Reply to this Thread
  • spacer Search Forum
  • spacer Back to Thread List
Whos Online: 71 guest(s), 0 user(s). More info »
Spotlight Features
Read the Spotlight Archives
Replies: 10 - Pages: 1  
Threads: [ Previous | Next ]
  spacer Reply

Ant: Debugging Classpaths

At 11:14 PM on May 1, 2006, R.J. Lorimer wrote:

Fresh Jobs for Developers Post a job opportunity

spacer

A lot of times when I'm working on an Ant build script, I'll run into compiler errors that I have no reason to believe are valid. You know the kind; you've written a script and you've set up all of your classpaths, and then you run the script and it starts complaining about classfiles missing that you *just know* are not missing.

This process can be a real pain to debug, particularly because Ant isn't exactly transparent in its running (Eclipse has the ability to debug an Ant script, but that's a different discussion). One technique I have used many, many times is to have Ant print out the classpath I'm getting ready to compile against so that I can see what class files, JAR files, and other minutia are actually on the classpath so that there is no doubt on my mind that the correct dependencies are being referenced.

To have Ant print out a classpath, you simply have to give your classpath a ref-id. This allows you to set up the correct variables later for printing. Here is an example. This is a snippet Ant file example that may look something similar to an ant script you have floating around:


<?xml version="1.0"?>
<project name="project" default="default">
	<property name="lib" value="web/WEB-INF/lib"/>
	<property name="src" value="src"/>
	<property name="dist" value="dist"/>
		
	<path id="classpath">
	  <fileset dir="${lib}">
		  <include name="**/*.jar"/>
	  </fileset>
	</path>

    <target name="default" description="--> description">
    	<javac srcdir="${src}" destdir="${dist}">
    		<classpath refid="classpath"/>
	</javac>
    </target>

</project>

If we wanted to debug the 'classpath' path attribute to make sure it had every JAR we anticipated, we could do this:


<?xml version="1.0"?>
<project name="project" default="default">
	<property name="lib" value="web/WEB-INF/lib"/>
	<property name="src" value="src"/>
	<property name="dist" value="dist"/>
		
	<path id="classpath">
	  <fileset dir="${lib}">
		  <include name="**/*.jar"/>
	  </fileset>
	</path>

    <target name="default" description="--> description">
	<!-- Capture the path as a delimited property using the refid attribute -->
	<property name="myclasspath" refid="classpath"/>
	<!-- Emit the property to the ant console -->
	<echo message="Classpath = ${myclasspath}"/>

    	<javac srcdir="${src}" destdir="${dist}">
    		<classpath refid="classpath"/>
	</javac>
    </target>

</project>

The refid attribute on the property tag serializes the object with the given reference ID onto the property (in this case, our classpath).

Note that after slinging together this tip, I also ran into this much more friendly version from Andrew Beacock's Blog: Pretty Printing Java Classpaths Using Ant's pathconvert Task . I wish I could take credit for this as it is much more fancy than mine spacer .

Anyway, this proved to be an interesting problem that elicited help from several teams: JDT Core, Platform/JDT Text, JDT UI, and Platform/JDT Debug.

Until next time,

R.J. Lorimer
Contributing Editor - rj -at- javalobby.org
Author              - www.coffee-bytes.com
Software Consultant - www.numbersix.com

spacer -->

10 replies so far (spacer Post your own)

1 . At 5:55 AM on May 2, 2006, Sebastian Mueller wrote:
  spacer Reply

The real tip is: includeantruntime="false"

Interesting tip.

However the biggest issue I've come across is the following:
The javac task *by default* has the 'includeantruntime' flag set to 'true' ! This IMHO is the stupidest thing the Ant developers ever did: The result is that all the librarys any ant task ever uses is part of the classpath during the compilation. This can lead to the weirdest errors you will ever see. Especially because in some configurations Ant has a mode so that any jar file that simply resides in the working directory where you invoke the ant call is automagically included to the ant classpath and thus included into the javac classpath (!).

So what I did is I did a "find in paths (e.g. find *.xml & grep javac)" for javac and make sure that includeantruntime="false" is specifiec for each javac task (only Ant developers and Ant task developers should ever need to set this to 'true').

I hope that for Ant 2.0 they remove this "feature".

'hope this helps other people as it helped me getting rid of the weirdest Ant javac classpath problems.

regards - Sebastian
yWorks - the diagramming company yFiles - graph layout library yGuard - Ant Java Obfuscator
2 . At 8:30 AM on May 2, 2006, R.J. Lorimer wrote:
  spacer Reply

Re: The real tip is: includeantruntime="false"

Sebastian,

Is that true even when you set 'fork="true"'?

Regards,
Best, R.J. Lorimer
3 . At 8:42 AM on May 2, 2006, Sebastian Mueller wrote:
  spacer Reply

Re: The real tip is: includeantruntime="false"

I just checked and it seems to me as if it is still true (just add an "import org.apache.tool.ant.Task;" to one of your sourcefiles and see whether it compiles through Ant) . It could be considered another bug/problem if the behavior was different depending on the fork setting.

Regards, Sebastian
yWorks - the diagramming company yFiles - graph layout library yGuard - Ant Java Obfuscator
4 . At 5:22 PM on May 2, 2006, Kees Kuip wrote:
  spacer Reply

Re: The real tip is: includeantruntime="false"

and ant also includes the environmentsetting CLASSPATH !

So I always edit the ant-scriptfile to set the CLASSPATH to ""
5 . At 1:46 PM on May 3, 2006, Mike Miller wrote:
  spacer Reply

Re: Ant: Debugging Classpaths

I believe I also was able to see the contents of the classpath thru hover-text when debugging an ant script. That was a very pleasant surprise!
6 . At 9:33 PM on May 4, 2006, Glen Marchesani wrote:
  spacer Reply

Re: Ant: Debugging Classpaths

I do something similar but got tired of reading all the path separators so I created this macro
	<macrodef name="echo-path">
		<attribute name="pathref" />
	   	<sequential>
	   		<echo>echoing path=@{pathref}</echo>
			<for param="fromfile">
	   			<path refid="@{pathref}"  />
	   			<sequential>
					<echo>@{fromfile}</echo>
	   			</sequential>
			</for>
	   	</sequential>
	</macrodef>


Here is how you use it...

<echo-path patclass="xyz.path" />



Here is one that works off of filesets

	<macrodef name="echo-fileset">
		<attribute name="filesetref" />
	   	<sequential>
	        <pathconvert pathsep="
" property="@{filesetref}.echopath">
		   		<path>
		   			<fileset refid="@{filesetref}" />
		   		</path>
		   	</pathconvert>
    		<echo>   ------- echoing fileset @{filesetref} -------</echo>
	        <echo>${@{filesetref}.echopath}</echo>
	   	</sequential>
	</macrodef>


Usage:
<echo-fileset filesetref="xyz.fileset" />
7 . At 9:03 AM on Mar 19, 2007, Israr Ahmed wrote:
  spacer Reply

Re: Ant: Debugging Classpaths

The good and interesting tip.
There certainly have been performance issues with Java. We've been working really hard on them. The primary way we've attacked the problem is with advanced virtual machines. The performance has been getting very nice. --James Gosling, 1999.
8 . At 9:48 AM on Nov 9, 2007, Tim Neilson wrote:
  spacer Reply

Re: Ant: Debugging Classpaths

Very useful tip, but the splitting of the paths onto new lines didn't work for me. But this did:

<pathconvert pathsep="${line.separator}" property="@{filesetref}.echopath">
9 . At 8:43 PM on Nov 9, 2007, javacorner wrote:
  spacer Reply

Re: Ant: Debugging Classpaths

Thansk for the tip,I meet the same errors.
Now I can fix it.
java persistance
10 . At 8:55 PM on Nov 9, 2007, javacorner wrote:
  spacer Reply

Re: Ant: Debugging Classpaths

Thansk for the tip,I meet the same errors.
Now I can fix it. (same post.please delete this ,sorry)
java persistance
Replies: 10 - Pages: 1   Threads: [ Previous | Next ]

spacer
spacer spacer spacer spacer spacer spacer spacer spacer


gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.