TaskHelper
The use of Ant Task objects from within another Task is actually quite easy--so long as you initialize the new object correctly. While this might not seem like a big problem, the number of times the problem was encountered while creating Craftsman
was enough to bring about TaskHelper and the rest of rsh-ant.
TaskHelper performs the following tasks for you, in order:
- task.setProject(project)
- task.setTaskType(name)
- task.setTaskName(type) (defaults to name if you do not specify a value)
- task.init()
This guarantees the Task is initialized like it would be if found in an Ant build.xml file. Usage is very straightforward:
Project project = getProject(); // get the project from the enclosing task ... Copy copyTask = new Copy(); TaskHelper.setUpTask(copyTask, project, "mycopy");Even easier, and usually closer to what you want the output to look like is the following:
// get the project from the enclosing task ... Project project = getProject(); Copy copyTask = new Copy(); TaskHelper.setUpTask(copyTask, project, getTaskName());This sets the name of the copyTask to the name of the enclosing task. When the copy executes, all the copy output will be prefaced with the name of the enclosing task--making the output more uniform.
You can also set the type String for the Task by supplying the type as an additional parameter:
TaskHelper.setUpTask(copyTask, project, getTaskName(), getTaskType());You will usually not need to set this value, but it is there if you need it for some reason. If you call the method without the task type, it will just use the name.
/rsh tech

