18

How can I get the value of the current target ant?

Does it exist a special variable something called TARGET?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
pindare
  • 2,382
  • 6
  • 21
  • 24

5 Answers5

14

Based on the issue you have to patch ant or used javascript:

<target name="test">
  <script language="javascript">
    project.setNewProperty("current_target", self.getOwningTarget());
  </script>
  <echo>${current_target}</echo>
</target>
cxw
  • 16,685
  • 2
  • 45
  • 81
FoxyBOA
  • 5,788
  • 8
  • 48
  • 82
  • Using `setNewProperty()` will treat `${current_target}` as immutable (which is how Ant usually works). If you want to overwrite the value of `${current_target}`, use `setProperty()` instead. – Scribblemacher Sep 22 '16 at 16:45
11

In ant 1.8.2 you can use ${ant.project.invoked-targets}

However, looking at the commit logs http://svn.apache.org/viewvc?view=revision&revision=663061 I'm guessing its been available since 1.7.1

David Laing
  • 7,605
  • 10
  • 33
  • 44
2

My answer, using antcontrib

  <macrodef name="showtargetname">
    <attribute name="property"/>
    <sequential>

      <!-- make temporary variable -->
      <propertycopy name="__tempvar__" from="@{property}"/>

      <!-- Using Javascript functions to convert the string -->
      <script language="javascript"> <![CDATA[
        currValue = [project-name].getThreadTask(java.lang.Thread.currentThread()).getTask().getOwningTarget().getName();
        [project-name].setProperty("__tempvar__", currValue);
      ]]>
      </script>

      <!-- copy result -->
      <var name="@{property}" value="${__tempvar__}"/>

      <!-- remove temp var -->
      <var name="__tempvar__" unset="true"/>

    </sequential>
  </macrodef>

Usage:

<showtargetname property="mycurrenttarget"/>
Marc
  • 524
  • 5
  • 19
1

I think you can't, unless you spend some time coding your own custom tasks (http://ant.apache.org/manual/tutorial-writing-tasks.html)

The built-in properties you can display are: basedir, ant.file, ant.version, ant.project.name, ant.java.version

JuanZe
  • 8,007
  • 44
  • 58
1

If you run ant using the -projecthelp arg:

ant -projecthelp

you will get a listing of the main targets specified in the build.xml (or other build file as declared on the commandline).

akf
  • 38,619
  • 8
  • 86
  • 96