Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Replace Tokens by finding the tokens from property file

Tags:

gradle

build

ant

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 file:

from file("myBeans.xml"), {
   filter(ReplaceTokens, tokens: ["myproperty": project.properties["myprop"]])
}

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.

like image 861
Chris Avatar asked Oct 10 '12 07:10

Chris


1 Answers

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:

myproperty=testme

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)
}

hope that helped.

cheers, René

like image 163
Rene Groeschke Avatar answered Sep 25 '22 18:09

Rene Groeschke