Conditional property in Ant properties' file
Solution 1
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>
Solution 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
Solution 3
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
Solution 4
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
BreakPhreak
Updated on July 09, 2022Comments
-
BreakPhreak almost 2 years
Is it possible to set a property value in Ant property files (as opposed to
build.xml
) in a conditional way? For example, ifapache.root
property is set - themy_property
will be${apache.root}/myapp
,/var/www/myapp
otherwise. If not, what would be the common practice - reusablebuild.xml
files? -
BreakPhreak over 11 yearsThank you. However, I was asking about the
property
files, not thebuild.xml
. Or, if I've got you right, iscondition
task the only way to make it happen? -
Vishal over 11 yearsYou can read the property file in the build xml and later can define new property based on concerned properties...
-
BreakPhreak over 11 yearsI 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? -
Dante WWWW over 11 years@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.