Apache Ant Java Debug Example

In this example, we will discuss about Apache Ant Java Debug Example. Before we start with this article, it is expected to have knowledge of Java, Apache Ant, and software build and deployment process so that we understand the usage of Apache Ant. For more information about Apache Ant installation and configuration with a basic Java example, please refer to Apache Ant Java Task.

1. Introduction

Apache Ant debugging can be done via eclipse or via command line. We will see the working of how Ant debugging work in the sections below but here is a brief about it. When we talk about command line, there are various attributes provided by Apache Ant to help analyse any problem we might have while using Ant build. The two options that we know are -verbose and -debug. Apache Ant provides a -debug option to help analyze our problem with Ant or Java task which provides more detailed information than -verbose. Using -debug option enables the debugging capabilities of Apache Ant.

In this tutorial, we will see how to do debugging using Apache Ant both via command line and in eclipse. We will be using Java v 1.8 , Eclipse Kepler and Apache Ant v 1.10.1.

2. Debugging in Apache Ant

2.1 Debugging Ant build script in eclipse

Ant provides a debugger that allows you to step through the build scripts just like we can debug our Java code using breakpoints. We can add breakpoints in build.xml, then step through the script. There is also a support for mouse hovering and the variables view. This is probably the best functionality in the Eclipse Ant integration.

Please follow the steps below:

1. Place breakpoints inside the build.xml targets, on the lines that call the task we’re interested in stepping through. For putting breakpoints just double click on the blue bar on the left. A green ball/icon appears which states that a breakpoint has been set.

Fig 1: Breakpoint in Build.xml

2. To debug the Ant build.xml created in Eclipse, Right click on build.xml->Debug As -> Ant Build.

Fig 2: Debugging build.xml in eclipse

3. Just as debugging works with Java files, the debugger in the build file pauses when the execution reaches the line on which we have set the breakpoint.

Fig 3: Showing how a thread is stuck at breakpoint

4. Now we can easily debug the Ant build file line by line as we do using Java breakpoints. To see the values, we can mouse over the attribute and the value shows up. Please see the below Screen Shot.

Fig 4: Mouse over the debugger to see variable values.

5. To traverse or step over through the lines we can use Fn+F6 and to step over to next breakpoint we can use Fn+F8. As we step through each task, it will be executed and produce its output, which we can examine to see what’s going wrong in the build process.

6. To see the call stack, we need to switch to the Debug perspective of eclipse. The Debug view shows the call stack of the tasks currently being executed. If a task calls another target — say, clean — that target appears above the current task in the call stack.

Fig 5: Call Stack-Debug perspective

7. A Variables view is also available. Open this view to see all the Ant properties, which are the Ant equivalent of variables. The properties are shown grouped in three parts:

  • System properties: Properties set from the system for the build
  • User properties: Properties such as those set using the -D option
  • Runtime properties: Properties defined in a buildfile that are set during runtime

Please refer to the screen shot below.

Fig 6: Variables View in eclipse showing all the properties.

Note: The Ant debugger doesn’t allow us to change the variable values in runtime as we do with the java debugger.

2.2 Debugging build script via command line

2.2.1 Debugging Ant build script using -debug or -d option

To run Ant build in debug or verbose mode in command prompt, we can simply use the -d or -debug (for debug mode) and -verbose(for verbose mode). This gives us deep insight into any error we might be facing with the Ant build. It also helps to understand how the particular Ant script is working (or not working) and not Ant itself.

Using -debug option in command prompt –
Open command prompt and go to the folder where Ant is installed. Type below command. Refer screenshot below.

ant -debug

Fig 7: Command prompt -debug option for Ant build

If we check the command prompt, we can see more detailed logs that helps to debug any problem we might have in our build.

Using -d option in command prompt –
Open command prompt and go to the folder where Ant is installed. Type below command. Refer screenshot below.

ant -d

Fig 8: Command prompt -d option for Ant build

2.2.2 Debugging Ant build script using -verbose option

To enable extra logging in Ant build.xml, we can also use the verbose command line options as shown below. The verbose command is also used for debugging Ant build script and it prints additional information to the console. However the debug command print considerably more additional information comparatively to verbose.

Using -verbose option in command prompt –
Open command prompt and go to the folder where Ant is installed. Type below command. Refer screenshot below.

ant -verbose

Fig 9: Command prompt -verbose option for Ant build

Using -v option in command prompt –
Open command prompt and go to the folder where Ant is installed. Type below command. Refer screenshot below.

ant -v

Fig 10: Command prompt -v option for Ant build

3. Conclusion

Through this example, we have learned how to debug Apache Ant build script in case of any errors or issues we might face in our project. We have also seen how to use various debugging techniques via eclipse and command line options.

Neha Goel

Neha holds a Bachelors degree in Computer Science and Engineering. Currently she is working as a Sr. Programmer Analyst for a client in USA and has a total of 9+ years of Java/J2EE experience.Her expertise includes participation in all stages of SDLC. She has experience with multiple web based and enterprise based applications. She has a very impressive understanding in Object oriented architecture, analysis, design and software development using latest technologies like Java, J2EE , Restful Services, Spring, Hibernate, JDBC, JSP, Servlets, GWT, ATG etc.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button