Use replacetokens with map


(Ayaka Kashiyama) #1

Hi, I am looking for how to use ant’s replacetoken method with map.

// build.gradle
ext {
    conf = [
      ENV: "foo",
      VERSION: "bar"
  ]
}

processResources {
    conf.each {k, v ->
        filter(ReplaceTokens, tokens : [k : "${v}"])
    }
}

// properties file in the resource directory
test.value=@ENV@
test.value=@VERSION@

Though there is no error message, looks no change in the properties file.
I checked of course the following works. Does anyone know the way to solve this issue?

processResources {
    filter(ReplaceTokens, tokens : [ENV: "foo"])
    filter(ReplaceTokens, tokens : [VERSION: "bar"])
}

(Lance Java) #2

A few points

1.The ReplaceTokens filter takes a map so I’m not sure why you’re trying to split it into separate filter instances. It seems you can achieve your goal with a single filter

2.I doubt you’ll want to run the filter over all resources so you’ll likely want to restrict the filter (otherwise image binaries etc may be corrupted)

processResources {
   filesMatching('**/*.properties') {
      filter(ReplaceTokens, tokens: conf) 
   } 
} 

3.I personally dislike include / exclude by file patterns and prefer separate directories. So I’d put my filtered resources in src/main/resource-templates

processResources {
   from('src/main/resource-templates') {
      filter(ReplaceTokens, tokens: conf) 
   } 
} 

(Ayaka Kashiyama) #3

Thank you for your advise.
I didn’t know that we can set map directly to tokens…
I could confirm now it works.

Also, though there are only properties files in my resources, as you told me, I will move those resources which needs filter to a directory so that I can restrict the filter to it.

It is amazing that I can solve this issue so quickly! Thank you so much!!