Invoking an Ant task using a specific JVM

Sometimes you’d like to invoke Ant using a specific JVM, maybe because you want to compile using a well specified JDK implementation.

I solved this problem with a task like this:

<macrodef name="ant-jvm">

   <attribute name="target" />
   <attribute name="jvm" />
   <attribute name="antfile" default="build.xml"/>
   <attribute name="basedir" default="."/>
   <attribute name="args" default="-Dsample.value=xxx"/>
   <attribute name="taskname" default="ant"/>

   <sequential>

      <java classname="org.apache.tools.ant.launch.Launcher"
            fork="true"
            jvm="@{jvm}"
            failonerror="true"
            dir="@{basedir}"
            timeout="99999999"
            taskname="@{taskname}">

        <classpath>
            <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
        </classpath>

        <arg value="-buildfile"/>
        <arg file="@{antfile}"/>
        <arg value="@{args}"/>
        <arg value="@{target}"/>
        <arg value="-Dbasedir=@{basedir}"/>
      </java>

    </sequential>

</macrodef>

You may use it instead of <ant> task and allows you to specify the jvm to use, something like this:

    <ant-jvm target="-do-ejbdoclet" jvm="${java-1.3}/bin/java" />

Quick and dirty, but it works 🙂

Advertisements

One thought on “Invoking an Ant task using a specific JVM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s