如何在 Ant build.xml 中设置环境变量 [英] How to set an env variable in Ant build.xml

查看:68
本文介绍了如何在 Ant build.xml 中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 build.xml 目标中设置一个 env 变量

I want to set an env variable inside my build.xml target

<target name="run-tenantManagement" depends="jar">
   <property name="SIMV3.1" value="${SIMV3.1}" />
    //now here i want to do something like setenv SIMV3.1 true
</target>

在我的java代码中,我想使用:

and Inside my java code, I want to access it using :

if("true".equals(System.getenv("SIMV3.1")){
//do something
}

请建议.我尝试了很多东西,但都没有奏效.此外,没有 main() 方法,因为框架是基于 testng 的,并且使用 testNG 调用测试用例.

Kindly suggest. I have tried many things but none of them worked.Also, there is no main() method as the framework is testng based and test cases are invoked using testNG.

推荐答案

你是如何运行你的程序的?如果它使用带有 fork 的 exec,那么您可以将新环境传递给它

How are you running your program? If it is using exec with fork, then you can pass new environment to it

https://ant.apache.org/manual/Tasks/exec.html.

页面中的示例..

<exec executable="emacs">
  <env key="DISPLAY" value=":1.0"/>
</exec>

考虑以下 build.xml 文件

Consider following build.xml file

<?xml version="1.0"?>
<project name="MyProject" default="myjava" basedir=".">
  <target name="myjava">
    <!--default , if nothing comes from command line -->
    <property name="SIMV3.1" value="mydefaultvalue"/>

    <echo message="Value of SIMV3.1=${SIMV3.1}"/>
    <java fork="true" classname="EnvPrint">
      <env key="SIMV3.1" value="${SIMV3.1}"/>
    </java>
  </target>
</project>

和小java程序

public class EnvPrint {
    public static void main(String[] args) {
        System.out.println(System.getenv("SIMV3.1"));
    }
}

没有任何命令行:

$ ant
Buildfile: C:\build.xml

myjava:
     [echo] Value of SIMV3.1=mydefaultvalue
     [java] mydefaultvalue

来自命令行的一些参数:

With some arguments from command line:

$ ant -DSIMV3.1=commandlineenv
Buildfile: C:\build.xml

myjava:
     [echo] Value of SIMV3.1=commandlineenv
     [java] commandlineenv

这篇关于如何在 Ant build.xml 中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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