Ant 中属性文件中的变量 [英] Variables from properties file in Ant
问题描述
我有一个 Android 应用,需要针对不同的环境(例如 UAT、暂存、生产等)构建.每个环境都需要不同的属性(例如,网址、包等).
I have an Android app that needs to be built for different environments (e.g., UAT, staging, production, etc.). Each environment needs different properties (e.g., URLs, packages, etc.).
我想将所有不同的参数放在一个属性文件中,并在每个参数前面加上它匹配的环境.
I would like to put all the different parameters into a single properties file and prefix each parameter with the environment it matches to.
例如,属性文件将包含用于开发环境的 dev.home-url = http://home_dev.com
和 prod.home-url = http://home.com
用于生产环境.
For example, the properties file will contain dev.home-url = http://home_dev.com
for the development environment and prod.home-url = http://home.com
for the production environment.
我使用下面的代码创建一个属性,该属性指向带有 params
前缀的属性文件:
I use the below to create a property that points to the properties file with a prefix of params
:
<property file="parameters.properties" prefix="params" />
为了使用属性,我使用:
And to use a property, I use:
${params.home-url}
当我需要将环境的前缀添加到参数时,问题就来了.它最终看起来像这样,这显然是不可能的:
The problem comes when I need to add the prefix of the environment to the parameter. It would end up looking like this, which obviously can't be done:
${params.${env-prefix}.home-url}
推荐答案
关于 Ant 的一个常见问题是:
A frequently asked question about Ant is:
我该怎么做<property name="prop"value="${${anotherprop}}"/>
(双重扩展属性)?
How can I do something like <property name="prop" value="${${anotherprop}}"/>
(double expanding the property)?
以下 Ant 构建文件的灵感来自该常见问题解答.
The following Ant build file was inspired by that FAQ.
parameters.properties
dev.home-url = http://home_dev.com
prod.home-url = http://home.com
build.xml
<project default="example">
<property name="env-prefix" value="dev" />
<property file="parameters.properties" prefix="params" />
<macrodef name="propertycopy">
<attribute name="name" />
<attribute name="from" />
<sequential>
<property name="@{name}" value="${@{from}}" />
</sequential>
</macrodef>
<target name="example">
<propertycopy name="local.property" from="params.${env-prefix}.home-url" />
<echo>${local.property}</echo>
</target>
</project>
执行 example
任务输出:
Buildfile: /workspace/build.xml
example:
[echo] http://home_dev.com
BUILD SUCCESSFUL
Total time: 405 milliseconds
这篇关于Ant 中属性文件中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!