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: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org

Reply via email to