14

I am developing a custom ant task with java in the eclipse ide.

is it somehow possible to debug it? that is put a breakpoint on the java line and when ant is executed, it stops there and i can step through the code?

edit: since this question has been closed as duplicate, i want to elaborate why it is quite different.

i am looking to debug the java code that makes up an ant task and not the just the steps of an ant build. the answers already helped me partially, but i still need to figure out why my eclipse doesnt have Debug Remote Java Application.

clamp
  • 33,000
  • 75
  • 203
  • 299
  • Have you seen this question? http://stackoverflow.com/questions/3039933/ant-debugging-in-eclipse – default locale Mar 13 '13 at 08:42
  • yeah, but i dont want to debug the ant buildfile, i want to debug the java code that makes up an ant task. – clamp Mar 13 '13 at 08:52
  • Sorry, misunderstood your question. [Relevant part of custom task tutorial](http://ant.apache.org/manual/tutorial-writing-tasks.html#Debugging) suggests to build ant from source code for this. – default locale Mar 13 '13 at 09:17
  • Don't see any difference between debugging ant build file and ant task. Put your ant task in the build file, run Ant in debug mode, connect your eclipse to the ant process, and it the build will stop at the break points in your Ant task, where you can actually debug your task in the "real" environment. – Dante WWWW Mar 13 '13 at 09:26
  • @coolcfan well that doesnt work for me. what exactly do you mean with `connect your eclipse to the ant process` ? – clamp Mar 13 '13 at 09:45
  • @clamp Sorry I am not using Eclipse, but when I use Netbeans, I set ANT_OPTS to put Ant to debug mode, as Nick's answer does -- then when you run ant, it will pause to wait a debugging client to connect (this actually runs an agent with the ant process, and opens a socket for the client to connect). Then in my Netbeans, I use "connect to debugger", filling up hostname (like localhost) and the port number (like 5005 as Nick's answer does), and then clieck "connect". When I debug client connects to the agent, the ant build starts to run. In Eclipse, there should be a similar place. – Dante WWWW Mar 13 '13 at 09:56
  • ok, thanks i found the place to set the ANT_OPTS in eclipse under the external tools configuration -> jre tab. now i need to find out how eclipse debugger can connect to the ant process. – clamp Mar 13 '13 at 10:15
  • 1
    Please do not close this question as a dup of the linked one. It is actually quite different. – Perception Mar 13 '13 at 11:34
  • here is a good tutorial on how to do it http://www.asjava.com/ant/ant-how-do-i-remote-debug-java-code-in-ant-and-eclipse/ – Abdu Oct 28 '16 at 12:09

1 Answers1

22

You need to start ANT with the remote debug enabled by entering the following before you run ANT.

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

This will cause the JVM running ANT to pause until you attach your debugger.

Next you'll need to modify the classpath ANT is using in order to pickup the classes that form the custom task you're writing. I don't think there's a way to do this easily with environment vars, so the quickest thing is to just edit the ant.bat file.

Now when your custom task is run, it will stop at any breakpoints in your IDE.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • thanks, is a simple enough to guide ant to the classes that make up my task? – clamp Mar 13 '13 at 09:27
  • No, `taskdef` just takes the classname, you'll need add the IDE's output directory to the ANT's classpath in order for the ANT JVM to find the class. – Nick Holt Mar 13 '13 at 09:59
  • According to https://ant.apache.org/manual/running.html, it looks like you can add your library to the classpath using the -lib command line option for ant 1.6 and above. This would be preferable to editing the batch file. – kc2001 Oct 30 '17 at 17:02