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

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

问题描述

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

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
}

建议.我尝试了很多东西,但没有一个起作用.此外,由于该框架基于testng且使用testNG调用测试用例,因此没有main()方法.

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.

推荐答案

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

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天全站免登陆