4

How can an ANT task like this one:

<target name="mytask">
  <echo>processing ${blabla}</echo>
</target>

print processing mytask ?

What should I replace blabla with ? Or this is actually even possible ?

Display Name
  • 349
  • 2
  • 10
  • 19
  • What's your use case? Do you just need to print when a task starts/finishes, because Ant can do that already. – skaffman Mar 28 '12 at 12:16
  • No, that was just an example. I want to create a file with the target name. – Display Name Mar 28 '12 at 12:23
  • Is the file-creator in Ant or external (reading the output)? – ewan.chalmers Mar 28 '12 at 12:51
  • It looks like you mean't target rather than task. – martin clayton Mar 28 '12 at 21:41
  • Seems a strange requirement. You want to know the name of the current target so you can use that value, in the same target, to do something (create a file). You are writing the target. It has a name. Type the name again within the body of the target. Why do you need to dynamically discover the name at runtime? – ewan.chalmers Mar 29 '12 at 09:43
  • @sudocode - does this appear strange to you ? I would avoid writing the same code over and over and introduce a pattern for file creation based on task name. What's strange ? – Display Name Apr 02 '12 at 08:29
  • I'm all for code reuse/abstraction in general, but for this case I was wondering how it could be worthwhile. If I compare this: `` with this `` it's hard to see why the first is better. But I shouldn't make assumptions about your requirements. If it works for you, go for it. – ewan.chalmers Apr 02 '12 at 08:33

3 Answers3

4

This might work for you with vanilla Ant, if your version is recent enough to include javascript support.

<scriptdef name="currenttarget" language="javascript">
    <attribute name="property"/>
    <![CDATA[
    importClass( java.lang.Thread );

    project.setProperty(
        attributes.get( "property" ),
        project.getThreadTask(
            Thread.currentThread( ) ).getTask( ).getOwningTarget( ).getName( ) );
    ]]>
</scriptdef>

<target name="foobar">
    <currenttarget property="my_target" />
    <echo message="${my_target}" />
</target>

The scriptdef sets up a task currenttarget that can be used to get the current target in a property, which you can then use as you see fit.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
0

The task name is always output to console by Ant:

Here is an example:

<project default="test">

    <target name="test" depends="mytask-silent, mytask-echo"/>

    <target name="mytask-echo">
        <echo>processing</echo>
    </target>

    <target name="mytask-silent"/>

</project>

Here is the output:

C:\tmp\ant>ant
Buildfile: c:\tmp\ant\build.xml

mytask-silent:

mytask-echo:
     [echo] processing

test:

BUILD SUCCESSFUL
Total time: 0 seconds
ewan.chalmers
  • 16,145
  • 43
  • 60
0

The only way I know it do this is to write an ANT task or use a groovy script as follows:

<target name="mytask" depends="init">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <groovy>
        println "processing ${target}"
    </groovy>
</target>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185