Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - change Begin/End Token when using ReplaceTokens

Tags:

gradle

I have some files that need token replacement .. instead of the default single @, they use double @'s ... So I need stuff like: @@replaceme@@ .. replaced.

Ant's ReplaceTokens filter allows you to set it using BeginToken / EndToken .. Though I'm not sure how to configure it in my example:

copy{ 
  into something
  from somethingelse
  filter(ReplaceTokens, tokens: [])  
}

How can I change the above code to replace correctly work with double @@ tokens instead of single ones?

like image 625
vicsz Avatar asked Apr 25 '12 19:04

vicsz


3 Answers

It's easy to change the delimiters - just add named parameters beginToken and endToken to the filter method call. However, Ant's ReplaceTokens only support single-character delimiters. A quick web search didn't turn up an Ant FilterReader that supports arbitrary multi-character delimiters out of the box. An alternative is to code this yourself, either by implementing FilterReader or by using the free-form filter method.

like image 85
Peter Niederwieser Avatar answered Nov 12 '22 04:11

Peter Niederwieser


I was in a similar (not exact) situation. Had to process test property resources which were in maven format. i.e '${}' format.

When I moved onto gradle, processTestResources task did not replace the values in ${}. This is because of the same reason that it uses '@' single char delimited. I found a hack to achieve what I wanted.

configure(tasks.processTestResources) {
    include '**/*.properties'
    filter(ReplaceTokens,
            tokens: ['$':'={'], beginToken : '=', endToken : '{')
    filter(ReplaceTokens,
            tokens: [project.properties], beginToken : '{', endToken : '}')
}
like image 42
suman j Avatar answered Nov 12 '22 04:11

suman j


The other answers seem to be old and report that you can't use multi-character token delimiters. But I have found that not to be the case anymore with Gradle 2.14.1

copy{ 
  into something
  from somethingelse
  filter(ReplaceTokens, tokens: [map], beginToken : '@@', endToken : '@@')
  // or, maven prop stle:
  // filter(ReplaceTokens, tokens: [map], beginToken : '${', endToken : '}')

}
like image 8
Patrice M. Avatar answered Nov 12 '22 02:11

Patrice M.