簡體   English   中英

使用默認值定義環境中的ant屬性

[英]define ant property from environment with default value

我希望我的構建腳本能夠在發布和開發環境中正常運行。

為此,我想在ant中定義一個屬性,調用它(例如) fileTargetName

fileTargetName將從環境變量RELEASE_VER獲取它的值,如果它可用,如果它不可用,它將獲得默認的dev

幫助ant <condition><value></condition><property>讓它工作表示贊賞。

Ant文檔中有關如何將環境變量導入屬性的示例:

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

在您的情況下,您將使用${env.RELEASE_VER}

然后對於條件部分, 這里的文檔說有三個可能的屬性:

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

把它放在一起:

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

您不需要為此使用<condition> Ant中的屬性是不可變的 ,因此您可以使用它:

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

如果設置了RELEASE_VER環境變量,則該屬性將從環境中獲取其值,並且第二個<property>語句將不起作用。 否則,在第一個語句之后將取消設置該屬性,第二個語句將其值設置為"dev"

我相信有比這更簡單的方法,但是如何:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM