You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Kichline, Don (EM, PTL)" <Do...@penske.com> on 2004/01/23 13:13:39 UTC

Tasks and TaskContainer

I am attempting to write a set of tasks for ant.  I am using ant 1.6 against
either the sun jdk 1.4.2 or the ibm jdk 1.3.1.  In either case the results
are the same.

In my TaskContainer, I have a public void addTask (Task task ) method.  The
task that is passed in is of type UnknownElement.  Looking at
UnknownElement, there is a method called getTask to retrieve the real
wrappered task.

When ever I call this method, the real task is null.

Any ideas of what I may be doing incorrectly?

I have been looking at example code of other projects, ant-contrib, etc... 

Thanks,

Don Kichline


Here is a sample of my code:

public class ParentTask extends Task implements TaskContainer {
	private List childList = new ArrayList();

	public void addTask(Task task) {
		childList.add(task);
	}

	public void execute() throws BuildException {
		try {
			UnknownElement task = null;
			ChildTask child = null;
			for (Iterator i = .iterator(); i.hasNext();) {
				task = (UnknownElement)i.next();
				child = (ChildTask)task.getTask();
**********			child.someMethod(...);    <===  NullPointer
Exception Here
			}
		}
		catch ( Throwable th ) {
			th.printStackTrace();
		}
		super.execute();
	}
}


public class ChildTask extends Task {
	private String command = null;

	public void addText(String command) {
		this.command = command;
	}

	public void execute() throws BuildException {
		try {
			//	Do Something
		} catch (Throwable th) {
			throw new BuildException(th);
		}

		super.execute();
	}

	public void someMethod(...) {
		//	Set something
	}
}

<project name="test" default="about" basedir=".">
	<taskdef name="parent" 
			 classname="some.package.ParentTask"
			 classpath="bin"/>
			 
	<taskdef name="child" 
			 classname="some.package.ChildTask"
			 classpath="bin"/>
			 
	<target name="about">
		<parent>
			<child>TEST</child>
		</parent>
	</target>			 
</project>

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


Re: Tasks and TaskContainer

Posted by Peter Reilly <pe...@corvil.com>.
The unknownelement needs to be configured - see ant.taskdefs.Antlib for 
an example
or the attached example container.

Peter
Kichline, Don (EM, PTL) wrote:

>I am attempting to write a set of tasks for ant.  I am using ant 1.6 against
>either the sun jdk 1.4.2 or the ibm jdk 1.3.1.  In either case the results
>are the same.
>
>In my TaskContainer, I have a public void addTask (Task task ) method.  The
>task that is passed in is of type UnknownElement.  Looking at
>UnknownElement, there is a method called getTask to retrieve the real
>wrappered task.
>
>When ever I call this method, the real task is null.
>
>Any ideas of what I may be doing incorrectly?
>
>I have been looking at example code of other projects, ant-contrib, etc... 
>
>Thanks,
>
>Don Kichline
>
>
>Here is a sample of my code:
>
>public class ParentTask extends Task implements TaskContainer {
>	private List childList = new ArrayList();
>
>	public void addTask(Task task) {
>		childList.add(task);
>	}
>
>	public void execute() throws BuildException {
>		try {
>			UnknownElement task = null;
>			ChildTask child = null;
>			for (Iterator i = .iterator(); i.hasNext();) {
>				task = (UnknownElement)i.next();
>				child = (ChildTask)task.getTask();
>**********			child.someMethod(...);    <===  NullPointer
>Exception Here
>			}
>		}
>		catch ( Throwable th ) {
>			th.printStackTrace();
>		}
>		super.execute();
>	}
>}
>
>
>public class ChildTask extends Task {
>	private String command = null;
>
>	public void addText(String command) {
>		this.command = command;
>	}
>
>	public void execute() throws BuildException {
>		try {
>			//	Do Something
>		} catch (Throwable th) {
>			throw new BuildException(th);
>		}
>
>		super.execute();
>	}
>
>	public void someMethod(...) {
>		//	Set something
>	}
>}
>
><project name="test" default="about" basedir=".">
>	<taskdef name="parent" 
>			 classname="some.package.ParentTask"
>			 classpath="bin"/>
>			 
>	<taskdef name="child" 
>			 classname="some.package.ChildTask"
>			 classpath="bin"/>
>			 
>	<target name="about">
>		<parent>
>			<child>TEST</child>
>		</parent>
>	</target>			 
></project>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>
>
>  
>