简体   繁体   English

如何检查是否在ant脚本中设置了dos环境属性

[英]how to check whether dos environment property is set or not in ant scripting

Ant script 蚂蚁脚本

<property environment="env">
    <if> <equals arg1="${env.PARA} arg2=""/>
    <then>
        <property name="${env.PARA}" value="abc"/>
    <then>
    <if>

    <echo message="${env.PARA}">

Output is 输出是

${env.PARA}

Expected output is 预期输出为

abc

I have not defined the environment variable PARA in dos. 我尚未在dos中定义环境变量PARA How to get the expected output. 如何获得预期的输出。

Note : I am using ant 1.8.2 and antcontrib in windows 7 注意:我在Windows 7中使用ant 1.8.2和antcontrib

Ant properties, once set, are immutable. 设置后,蚂蚁属性是不可变的。 You can therefore just set the property. 因此,您可以只设置属性。

If it had been already set via environment variable, it will have a value and will not be set to "abc" . 如果已经通过环境变量进行了设置,则它将具有一个值,并且不会设置为"abc" If it wasn't set via environment variable, your <property/> statement will be applied. 如果不是通过环境变量设置的,则将应用您的<property/>语句。

<property name="env.PARA" value="abc"/>

<echo message="${env.PARA}"/>

The code below will set the 'port' property to the environment variable 'CATALINA_PORT' if it is defined and to '8080' otherwise: 下面的代码将'port'属性设置为环境变量'CATALINA_PORT'(如果已定义),否则将其设置为'8080':

<property environment="env" />
<condition property="port" value="${env.CATALINA_PORT}" else="8080">
    <isset property="env.CATALINA_PORT"/>
</condition>

The following is the "ANT way" to conditionally set properties. 以下是有条件设置属性的“蚂蚁方法”。

<project name="test" default="run">

    <property environment="env"/>

    <target name="check-prop" unless="${env.PARA}">
        <property name="env.PARA" value="abc"/>
    </target>

    <target name="run" depends="check-prop">
        <echo message="${env.PARA}"/>
    </target>

</project>

Testing 测试中

I'm a linux user, however, it should work the same on windows. 我是linux用户,但是在Windows上应该可以正常使用。

No environment variable 没有环境变量

$ ant
Buildfile: /home/mark/tmp/build.xml

check-prop:

run:
     [echo] abc

BUILD SUCCESSFUL

environment variable 环境变量

$ (export PARA="hello world"; ant)
Buildfile: /home/mark/tmp/build.xml

check-prop:

run:
     [echo] hello world

BUILD SUCCESSFUL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM