You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Al Le <Al...@gmx.de> on 2016/04/26 08:53:20 UTC

How to create a task associated a target

Hello,

in my ant script, I do the following:

1. there is a target "calledTarget"

2. there is a task "macroTask", defined via "macrodef". This task contains in its body a call to "calledTarget" (via "antcall").

3. there is a target "mainTarget" which contains a task "script" (javascript). In this script, I create an instance of macroTask and then execute it.


Here's the picture as an outline:

<project default="mainTarget">
    
    <target name="mainTarget">
        <script language="javascript">
            var task = project.createTask("macroTask");
            // task.setOwningTarget(project.getTargets().get("mainTarget")); // This must be done to avoid NPE
            task.perform();
        </script>
    </target>

    <macrodef name="macroTask">
        <sequential>
            <antcall target="calledTarget"/>
        </sequential>
    </macrodef>

    <target name="calledTarget">
        <!-- Do something -->
    </target>
    
</project>

When I run the script, I get a NullPointerException. I found out that it occurs in Ant.java, at the line "if (getOwningTarget().getName().equals("")) {" (line number about 380, depending on the ant version).

OK, it's clear that the owning target is not set for the task because the task is created via the project (and not via a target).

Now I have some questions:

1. Is it possible to create a task so that it "automatically" gets associated with a task?

2. Is it possible to find out the name of the current target (or even get the current target as an object) in a script? So that I don't have to write "mainTarget" in the call to "setOwningTarget" in the script above, but rather can do something like getCurrentTarget().


The code above is just an outline. In the real script, I have a rather complicated logics in the javascript, and from the script I set some project properties and call the macro which passes the properties to the called target (to call the target is my real goal; the macroTask is just passing it through).

Thank you
AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


AW: How to create a task associated a target

Posted by "Jan Matèrne (jhm)" <ap...@materne.de>.
Try 
            task.setOwningTarget( self.getOwningTarget() );

'self' is referencing the <script> task and the new created instance will get its owning target.


Jan


> -----Ursprüngliche Nachricht-----
> Von: Al Le [mailto:Al.Le@gmx.de]
> Gesendet: Dienstag, 26. April 2016 08:53
> An: user@ant.apache.org
> Betreff: How to create a task associated a target
> 
> Hello,
> 
> in my ant script, I do the following:
> 
> 1. there is a target "calledTarget"
> 
> 2. there is a task "macroTask", defined via "macrodef". This task
> contains in its body a call to "calledTarget" (via "antcall").
> 
> 3. there is a target "mainTarget" which contains a task "script"
> (javascript). In this script, I create an instance of macroTask and
> then execute it.
> 
> 
> Here's the picture as an outline:
> 
> <project default="mainTarget">
> 
>     <target name="mainTarget">
>         <script language="javascript">
>             var task = project.createTask("macroTask");
>             //
> task.setOwningTarget(project.getTargets().get("mainTarget")); // This
> must be done to avoid NPE
>             task.perform();
>         </script>
>     </target>
> 
>     <macrodef name="macroTask">
>         <sequential>
>             <antcall target="calledTarget"/>
>         </sequential>
>     </macrodef>
> 
>     <target name="calledTarget">
>         <!-- Do something -->
>     </target>
> 
> </project>
> 
> When I run the script, I get a NullPointerException. I found out that
> it occurs in Ant.java, at the line "if
> (getOwningTarget().getName().equals("")) {" (line number about 380,
> depending on the ant version).
> 
> OK, it's clear that the owning target is not set for the task because
> the task is created via the project (and not via a target).
> 
> Now I have some questions:
> 
> 1. Is it possible to create a task so that it "automatically" gets
> associated with a task?
> 
> 2. Is it possible to find out the name of the current target (or even
> get the current target as an object) in a script? So that I don't have
> to write "mainTarget" in the call to "setOwningTarget" in the script
> above, but rather can do something like getCurrentTarget().
> 
> 
> The code above is just an outline. In the real script, I have a rather
> complicated logics in the javascript, and from the script I set some
> project properties and call the macro which passes the properties to
> the called target (to call the target is my real goal; the macroTask is
> just passing it through).
> 
> Thank you
> AL
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional
> commands, e-mail: user-help@ant.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org