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

时间:2022-06-01 21:08:59

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

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

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

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

帮助ant & 使其工作。

3 个解决方案

#1


72  

An example from the Ant documentation of 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 here says 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>

#2


38  

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

这里不需要使用 <条件> 。Ant中的属性是不可变的,所以您可以这样使用:

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

If the RELEASE_VER environment 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环境变量,则属性将从环境中获取其值,第二个 语句将无效。否则,在第一个语句之后,属性将被取消,第二个语句将将其值设置为“dev”。

#3


0  

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>

#1


72  

An example from the Ant documentation of 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 here says 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>

#2


38  

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

这里不需要使用 <条件> 。Ant中的属性是不可变的,所以您可以这样使用:

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

If the RELEASE_VER environment 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环境变量,则属性将从环境中获取其值,第二个 语句将无效。否则,在第一个语句之后,属性将被取消,第二个语句将将其值设置为“dev”。

#3


0  

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>