在不同的jvm中运行ant任务 [英] Run ant task in different jvm

查看:37
本文介绍了在不同的jvm中运行ant任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 ant 构建是使用 Java 1.7.0 for JAVA_HOME 运行的.这样 javac 和所有其他 Java 依赖目标默认使用正确的 Java.

Our ant build is run using Java 1.7.0 for JAVA_HOME. This way javac and all other Java dependent targets use the correct Java by default.

但是来自外部供应商的 1 个 ant 目标不支持(或者说存在错误)使用 Java 1.7.0.而且不像例如javac或者fork junit,这个target不支持参数切换jvm.

But 1 ant target from an external supplier does not support (or rather has a bug) using Java 1.7.0. And unlike e.g. javac or a forked junit, this target does not support parameters to switch jvm.

是否可以在不同的 jvm 中运行特定的 ant 目标?

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

推荐答案

制作 Jeanne Boyarsky 的建议使用exec Ant 任务具体,如下示例将 exec 任务包装在一个宏中,以简化使用各种 JVM 调用目标.请注意,JVM 是使用 Ant 环境变量设置的 JAVACMD.

To make Jeanne Boyarsky's suggestion of using the exec Ant task concrete, the following example wraps the exec task in a macro to simplify calling targets with various JVMs. Notice that the JVM is set using the Ant environment variable JAVACMD.

<?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>

输出

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

这篇关于在不同的jvm中运行ant任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆