Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Adding Ant Tasks

The purpose of this page is to describe the steps you need to take in order to contribute Ant tasks from your project.

Prepare the Project

  • Create a new source folder in your project to help keep things separate. (src_ant)
  • Create an output folder to hold the compiled JAR. (ant_tasks)

Modify the build.properties

  • We will add entries to the build.properties file to enable PDE/Build to properly compile our Ant tasks.
  • Note that we only have to include the org.eclipse.ant.core bundle on the "extra..." line if we actually reference something from that bundle. (for instance, the constant that refers to the progress monitor)
...
source.ant_tasks/mybundle-ant.jar=src_ant/
bin.includes = ant_tasks/mybundle-ant.jar, ...
jars.compile.order=.,ant_tasks/mybundle-ant.jar
extra.ant_tasks/mybundle-ant.jar = platform:/plugin/org.apache.ant, platform:/plugin/org.eclipse.ant.core
jars.extra.classpath = platform:/plugin/org.apache.ant/lib/ant.jar
...

Write the Code

  • Create a new class which is a sub-class of org.apache.tools.ant.Task.
  • Over-ride the execute() method.
  • Write your code.
  • Note that each element attribute that you wish to have in XML must have a corresponding public void set method that takes a string parameter.

Create the Build Script

  • Create a folder called scripts and a file in it called buildExtraJAR.xml.
  • The contents of the XML file are included below.
  • Note that we add the bundles manually to the classpath... once as the bin/ directory (if they are being built at the same time as us) and once in JAR form.
  • Editor's Note: What if the bundles are in the base but aren't JAR'd?
<?xml version="1.0" encoding="UTF-8"?>
<project name="org.eclipse.mybundle" default="main" basedir="..">
   <target name="main" depends="clean, ant_tasks/mybundle-ant.jar" />

   <target name="init" depends="properties">
      <property name="plugin" value="org.eclipse.mybundle"/>
      <property name="temp.folder" value="${basedir}/temp.folder"/>
      <property name="plugin.destination" value="${basedir}"/>
      <property name="build.result.folder" value="${basedir}/ant_tasks"/>
      <property name="version.suffix" value="_3.1.0"/>
   </target>

   <target name="properties" if="eclipse.running">
      <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
   </target>

   <target name="ant_tasks/mybundle-ant.jar" depends="init" unless="ant_tasks/mybundle-ant.jar">
      <property name="destdir" value="${temp.folder}/ant_tasks/mybundle-ant.jar.bin"/>
      <delete dir="${temp.folder}/ant_tasks/mybundle-ant.jar.bin"/>
      <mkdir dir="${temp.folder}/ant_tasks/mybundle-ant.jar.bin"/>

      <!-- compile the source code -->
      <javac destdir="${temp.folder}/ant_tasks/mybundle-ant.jar.bin" failonerror="true" verbose="true" debug="on">
         <classpath>
            <dirset dir="${basedir}/..">
               <include name="org.eclipse.mybundle/bin"/>
               ...
            </dirset>
            <fileset dir="${eclipse.home}">
               <include name="plugins/org.eclipse.mybundle*.jar"/>
               ...
            </fileset>
         </classpath>
         <src path="src_ant/"/>
      </javac>

      <!-- copy necessary resources -->
      <copy todir="${temp.folder}/ant_tasks/mybundle-ant.jar.bin">
         <fileset dir="src_ant/" excludes="**/*.java"/>
      </copy>
      <mkdir dir="${build.result.folder}"/>
      <jar jarfile="${build.result.folder}/mybundle-ant.jar" basedir="${temp.folder}/ant_tasks/mybundle-ant.jar.bin"/>
      <delete dir="${temp.folder}/ant_tasks/mybundle-ant.jar.bin"/>
      <delete dir="${temp.folder}"/>
   </target>

   <target name="clean" depends="init">
      <delete file="${build.result.folder}/mybundle-ant.jar"/>
      <delete dir="${temp.folder}"/>
   </target>
</project>

Add the Builder to the Project

  • Right-click on the project in the Package Explorer and choose the Properties.
  • In the Builder category select New... -> Ant Builder
  • Name your builder and choose the build file. Its path will look something like: ${workspace_loc:/org.eclipse.mybundle/scripts/buildExtraJAR.xml}
  • In the Refresh tab select Refresh resources upon completion and choose The project containing the selected resource.

Back to the top