eastwater
eastwater

Reputation: 5588

Gradle replaceToken for any string without @@

Gradle replacing a string in text files:

 foo --> bar

import org.apache.tools.ant.filters.ReplaceTokens

ReplaceTokens requires tokens to be in the format: @token@.

But in ant, a token be anything without @@.

from ('/path') {
    filter(ReplaceTokens, tokens:["foo", "bar"])
}

It won't work. Replacing line by line is the solution?

from ('/path') {
    filter { String line -> line.replaceAll("foo", "bar") }
}

Performance issue for a big file?

Upvotes: 0

Views: 922

Answers (1)

Vampire
Vampire

Reputation: 38669

You should be able to configure it the same as in Ant, as it is the exact same class. So the following should probably work:

from ('/path') {
    filter(ReplaceTokens, beginToken: '', endToken: '', tokens:["foo", "bar"])
}

But it doesn't make much different probably than going through the file line by line, so I guess performancewise it doesn't make much difference.

One thing you should remember though. Whenever you use content filtering, set the filteringCharset, at least if you potentially have non-ASCII characters in your file, otherwise the system default encoding is used and this can destroy the files content, depending on which system you build.

Upvotes: 1

Related Questions