How can I find the name of the ant target being executed? [duplicate]
Say I'd like to set some variables if the name of an ant's target contains certain words. Is this possible? Can I obtain the current executed target's name?
EDIT: I tried running ant with -d
, and I noticed an environment variable TEMP_A
which seems to hold the current target's name. I wonder, will this work across different ant versions?
You may create a macrodef using the builtin (since JDK 6) Javascript engine, no extra libraries needed.
something like that :
<project>
<!--
Loglevel defined in org.apache.tools.ant.Project
MSG_ERR = 0;
MSG_WARN = 1;
MSG_INFO = 2;
MSG_VERBOSE = 3;
MSG_DEBUG = 4;
-->
<!-- macrodef creating a property with name of executing target
optional logging to stdout via adjustable loglevel -->
<macrodef name="showtargetname">
<attribute name="property"/>
<attribute name="loglevel" default="3"/>
<sequential>
<script language="javascript">
t = self.getOwningTarget();
self.getProject().setNewProperty("@{property}", t);
self.log("TargetName => " + t, @{loglevel});
</script>
</sequential>
</macrodef>
<target name="foobar">
<showtargetname property="foo"/>
<echo>$${foo} => ${foo}</echo>
</target>
</project>
Afterwards you have the targetname available as property for further processing, f.e. use condition task (=> contains) to check whether the property includes certain strings.
Alternatively add the logic for " targetname contains certain strings " also to the macrodef.
To explore what is available for self, use :
<script language="javascript">
for(x in self) {self.log(x);}
</script>
If you need that information during the build, then you should always be able to determine the target name because most of the time you're inside a target. So one could add a line of code to the target body that sets the property/ies according to the name. That would be the pure manual approach.
Maybe you want some kind of general (custom) task, say a line of code that can be copied to any target and determines the current target name. I don't know of a build in property that provides this name. But if you write a custom task and subclass org.apache.tools.ant.Task
then you may be able to get the name of the 'parent' target and build the property.
(If apache had published the API on the web then I'd been able to give a more precise answer...)
EDIT
It could be possible from outside. One can implement a listener and attach it to ant. So the listener would get notified when a target is entered. But the problem could be to change a property 'inside' ant. I don't know actually if this listener 'lives' in the same VM as the ant thread.
There is no built-in property for the current executing target name. This is apparently by design; the property was present in an earlier version of Ant but was removed to avoid potential abuse.
Check out this question: Print/Access the Name of the Currently Executing Ant Target
Also, see the discussion in this email thread by the Ant creators: link text
Another way to do it is through JavaScript with Java Reflection.
You can include the following scriptdef in your ant build file.
Then just place <currentTarget/>
in any target to display the name.
Obviously, this is a simple example, but it can be used in other ways.
<scriptdef name="currentTarget" language="javascript">
java.lang.System.out.println(project.getThreadTask(java.lang.Thread.currentThread()).getOwningTarget());
</scriptdef>
精彩评论