Thursday, 28 February 2013

Ant tasks with Eclipse

When managing an eclipse project and building it with ANT it can be a real pain to constantly manage the class paths in the ANT file and your project. The EBuilder ant tasks make this easier.

There is a project on source forge (http://ebuild.firefang.net/) which does this nicely however there are a couple of flaws in that version and I have created a new release (Sourceforge version TBA post a request to me and I will send it to you).

After downloading and placing the JAR into your ANT_HOME\lib directory, the way the project works is the typical task definition:

<taskdef       resource="firefang_ant.properties"      classpathref="ant.lib.classpath" />

... followed by a series of tasks the most useful of which is the eclipse_cp  task.
This will read a specified .classpath file from your project and scan the entries it finds there.
ie.

<eclipse_cp action="libs" rootdir="${project.dir}" project="." result="proj.libs" />

See Eclipse Help

Your .classpath will look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" path="test"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="conf"/>
    <classpathentry kind="con" path="org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?ivyXmlPath=ivy.xml&amp;confs=*"/>
    <classpathentry kind="output" path="build"/>
</classpath>

The action attribute on the task can be any of the following values:
lib : Which reads the library entries;
src: Which reads the source entries;
output : Which reads the library entries;

This produces a ';' separated list which can be used directly as a class path or it can have the libaries turned into fully qualified paths (the next iteration of the project).

ie.

<for list="${proj.libs}" param="lib" delimiter=";">
                    <sequential>
                        <echo message="    checking @{lib}"/>
                        <!--
                        <var name="build.classpath" value="${build.classpath};${project.dir}/@{lib}" />
                        -->
                        <if>
                            <available file="@{lib}"/>
                            <then>
                                <var name="build.classpath" value="${build.classpath};@{lib}" />
                            </then>
                            <else>
                                <var name="build.classpath" value="${build.classpath};${project.dir}/@{lib}" />
                            </else>
                        </if>
                       
                    </sequential>
                </for>


NB. The above uses ant-contrib v1 (http://ant-contrib.sourceforge.net/)






No comments:

Post a Comment