Contrib tasks
Lets see how we can create a contrib tasks in ANT first and then we proceed to custom tasks. To create contrib tasks in ANT first we have to create ant-contrib plug in and drop it in target platform's directory. This topic meant for advanced Eclipse Plugin Developers.
Download the ant-contrib jar to some location in local machine.
Create a new plugin project using eclipse.
Create a lib folder parallel to META-INF file and, place the ant-contrib jar over there.
Goto Eclipse plugin editor, and add lib/ant-contrib.jar to the bundle classpath.
Once you are done here, add org eclipse ant core as dependent plugin by going to dependents tab.This will help in identifying extensions of ant core defined in ant core plugin.
We will be using ExtraClassPathEntries extension and AntTask extension.
Curretnly ant-contrib defines the following tasks:
for foreach with parallel execution benefit
For each task that you want to use, define a mapping of task against classname defined in the ant-contrib jar for that task.
After defining all the need tasks that you want to use in your build system, add the lib/ant-contrib.jar in to the extra class path entries section of the manifest file.
Here is a sample and partial manifest file after above change.
<extension point="org.eclipse.ant.core.antTasks">
<antTask
library="lib/ant-contrib.jar"
name="foreach"
class="net.sf.antcontrib.logic.ForEach">
</antTask>
<antTask
library="lib/ant-contrib.jar"
name="for"
class="net.sf.antcontrib.logic.For">
</antTask>
</extension>
<extension point="org.eclipse.ant.core.extraClasspathEntries">
<extraClasspathEntry
library="lib/ant-contrib.jar">
</extraClasspathEntry>
</extension>
If everything is configured properly, you should be able to pick the defined ant tasks in the ant contrib jar from the Eclipse's plugin editor extension tab.
Once all of the tasks are added in the extension section of the manifest, the very important step is to remove the lib/ant-contrib.jar from the runtime bundle classpath. This is to make sure, we do not include ant-contrib.jar into the class path of the manifest classloader.
Note that all the task defined in ant-contrib.jar need to be loaded by Ant class loader defined in org eclipse ant core plugin. Otherwise you will see all kinds weird error messages from the ant builder in eclipse plugins.
At this point we are ready to export this project as ant contrib plugin. Goto File menu, select export and export the project as deployable plugin. Select any destination directory. Once exported you will have plugin with ant-contrib jar inside.
How to use this plugin:
Drop this jar into the plugin's folder of your target platform's build system. Modify build.xml to define ant contrib xmlns as follows..
build.xml sample -
<project default="some.default.target" xmlns:ac="antlib:net.sf.antcontrib">
...
..
</project>
Note that above defined as prefix namespace for antcontrib.
Now use ac prefix to call the relevant ant contrib tasks anywhere in the build.xml
Sample is like the following:
<target name="iterate">
<ac:for param="file">
<fileset dir="."/>
<sequential>
<echo message="- @{file}"/>
</sequential>
</antcontrib:for>
</target>
At this point, we should be able to run the build successfully calling tasks defined in ant contrib.
CUSTOM TASKS
Steps to create custom ANT tasks.
1. Create a new Java Project in eclipse. Right click on Project->Properties->Java Build Path->Add external Jar. Browse to Ant.jar file in ANT_HOME/lib folder.
2.Make a new class say Hello which has to extend class org.apache.tools.ant.Task
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
public class Hello extends Task
{
private String retproperty = null;
private String msg;
public String getRetProperty() {
return retproperty;
}
public void setRetProperty(String property) {
this.retproperty = property;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void execute() throws Exception {
System.out.println(msg);
Project project = getProject();
project.setProperty(retproperty,
"Value Returned By Custom task");
}
}
3. Once you have the java code ready in eclipse click on File=>Export=>Choose Jar and export to any suitable location on your system
4. Now it’s time to write Ant script which will call this custom task pass a value and also read the value returned by the task.So write a similar Ant script. Here classpath should be location where jar in Step 3 was exported.
<project name="CustomTask" default="build" basedir=".">
<taskdef name="Hello" classpath="../../CustomTask.jar" classname="HelloWorld">
<target name="build">
<helloworld retproperty="val" msg=" Custom Ant Task">
<echo message="${val}">
</echo></helloworld></target>
</taskdef></project>
5.Now we can run the Ant script through command prompt or eclipse as per convenience.
<span style="font-family: Arial;">
<hello retproperty="val" msg="Custom Ant Task">
</hello>
</span>
This executes method of the HelloWorld class is invoked and msg variable is initialised with value passed. Similarly
project.setProperty(retproperty,"Value Returned By Custom task");
The above line sets the value into property value which is again defined in ret property. So that in Ant script variable val will hold the value returned or set inside the class so we can use it for further processing.
No comments:
Post a Comment