ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Connor Arpe <kevina...@gmail.com>
Subject How do I share data between custom Ant tasks?
Date Mon, 25 Oct 2010 16:19:06 GMT
Anters,

I wrote a StackOverflow.com post, but was unable to get help on this issue.
I realise this is not a simple issue.  To encourage others to take a
look, I am willing to contribute a patch if someone can explain the
bug (if it is one).

Since I have been debugging Ant code for the last two weeks (writing
custom Tasks), I might have a chance to contribute the fix.

The story/explanation is below.

If this is the wrong place to ask this question, apologies in advance.
 Please tell me the appropriate location/forum.

Thanks and regards,
Arpe

----
Original Source:
http://stackoverflow.com/questions/3977049/how-do-i-share-data-between-custom-ant-tasks
----	

I wrote two different custom Ant tasks. They are trying to share data
through a static member in a base class. This is not working for me.

I assume I am using static members correctly within Java. I think this
is a dynamic loading issue with the Java VM. However, I am a relative
newbie with Java.

Since Ant custom tasks are mapped at runtime using the taskdef task,
the Ant build engine must dynamically load this code via
java.lang.reflect.Constructor.newInstance().

Is there a trick to make this work?

Note: This works fine in "regular" Java code... -- sharing data
through a static method/member in a base class.  It is the dynamic
loading of Ant that is the issue.

Example classes:

import org.apache.tools.ant.Task;

public class AntCustomTaskShared extends Task {
    private static Integer _static_bigdata = null;
    public Integer get_bigdata() {
        if (_static_bigdata == null) {
            log("alloc");  // from ant Task class
            _static_bigdata = new Integer(0);
        }
        return _static_bigdata;
    }
}

import org.apache.tools.ant.BuildException;

public class AntCustomTask1 extends AntCustomTaskShared {
    public void execute() throws BuildException {
        Integer big_data = get_bigdata();  // "alloc" is printed
        // do stuff with big_data
        log("I'm doing big stuff");
    }
}

import org.apache.tools.ant.BuildException;

public class AntCustomTask2 extends AntCustomTaskShared {
    public void execute() throws BuildException {
        Integer big_data = get_bigdata();  // "alloc" is printed (again)
        // do stuff with big_data
        log("I'm doing big stuff again");
    }
}

Example Ant build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyTask" basedir="." default="init">
   <target name="init"
           description="test the custom task"
   >
      <taskdef name="CustomTask1"
               classname="AntCustomTask1"
               classpath="C:\my_custom_ant_task_class_files"
      />
      <taskdef name="CustomTask2"
               classname="AntCustomTask2"
               classpath="C:\my_custom_ant_task_class_files"
      />
      <CustomTask1/>
      <CustomTask2/>
   </target>
</project>

Do all of the above and you will see "alloc" logged twice. I cannot
get these two custom tasks to share the "big data".

I am running ant 1.8.1 on Windows with these two env vars:

    * JAVA_HOME=C:\Program Files\Java\jdk1.6.0_21
    * CLASSPATH=(empty)

Hint: If you want to step into this custom task from an Ant (1.8)
process, set your breakpoints here:

    * org.apache.tools.ant.launch.Launcher.main()
    * org.apache.tools.ant.UnknownElement.execute()

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


Mime
View raw message