« Back to Debugging

Debugging ant java tasks

Introduction #

Sometimes it is necessary to debug java code which gets called from an ant task. There are many examples of this: from LangBuilder.java, which manages Language.properties files, to the ServiceBuilder itself.

Debugging #

A way to set up debugging is to tell ant java task to enable a debugging port, then connect the IDE of our choice to that port. To do this, we need to:

  • Pass some information to the JVM via <jvmarg> tag
  • Tell java task to run in an independent process

Let's illustrate it with the Service Builder task.

Original ant task:

<target name="build-service">
               <java
                       classname="com.liferay.portal.tools.servicebuilder.ServiceBuilder"
                       classpathref="project.classpath"
               >
                       <arg value="-Dexternal-properties=com/liferay/portal/tools/dependencies/portal-tools.properties" />

                       <!-- more main() args go here -->

               </java>

               <delete file="ServiceBuilder.temp" />

               <ant dir="../portal-service" target="compile" inheritAll="false" />
       </target>

To enable debug, simply tell java to use a separate process and set some JVM debug options:

 <target name="build-service">
               <java
                       classname="com.liferay.portal.tools.servicebuilder.ServiceBuilder"
                       classpathref="project.classpath"
                       fork="true"
                       newenvironment="true"
               >
                       <jvmarg  value="-Xdebug" />
                       <jvmarg  value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005" />

                        <!-- more main() args go here -->

               </java>

               <delete file="ServiceBuilder.temp" />

               <ant dir="../portal-service" target="compile" inheritAll="false" />
       </target>

To start a debug session, you must create a "Remote" debug profile using the port of your choice (5005 in this example). After you run the task from your IDE or from command line, you should be aware of the debug listener message in the java log:

~/src/portal-impl$ ant build-services
...
Listening for transport dt_socket at address: 5005

Right after you see this message, start the remote debug session.

If you set breakpoints very close to the beginning of main(), there are chances that your debugging session connects with JVM after reaching them. A more convenient configuration, in case you plan to debug each invocation of a given java task during some time, is to let the JVM wait for some incoming debug connection. By using suspend=y, we are telling the JVM to suspend itself until we start the debugging session:

<jvmarg  value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005" />
0 Attachments
18570 Views
Average (2 Votes)
The average rating is 5.0 stars out of 5.
Comments