java 检查 Ant 脚本中是否设置了环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6247152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:00:59  来源:igfitidea点击:

Check if environment variable has been set in Ant script

javaant

提问by digiarnie

What is the most efficient way of checking if an environment variable has been set prior to executing the rest of an Ant script?

在执行 Ant 脚本的其余部分之前检查是否已设置环境变量的最有效方法是什么?

Let's say my Ant script requires the environment variable "FOO" to be set. I got the following to work, but I was wondering whether there was a less convulated way of achieving the same result:

假设我的 Ant 脚本需要设置环境变量“FOO”。我得到了以下工作,但我想知道是否有一种不那么复杂的方法来实现相同的结果:

<property environment="env"/>
<property name="env.FOO" value=""/>

<target name="my-target">
    <condition property="foo.found">
        <not>
            <equals arg1="${env.FOO}" arg2=""/>
        </not>
    </condition>
    <fail unless="foo.found" message="FOO not set."/>
    <!-- do stuff here that uses the FOO environment variable -->
</target>

回答by matt

Isn't this as simple as:

是不是很简单:

<property environment="env"/>
<fail unless="env.FOO" message="FOO not set."/>

回答by Op De Cirkel

and the other thing you can do (additional to David's) is use

你可以做的另一件事(除了大卫的)是使用

<isset property="env.Foo"/> instead of <equals />

回答by David Harkness

You can shorten it a bit by using an embedded <condition>inside <fail>.

您可以使用嵌入的<condition>inside将其缩短一点<fail>

<property environment="env"/>
<fail message="FOO not set.">
    <condition>
        <isset property="${env.FOO}"/>
    </condition>
</fail>

回答by sgokhales

<property name="test.home.0" value="${env.TEST_HOME}"/>
<condition property="test.home" value="TO_BE_REPLACED">
  <equals arg1="${test.home.0}" arg2="${env.TEST_HOME}"/>
</condition>
<property name="test.home" value="${env.TEST_HOME}"/>

<target name="test">
  <echo>TEST_HOME: ${test.home}</echo>
</target>

回答by David W.

Close:

关闭:

<fail message="FOO not set.">
    <condition>
        <isset property="env.FOO"/>
    </condition>
</fail>

This won't fail if $FOO was set, but is null.

如果设置了 $FOO,这不会失败,但它为空。

回答by eric manley

Here is what I came up with, using the isset property to check for a enviro variable that is only present on Unix. set.properties is the first target to kick this off.

这是我想出的,使用 isset 属性检查仅存在于 Unix 上的环境变量。set.properties 是启动它的第一个目标。

<property environment="env" />        

<target name="init" depends="set.properties" />

    <!--  Do init stuff....  -->
    </target>

    <!-- Other target stuff.....  -->

<!--
        Target: set.properties
--> 
<target name="set.properties"       
 description="Initializes Build Script, checks displays properties"
     depends="cond.hostname.exist,cond.hostname.not.exist">
</target>

<!--
        Target: check.cond HostName is Present
-->     
<target name="cond.check">
    <condition property="cond-is-true">
            <isset property="env.HOSTNAME"/>
    </condition>
</target>   

<!--
        Target: cond.hostname.exist
-->         
<target name="cond.hostname.exist" depends="cond.check" if="cond-is-true">
    <property name="targetboxname"  value="${env.HOSTNAME}" />
</target>   

<!--
        Target: cond.hostname.not.exist
-->             
<target name="cond.hostname.not.exist" depends="cond.check" unless="cond-is-true">
    <property name="targetboxname"  value="${env.COMPUTERNAME}" />
</target>   

    <!-- Then later on....  -->
    <echo>ComputerName/HostName:  ${targetboxname}  </echo>