Conditional property in Ant properties' file
Asked Answered
J

4

17

Is it possible to set a property value in Ant property files (as opposed to build.xml) in a conditional way? For example, if apache.root property is set - the my_property will be ${apache.root}/myapp, /var/www/myapp otherwise. If not, what would be the common practice - reusable build.xml files?

Joker answered 25/11, 2012 at 17:47 Comment(0)
M
31

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>
Molarity answered 26/11, 2012 at 7:21 Comment(4)
Thank you. However, I was asking about the property files, not the build.xml. Or, if I've got you right, is condition task the only way to make it happen?Joker
You can read the property file in the build xml and later can define new property based on concerned properties...Heiskell
I would like to have "kinda" reusable property files (with conditions) that will be read by several build.xml files. Am I aiming the wrong direction?Joker
@BreakPhreak .properties file doesn't support "condition" -- it's just a pure text file containing key-value pairs. YAML or other config file formats may support condition, but Ant doesn't provide native support to any of them.Diagram
R
2

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. Of course the property directive looks in the current directory as well as home directory. 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

Regimen answered 29/1, 2013 at 19:7 Comment(0)
H
2

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. Here, default install path is /usr/bin/python except for host0 where it /usr/bin/python3.4

Hedda answered 1/3, 2017 at 13:57 Comment(0)
L
1

The OP was asking about a properties file, not within the ant build file. Unfortunately conditionals cannot be done from within the build file. What you can do is have separate property files for each set of dependant properties. For instance:

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

apache.root=/appA

restOfProps.properties

my_property=${apache.root}/myapp
Lewandowski answered 14/8, 2017 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.