Run ant task in different jvm

Our ant build is done using Java 1.7.0 for JAVA_HOME. Thus, javac and all other Java dependent targets use the correct Java by default.

But 1 ant target from an external provider does not support (or rather has an error) using Java 1.7.0. And unlike, for example, javac or a branched unit, this target does not support parameters for switching jvm.

Is it possible to run a specific ant target in another jvm?

+6
source share
3 answers

To make an offer from Jeanne Boyarsky to use the exec Ant task specific, the following example completes the exec task in a macro to make it easier to call targets using different JVMs. Note that the JVM is installed using the Ant JAVACMD environment variable.

Project example

 <?xml version="1.0" encoding="UTF-8"?> <project name="run-target-with-specified-java-version" default="test"> <macrodef name="exec-target"> <attribute name="antfile" default="${ant.file}" /> <attribute name="target" /> <attribute name="jvm" default="${java.home}/bin/java" /> <sequential> <exec executable="ant"> <env key="JAVACMD" value="@{jvm}" /> <arg line='-f "@{antfile}"' /> <arg line="@{target}" /> </exec> </sequential> </macrodef> <target name="echo-java-version"> <echo message="Java version: ${java.version}" /> </target> <target name="test"> <exec-target target="echo-java-version" /> <property name="java1.6" location="/usr/lib/jvm/jdk1.6/bin/java" /> <exec-target target="echo-java-version" jvm="${java1.6}" /> </target> </project> 

Exit

 test: [exec] Buildfile: /home/your/project/build.xml [exec] [exec] echo-java-version: [exec] [echo] Java version: 1.7.0 [exec] [exec] BUILD SUCCESSFUL [exec] Total time: 0 seconds [exec] Buildfile: /home/your/project/build.xml [exec] [exec] echo-java-version: [exec] [echo] Java version: 1.6.0 [exec] [exec] BUILD SUCCESSFUL [exec] Total time: 0 seconds BUILD SUCCESSFUL Total time: 2 seconds 
+17
source

You can use the exec task to run the assembly file for the purpose specified to run as a parameter. It may work in another JVM, since you can pass the JVM to this exec call.

Note that you will have to reorganize the goal to rely on files for communication, rather than setting properties. Since this will be in another JVM, it is obvious that it cannot rely on memory.

+3
source

You can run the target in another JVM (we do this all the time). You just need to use fork:

  <javac srcdir="${src}" destdir="${build}" fork="yes" /> 

But I feel that you know this, so what about running the external ANT task as it is, and the rest (let's say you have 3 more javac tasks) in the JVM that you want. This can be achieved by setting the properties file. See javac task

You can use different compilers. This can be set by setting the global property build.compiler, which will affect all tasks within the assembly.

Thus, this property will affect your 3 tasks and run them in the JVM you specify (say 1.7), and you can set JAVA_HOME by default for any task in your external library.

+3
source

Source: https://habr.com/ru/post/922842/


All Articles