How can I find the name of the ant target being executed?

Let's say I would like to set some variables if the target name ant contains certain words. Is it possible? Can I get the current current target name?

EDIT: I tried to run ant with -d , and I noticed the TEMP_A environment TEMP_A , which seems to contain the current target name. I wonder if this will work in different versions of ant?

+2
source share
4 answers

If you need this information during assembly, you should always determine the target name, because most of the time you are inside the target. Thus, you can add a line of code to the target body, which sets the properties / names according to the name. This will be a purely manual approach.

Perhaps you need some kind of general (ordinary) task, say, a line of code that can be copied to any target and determines the current target name. I do not know how to build a property that provides this name. But if you write a custom task and subclass org.apache.tools.ant.Task , then you can get the name of the "parent" target and build the property.

(If apache published the API on the Internet, I was able to give a more accurate answer ...)

EDIT

It is possible from the outside. You can implement a listener and attach it to ant. In this way, the listener will be notified when the goal is entered. But the problem may be to change the property "inside" ant. I do not know if this listener really "lives" in the same virtual machine as the ant stream.

+2
source

You can create macros using the built-in (with JDK 6) Javascript engine; no additional libraries are required.
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> 

Subsequently, you have a target name available as a property for further processing, i.e. use the task condition (=> contains) to check if the property contains certain rows.
Alternatively, add the logic for "targetname contains specific strings" also for macroform.

To find out what is available to yourself, use:

 <script language="javascript"> for(x in self) {self.log(x);} </script> 
+3
source

There is no built-in property for the current executable name of the target. This is apparently by design; the property was in an earlier version of Ant, but was removed to avoid potential abuse.

Have a look at this question: Print / access the name of the current version of Ant

Also see Ant creators discussing this email thread: link text

+1
source

Another way to do this is through JavaScript with Java Reflection. You can include the following scriptdef file in your ant build file. Then just put <currentTarget/> on 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> 
0
source

Source: https://habr.com/ru/post/1313392/


All Articles