debug custom ant-task with eclipse

Imagine, one of your colleagues wrote an ant-task and you have to modify it, but you have no idea how it works.

The counter-task is just an example which is provided with 2 arguments (start and end) and counts the interval line by line, so nothing serious 😉

ant-task-debug-example

first, the task:

package org.jtaddeus.example;

import org.apache.tools.ant.Task;

public class AntCounterTask extends Task {  
	
	private int start;
	private int end;
	
	public void setStart(int start) {
        this.start = start;
    }
	public void setEnd(int end) {
        this.end = end;
    }
 
    public void execute() {
		for (int i=this.start; i<this.end; i++)
		System.out.println(i);
    }
}

and the pom for the task:

<?xml version="1.0" encoding="UTF-8"?>
<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.jtaddeus.example</groupId>
	<artifactId>ant-counter-task</artifactId>
	<version>1.0-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant-nodeps</artifactId>
			<version>1.8.1</version>
		</dependency>
	</dependencies>
</project>

second, the debugging-example:

<project name="debug-example" default="default" basedir=".">

	<property name="lib.dir" location="${basedir}/libs"/>
	<taskdef name="counterTask" classname="org.jtaddeus.example.AntCounterTask">
		<classpath>
			<pathelement location="${lib.dir}/ant-counter-task-1.0-SNAPSHOT.jar"/>
		</classpath>
	</taskdef>

	<target name="default">
		<counterTask start="5" end="15"/>
	</target>

</project>

and the output:

Buildfile: D:\Dropbox\Christoph\devapps\eclipse-workspace2\debug-example\build.xml
default:
[counterTask] 5
[counterTask] 6
[counterTask] 7
[counterTask] 8
[counterTask] 9
[counterTask] 10
[counterTask] 11
[counterTask] 12
[counterTask] 13
[counterTask] 14
BUILD SUCCESSFUL
Total time: 13 seconds

Now you have to start the ant-task with some special params. You can do this via shell-call by setting ANT_OPTS :

set ANT_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9999

or, if you like to stay in your eclipse-ide, you can pass them via run-config:
ant-task-debug-example-external-tools-configuration

ant-task-debug-example-external-tools-configuration-details

After starting the ant-build with this configuration, you will see nothing, because the jvm will wait vor a remote-debugger that will connect to localhost:9999

You create a remote-debugger in this way:

ant-task-debug-example-remote-debugger-1

ant-task-debug-example-remote-debugger-2

ant-task-debug-example-remote-debugger-3

Now if the ant-build is still running and you start the remote-debugging AND (!) you’ve set several breakpoints in your custom task, the debugging-perspective will open up and let you easily debug your task.