From db19df41c4634c8782060fafcffd8b71bb86a171 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
The parent project of your custom task may be accessed through method getProject()
. However, do not call this from the custom task constructor, as the return value will be null. Later, when node attributes or text are set, or method execute()
is called, the Project object is available.
Here are two useful methods from class Project: +
String getProperty(String propertyName)
String replaceProperties(String value)
+ The method replaceProperties()
is discussed further in section Nested Text.
... public class HelloWorld extends Task { + private String message; ... public void addText(String text) { message = text; @@ -308,6 +323,12 @@ public class HelloWorld extends Task { But here properties are not resolved! For resolving properties we have to use Project's replaceProperties(String propname) : String method which takes the property name as argument and returns its value (or ${propname} if not set). ++Debugging + +Thus, to replace properties in the nested node text, our method
+addText()
can be written as:+ public void addText(String text) { + message = getProject().replaceProperties(text); + } +@@ -735,6 +756,21 @@ C:\tmp\anttests\MyFirstTask>
Try running Ant with the flag -verbose
. For more information, try flag -debug
.
For deeper issues, you may need to run the custom task code in a Java debugger. First, get the source for Ant and build it with debugging information.
+Since Ant is a large project, it can be a little tricky to set the right breakpoints. Here are two important breakpoints for version 1.8: +
main()
function: com.apache.tools.ant.launch.Launcher.main()
com.apache.tools.ant.UnknownElement.execute()
If you need to debug when a task attribute or the text is set, begin by debugging into method execute()
of your custom task. Then set breakpoints in other methods. This will ensure the class byte-code has been loaded by the Java VM.
This tutorial and its resources are available via