11

目前我知道我们可以用org.apache.tools.ant.filters.ReplaceTokens以下方式在构建期间替换文件的内容。

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手动添加所有内容。

4

1 回答 1

25

您可以将属性作为映射传递给 ReplaceTokens 配置。密钥必须与您希望替换的令牌匹配。例子:

豆类.xml:

<bean id="mybean1" class="com.test.MyClass1" >
    <property name="myprop1" value="@myproperty@" />
</bean>

我的属性:

myproperty=testme

构建.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)
}

希望有帮助。

干杯,勒内

于 2012-10-10T07:59:16.280 回答