简体   繁体   中英

Reading Ant variables from multiple properties files

I have an Ant buildfile that imports the following at the top of the file:

<project name="..." ...>
    <property file="build.properties"/>
    ...

In the project root I have two properties files: build.properties and special-config.properties . I am defining a target that needs to read the following property out of special-config.properties :

always.ask=true

So the target needs to be something like this (just keep in mind that build.properties was already set as the property file long before this target ever executes):

<target name="exec-special-logic">
    <!-- Somehow read special-config.properties#always.ask and set it to a local variable... -->
</target>

But I am ansure of how to load this 2nd property file and make its properties (such as always.ask ) available to Ant. Thanks in advance.

You can read properties from as many different files as you like, so you could have

<property file="build.properties"/>
<property file="build.properties.part2"/>

And so on. In Ant the first value set for a property sticks - properties are quietly immutable. Hence if you have:

<property name="my.prop" value="one" />

somewhere in the first file and

<property name="my.prop" value="two" />

later - perhaps in the second file, the property will have the value "one" for the duration of the build.

A feature of recent versions is that properties can be localised to an execution block - this lets you "get around" the immutability. Here's an example lifted straight from the docs for the Ant local task :

<property name="foo" value="foo"/>

<target name="step1">
    <echo>Before local: foo is ${foo}</echo>
    <local name="foo"/>
    <property name="foo" value="bar"/>
    <echo>After local: foo is ${foo}</echo>
</target>

<target name="step2" depends="step1">
    <echo>In step2: foo is ${foo}</echo>
</target>

outputs

step1:
     [echo] Before local: foo is foo
     [echo] After local: foo is bar

step2:
     [echo] In step2: foo is foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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