Hi All,

I have found an interesting problem when one is defining their own task
and this
task is being defined within multiple build.xml files. The problem
happens when
you do the following.

        1 - define a task using the taskdef within a top level build file and 
            supply a classpath

           <taskdef name="foo" classpath="foo.jar" />

        2 - use the ant task to run another build.xml file.

        3 - define the same task within the new project using
            the same/classpath information.

          <taskdef name="foo" classpath="foo.jar" />


This process causes the following error:

        Trying to override old definition of task osjcfp

        BUILD FAILED
        file:/os/dserve/java/build.xml:514: The <osjcfp> task doesn't support
the nested "persistCapable" element.
                at
org.apache.tools.ant.IntrospectionHelper.createElement(IntrospectionHelper.java:521)
                at
org.apache.tools.ant.UnknownElement.handleChildren(UnknownElement.java:239)
                at
org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:122)
                at org.apache.tools.ant.Task.getReplacement(Task.java:401)
                at org.apache.tools.ant.Task.perform(Task.java:354)
                at org.apache.tools.ant.Target.execute(Target.java:309)
                at org.apache.tools.ant.Target.performTasks(Target.java:336)
                at org.apache.tools.ant.Project.executeTarget(Project.java:1339)
                at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:397)
                at org.apache.tools.ant.Task.perform(Task.java:341)
                at org.apache.tools.ant.Target.execute(Target.java:309)
                at org.apache.tools.ant.Target.performTasks(Target.java:336)
                at org.apache.tools.ant.Project.executeTarget(Project.java:1339)
                at 
org.apache.tools.ant.Project.executeTargets(Project.java:1255)
                at org.apache.tools.ant.Main.runBuild(Main.java:609)
                at org.apache.tools.ant.Main.start(Main.java:196)
                at org.apache.tools.ant.Main.main(Main.java:235)

        Total time: 41 seconds

NOTE: I do not get this exception if I try to create the same task
multiple
times within the same top level project. I only get the "Trying ..."
message
and I am also able to use the task. This only happens when I use the ant
task 
and that build process creates a taskdef. Also, the ant task call I used
was simply 

        <ant dir="foo" target="compile" />

I was able to remove this problem by not adding the defined task to the
new project
that ant creates. I changed the Ant initializeProject method from:

        Hashtable taskdefs = getProject().getTaskDefinitions();
        Enumeration et = taskdefs.keys();
        while (et.hasMoreElements()) {
            String taskName = (String) et.nextElement();
            if (taskName.equals("property")) {
                // we have already added this taskdef in #init
                continue;
            }
            Class taskClass = (Class) taskdefs.get(taskName);
            newProject.addTaskDefinition(taskName, taskClass);
        }
to:

        Hashtable taskdefs = getProject().getTaskDefinitions();
        Enumeration et = taskdefs.keys();
        while (et.hasMoreElements()) {
            String taskName = (String) et.nextElement();
            if (taskName.equals("property")) {
                // we have already added this taskdef in #init
                continue;
            }
            Class taskClass = (Class) taskdefs.get(taskName);
            Class newTaskClass = null;
            try {
              newTaskClass = Class.forName( taskClass.getName() );
            } catch( ClassNotFoundException cnfe ) {
              // This is no longer defined within this project.
              continue;
            }
            newProject.addTaskDefinition(taskName, (newTaskClass ==
null) ? taskClass : newTaskClass);
        }

I have tested this tested this solution and it works correctly. I also
wonder if
this will also be a problem with the DataTypeDefinitions that are
propagated to
the new projects?

Ant developers can you tell me if this is a bug or not?

If it is, is this solution that I offered useful?

thanks,

--Claudio

Reply via email to