How can I exchange data between Ant user tasks?

I wrote two different Ant user tasks. They try to exchange data through a static member in the base class. This does not work for me.

I assume that I am using static elements correctly inside Java. I think this is a dynamic boot problem with a Java virtual machine. However, I am a relative newbie with Java.

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

Is there any trick to make this work?

Note. This works well in the "right" Java code ... this is a problem with Ant loading dynamically.

Class examples:

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");
    }
}

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>

, "alloc". " ".

Ant 1.8.1 Windows env vars:

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

. Ant (1.8), :

  • org.apache.tools.ant.launch.Launcher.main()
  • org.apache.tools.ant.UnknownElement.execute()
+5
2

, ( ?) - Ant. ( Ant, !)

loaderref taskdef. ClassLoader ( ). .

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"
               loaderref="my_shared_class_loader"
      />
      <taskdef name="CustomTask2"
               classname="AntCustomTask2"
               classpath="C:\my_custom_ant_task_class_files"
               loaderref="my_shared_class_loader"
      />
      <CustomTask1/>
      <CustomTask2/>
   </target>
</project>
+1

, System.setProperty System.getProperty. ANT Java.

0

All Articles