简体   繁体   English

从多个属性文件中读取Ant变量

[英]Reading Ant variables from multiple properties files

I have an Ant buildfile that imports the following at the top of the file: 我有一个Ant构建文件,它在文件顶部导入以下内容:

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

In the project root I have two properties files: build.properties and special-config.properties . 在项目根目录中,我有两个属性文件: build.propertiesspecial-config.properties I am defining a target that needs to read the following property out of special-config.properties : 我正在定义一个需要从special-config.properties读取以下属性的目标:

always.ask=true always.ask =真

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): 所以目标必须是这样的 (只是要记住, build.properties已经被设置为属性文件这一目标不断执行长前):

<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. 但我想知道如何加载第二个属性文件并使其属性(如always.ask )可用于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. 在Ant中,属性的第一个值集 - 属性是悄然不可变的。 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. 稍后 - 也许在第二个文件中,属性在构建期间将具有值“1”。

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 : 这是一个直接从Ant 本地任务的文档中提取的示例:

<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

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

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