问题描述
目前我了解到我们可以使用 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
gradle 文件:
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:
<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)
}
希望有所帮助.
干杯,雷内
这篇关于Gradle:通过从属性文件中查找令牌来替换令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!