java 如何在 Ant build.xml 中设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27701452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set an env variable in Ant build.xml
提问by Sampada Aggarwal
I want to set an env variable inside my build.xml target
我想在我的 build.xml 目标中设置一个 env 变量
<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>
and Inside my java code, I want to access it using :
在我的 Java 代码中,我想使用以下方法访问它:
if("true".equals(System.getenv("SIMV3.1")){
//do something
}
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.
请建议。我尝试了很多东西,但都没有奏效。此外,没有 main() 方法,因为框架是基于 testng 的,并且使用 testNG 调用测试用例。
回答by Jayan
How are you running your program? If it is using exec with fork, then you can pass new environment to it
你是如何运行你的程序的?如果它使用带有 fork 的 exec,那么您可以将新环境传递给它
https://ant.apache.org/manual/Tasks/exec.html.
https://ant.apache.org/manual/Tasks/exec.html。
Example from the page..
页面中的示例..
<exec executable="emacs">
<env key="DISPLAY" value=":1.0"/>
</exec>
Consider following build.xml file
考虑以下 build.xml 文件
<?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>
and small java program
和小java程序
public class EnvPrint {
public static void main(String[] args) {
System.out.println(System.getenv("SIMV3.1"));
}
}
With out any command line:
没有任何命令行:
$ 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
回答by atish shimpi
Immutability:In ant, properties are immutable:
不变性:在 ant 中,属性是不可变的:
<property name="env.foo" value="your value goes here"/>
won't work.
不会工作。
Mutability:But variables are mutable, so this works:
可变性:但是变量是可变的,所以这是有效的:
<variable name="env.foo" value="your value goes here"/>
Modified Code :
修改后的代码:
<target name="run-tenantManagement" depends="jar">
<variable name="env.SIMV3.1" value="${SIMV3.1}"/>
</target>
回答by Eddie B
Yes you can do this. Place your variable in a build.properties
file and reference it in your build.xml. Then you can pass the variable... But I think it would be much better to use Maven Profilesif you need to have better control over multiple environment configurations.
是的,你可以这样做。将您的变量放在一个build.properties
文件中并在您的 build.xml 中引用它。然后你可以传递变量......但我认为如果你需要更好地控制多个环境配置,使用Maven Profiles会更好。
build.properties
构建属性
var=${val};
build.xml
构建文件
<property file="build.properties"/>
<property name="var" value="${val}"/>
<target name="init">
<echo>${var}</echo>
</target>
CLI
命令行界面
ant -Dvar=value