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

发布于 2024-07-21 20:18:49 字数 344 浏览 5 评论 0原文

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

为此,我想在 ant 中定义一个属性,调用它(例如) fileTargetName

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

Help with ant & 使其正常工作值得赞赏。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不奢求什么 2024-07-28 20:18:49

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>

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

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

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>
捎一片雪花 2024-07-28 20:18:49

您不需要为此使用 。 Ant 中的属性是 不可变,因此您可以使用它:

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

如果 RELEASE_VER 环境变量设置后,属性将从环境中获取其值,第二个 语句将不起作用。 否则,该属性将在第一个语句之后取消设置,第二个语句将其值设置为“dev”

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

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

jJeQQOZ5 2024-07-28 20:18:49

我确信有比这更简单的方法,但是怎么样:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文