0
votes

I'm developing an ant script which is calling another ant script using the <ant> task. This ant script is an installer a Java product and is to be used by our customers, who will have ant installed separately.

The script being called uses the antlr task <antlr:ant-antlr3>. To do this I must place the ant-antlr3.jar file in the ant lib directory, as well as adding antlr-3.2.jar to the classpath.

But I don't want to have this dependency of having ant-antl3.jar file in the client's own installed version of ant.

Is there a way of providing the equivalent to ant's command-line '-lib' option to specify other paths for jars to be added to antlib using the <ant> task itself?

I've taken a look at the online docs and there doesn't seem to be a way.

Thanks

2

2 Answers

1
votes

I believe the accepted way to do this is to manually set up your classpath in the build file rather than implicitly including it via the global ant lib directory. i.e.

<path id="master-classpath">
    <fileset dir="${lib}" />
    <fileset file="${findbugs-base}/lib/annotations.jar" />
    <pathelement location="${build-classes}" />
</path>

You can then use this path element in any task that can accept classpath args such as javac

<javac
        destdir="${out}"
        source="1.5"
        target="1.5"
        debug="true">
    <src path="${src}" />
    <classpath refid="master-classpath" />
</javac>

This way, the global ant set up isn't a dependency, and you can specify any files you might need for any build, as specifically as you need to (down to a given call or target).

Obviously, this is all to be carried out in the build file you're calling from the clients' build file. This way, when you call out to yours, the classpath will be set up exactly as you desire.

Another far less idiomatic possibility would be to literally shell out with the Exec Task and call ant that way. Obviously, with the provision of the Ant task, the developers of ant don't recommend you doing that. It is an option, nonetheless.

1
votes

Tim's answer gives most of the story, but in order to run Ant and set JVM options, you'd need to invoke it via the java task.

There is an example of running this way in the Ant docs, here slightly modified to include -lib:

<java
        classname="org.apache.tools.ant.launch.Launcher"
        fork="true"
        failonerror="true"
        dir="${sub.builddir}"
        timeout="4000000"
        taskname="startAnt"
>
    <classpath>
        <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
    </classpath>
    <arg value="-lib"/>
    <arg value="${path.to.your.antlr.jar}"/>
    <arg value="-buildfile"/>
    <arg file="${sub.buildfile}"/>
    <arg value="${sub.target}"/>
</java>