简体   繁体   English

Ant属性文件中的条件属性

[英]Conditional property in Ant properties' file

Is it possible to set a property value in Ant property files (as opposed to build.xml ) in a conditional way? 是否可以有条件地在Ant 属性文件中设置属性值(与build.xml相反)? For example, if apache.root property is set - the my_property will be ${apache.root}/myapp , /var/www/myapp otherwise. 例如,如果设置了apache.root属性my_property将为${apache.root}/myapp ,否则为/var/www/myapp If not, what would be the common practice - reusable build.xml files? 如果没有,通常的做法是-可重用的build.xml文件?

Use the condition task: 使用条件任务:

<project name="demo" default="run">

    <condition property="my_property" value="${apache.root}/myapp" else="/var/www/myapp">
        <isset property="apache.root"/>
    </condition>

    <target name="run">
        <echo message="my_property=${my_property}"/>
    </target>

</project>

You can include different property files based on environments or the conditional variables. 您可以基于环境或条件变量包括不同的属性文件。 For example 例如

    <echo>Building ${ant.project.name} on OS: ${os.name}-${os.arch}</echo>
<property file="build-${os.name}.properties" />

this would include a file named 'build-Windows 7.properties' or 'build-Linux.properties' depending on where the build is being run. 其中将包含一个名为“ build-Windows 7.properties”或“ build-Linux.properties”的文件,具体取决于运行构建的位置。 Of course the property directive looks in the current directory as well as home directory. 当然,property指令会在当前目录和主目录中查找。 So the property file could be a part of the build source or in the home directory of the build account. 因此,属性文件可以是构建源的一部分,也可以是构建帐户的主目录中的一部分。

You can use the condition tag to generate part of the name of the property file as well to select 您还可以使用条件标签来生成属性文件名称的一部分,然后选择

One of the simplest form of condition you can use is: 您可以使用的最简单的条件形式之一是:

<exec executable="hostname" outputproperty="hostname"/>
<condition property="python" value="/usr/bin/python3.4">
  <equals arg1="${hostname}"    arg2="host0"/>
</condition>
<property name="python" value="/usr/bin/python"/>

to accommodate different python installation path for example. 例如,以适应不同的python安装路径。 Here, default install path is /usr/bin/python except for host0 where it /usr/bin/python3.4 在这里,默认的安装路径是/ usr / bin / python,除了host0以外是/usr/bin/python3.4

The OP was asking about a properties file, not within the ant build file. OP询问的是属性文件,而不是ant构建文件中的文件。 Unfortunately conditionals cannot be done from within a property file. 不幸的是,不能从属性文件中完成条件。 What you can do is have separate property files for each set of dependant properties. 您可以做的是为每组相关属性设置单独的属性文件。 For instance: 例如:

Build.xml Build.xml

<condition property="app.name" value="appA">
    <equals arg1="${appName}" arg2="A" />
</condition>
<condition property="app.name" value="appB">
    <equals arg1="${appName}" arg2="B" />
</condition>
<property file="${app.name}.properties" />
<!-- since properties are immutable, set your defaults here -->
<property name="apache.root" value="/var" />
<property file="restOfProps.properties" />

appA.properties appA.properties

apache.root=/appA

restOfProps.properties restOfProps.properties

my_property=${apache.root}/myapp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM