Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3310616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:46:51+00:00 2026-05-17T21:46:51+00:00

I wrote two different custom Ant tasks. They are trying to share data through

  • 0

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… 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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:46:51+00:00Added an answer on May 17, 2026 at 9:46 pm

    The solution is simple, but poorly (or not?) documented on the Ant website. (I will commit a patch to the Ant docs to correct this situation!)

    I needed to use the attribute loaderref on taskdef nodes. The token is used to reference the ClassLoader (and share it). After, static members are shared between custom tasks correctly.

    Corrected 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote an application that loops through a set of records and prints two
I am trying to write a static function to Or two expressions, but recieve
In my code I use a small data-storing class, which is created in different
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I'm trying to write a query that will pull back the two most recent
I'm trying to write a simple WPF app that has two ellipses, joined by
I wrote a windows service using VB that read some legacy data from Visual
I need to configure a property of a custom control I wrote, specifically before
I'm creating a Custom tag in my web.config. I first wrote the following entry
I've got two ASP.Net applications residing in two different folders on my server: /Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.