Gradle:通过从属性文件中找到令牌来替换令牌 [英] Gradle: Replace Tokens by finding the tokens from property file
问题描述
目前,我了解我们可以在构建过程中通过以下方式使用org.apache.tools.ant.filters.ReplaceTokens
替换文件的内容.
Currently I understand that we can use org.apache.tools.ant.filters.ReplaceTokens
to replace the contents of a file during build in the following way.
myBeans.xml
:
<bean id="mybean1" class="com.test.MyClass1" >
<property name="myprop1" value="@myproperty@" />
</bean>
my.properties
:
myprop=testme
渐变文件:
from file("myBeans.xml"), {
filter(ReplaceTokens, tokens: ["myproperty": project.properties["myprop"]])
}
但是,我希望gradle从my.properties
文件中找到属性名称,并将其替换为xml文件(在过滤器中未提及myprop
).如果没有,我将必须手动添加所有PlaceHolders
.
However I would want gradle to find the property names from my.properties
file and replace it in the xml file (without mentioning myprop
in the filter). If not, I would have to add all the PlaceHolders
manually.
推荐答案
,您可以将属性作为映射传递给ReplaceTokens配置.密钥必须与您要替换的令牌匹配.示例:
you can pass properties as a map to the ReplaceTokens configuration. The key must match the token you want to see replaced. Example:
beans.xml:
beans.xml:
<bean id="mybean1" class="com.test.MyClass1" >
<property name="myprop1" value="@myproperty@" />
</bean>
my.properties:
my.properties:
myproperty=testme
build.gradle:
build.gradle:
task myCopy(type:Copy){
from "bean.xml"
into ("$buildDir/beans")
def myProps = new Properties()
file("my.properties").withInputStream{
myProps.load(it);
}
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: myProps)
}
希望有帮助.
欢呼声, 雷内(René)
cheers, René
这篇关于Gradle:通过从属性文件中找到令牌来替换令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!