[英]How to set an env variable in Ant build.xml
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.
我尝试了很多东西,但是都没有用。此外,由于框架基于testng并且使用testNG调用测试用例,因此没有main()方法。
How are you running your program? 您如何运行程序? If it is using exec with fork, then you can pass new environment to it
如果它使用exec与fork,那么您可以将新环境传递给它
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
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>
Yes you can do this. 是的,您可以这样做。 Place your variable in a
build.properties
file and reference it in your build.xml. 将变量放在
build.properties
文件中,并在build.xml中引用它。 Then you can pass the variable... But I think it would be much better to use Maven Profiles if you need to have better control over multiple environment configurations. 然后,您可以传递变量...但是我认为,如果需要更好地控制多个环境配置,使用Maven配置文件会更好。
var=${val};
<property file="build.properties"/>
<property name="var" value="${val}"/>
<target name="init">
<echo>${var}</echo>
</target>
ant -Dvar=value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.