Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a token replaced in a file for a Gradle build product?

I have a normal buildscript for Gradle set up, and one thing I want to do is have the version of my build specified. This is the code I've set up to replace the version token in my main Java source file:

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

processResources {
    from (sourceSets.main.java) {
        include 'T145/myproj/Main.java'
        filter(ReplaceTokens, tokens: ['@VERSION@' : project.version])
    }
}

However it doesn't work. I tried using the replace function, but that didn't prove to be a success either. My Main.java has a public variable VERSION that equals @VERSION@, and that is what I want to be replaced.

like image 487
T145 Avatar asked Oct 19 '22 10:10

T145


1 Answers

Based on what I'm seeing in the Gradle manual example of ReplaceTokens, you want to get rid of the @'s in your filter line, so that it reads:

filter(ReplaceTokens, tokens: [VERSION : project.version])

Gradle assumes that the token it's looking for has the @'s delimiting it already, so it is trying to replace @@VERSION@@ instead of @VERSION@, like you want.

like image 64
Brandon McKenzie Avatar answered Oct 21 '22 23:10

Brandon McKenzie