Java 使用默认值从环境中定义 ant 属性

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

define ant property from environment with default value

javaantenvironment-variablesreleasepackaging

提问by Maxim Veksler

I would like my build script to act properly for release and development environments.

我希望我的构建脚本能够在发布和开发环境中正确运行。

For this I would like to define a property in ant, call it (e.g.) fileTargetName

为此,我想在 ant 中定义一个属性,称之为(例如) fileTargetName

fileTargetNamewill get it's value from the environment variable RELEASE_VERif it's available, if it is not available it will get the default value of dev

fileTargetNameRELEASE_VER如果可用,将从环境变量中获取它的值,如果不可用,它将获取dev的默认值

Help with ant <condition><value></condition>& <property>to get it working is appreciated.

帮助 ant <condition><value></condition>&<property>让它工作是值得赞赏的。

采纳答案by Michael Myers

An example from the Ant documentationof how to get an environment variable into a property:

Ant 文档中有关如何将环境变量放入属性的示例:

<property environment="env"/>
<echo message="Number of Processors = ${env.NUMBER_OF_PROCESSORS}"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>

In your case, you would use ${env.RELEASE_VER}.

在您的情况下,您将使用${env.RELEASE_VER}.

Then for the conditional part, the documentation heresays that there are three possible attributes:

然后对于条件部分,这里的文档说有三个可能的属性:

Attribute  Description                                             Required 
property   The name of the property to set.                        Yes 
value      The value to set the property to. Defaults to "true".   No 
else       The value to set the property to if the condition       No
           evaluates to false. By default the property will
           remain unset. Since Ant 1.6.3

Putting it together:

把它放在一起:

<property environment="env"/>
<condition property="fileTargetName" value="${env.RELEASE_VER}" else="dev">
    <isset property="env.RELEASE_VER" />
</condition>

回答by toolkit

I'm sure there are easier ways than this, but how about:

我相信有比这更简单的方法,但是如何:

<project name="example" default="show-props">

    <property environment="env" />

    <condition property="fileTargetName" value="${env.RELEASE_VER}">
        <isset property="env.RELEASE_VER" />
    </condition>

    <condition property="fileTargetName" value="dev">
        <not>
            <isset property="env.RELEASE_VER" />
        </not>
    </condition>

    <target name="show-props">
        <echo>property is ${fileTargetName}</echo>
    </target>

</project>

回答by Jason Day

You don't need to use a <condition>for this. Properties in Ant are immutable, so you can just use this:

您不需要为此使用 a <condition>。Ant 中的属性是不可变的,所以你可以使用这个:

<property environment="env"/>
<property name="env.RELEASE_VER" value="dev"/>

If the RELEASE_VERenvironment variable is set, then the property will get its value from the environment and the second <property>statement will have no effect. Otherwise, the property will be unset after the first statement, and the second statement will set its value to "dev".

如果RELEASE_VER设置了环境变量,则该属性将从环境中获取其值,第二条<property>语句将无效。否则,该属性将在第一条语句后取消设置,第二条语句将其值设置为"dev"