Question about the task life cycle (one shot or not)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about the task life cycle (one shot or not)

Al Le
Hello,

I have a question about how many times a task is instantiated during a
build. I've read the description at
https://ant.apache.org/manual/develop.html but still a question remains.

[BTW, there is a typo on that page: "addCondifgired"]

The mentioned page describes very well how a task is instantiated and
populated. But in the end it states:

     If target1 and target2 both depend on target3, then running 'ant
     target1 target2' will run all tasks in target3 twice.


Does this mean that the same instance of the task object is used twice?
Or will *two* instances of the task be created?

In other words: Can it happen that the "execute" method of a task
instance is executed more than once?


In the former case, what is a good place to reset all the data in the
task? init()?

In the latter case, there would be no need to worry about it because
each task instance is used exactly once.

Thanks!
AL

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Question about the task life cycle (one shot or not)

Stefan Bodewig
On 2018-02-19, Al Le wrote:

> I have a question about how many times a task is instantiated during a
> build. I've read the description at
> https://ant.apache.org/manual/develop.html but still a question
> remains.

> [BTW, there is a typo on that page: "addCondifgired"]

will be fixed soon, thanks!

> The mentioned page describes very well how a task is instantiated and
> populated. But in the end it states:

>     If target1 and target2 both depend on target3, then running 'ant
>     target1 target2' will run all tasks in target3 twice.


> Does this mean that the same instance of the task object is used
> twice? Or will *two* instances of the task be created?

TBH I think this has changed between releases. At one point in time you
would have had the same instance being used twice and you will find
specific cleanup code that ensured execute could be called twice in some
of the built-in tasks. But now you get two separate instances, at least
this is what my little experiment says:

<project>
  <target name="-check">
    <available property="task-exists?" file="build/org/example/Task.class"/>
  </target>
 
  <target name="-create" unless="task-exists?">
    <mkdir dir="src/org/example"/>
    <echo file="src/org/example/Task.java"><![CDATA[
package org.example;

public class Task {
    public void execute() {
        System.out.println("Task instance : " + this);
    }
}]]></echo>
    <mkdir dir="build"/>
    <javac srcdir="src" destdir="build"/>
  </target>

  <target name="-taskdef" depends="-check,-create">
    <taskdef name="foo" classname="org.example.Task">
      <classpath location="build"/>
    </taskdef>
  </target>

  <target name="log" depends="-taskdef">
    <foo/>
  </target>

  <target name="test1" depends="log"/>
  <target name="test2" depends="log"/>
</project>

> In the former case, what is a good place to reset all the data in the
> task? init()?

The end of execute().

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]