4

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.

T145
  • 1,118
  • 1
  • 9
  • 28

1 Answers1

2

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.

Brandon McKenzie
  • 1,555
  • 12
  • 25
  • Well, I changed that but it still doesn't work for some reason. I even changed the task from `processResources` to `jar`, since that is the task that specifically handles outputting the release. – T145 Jun 03 '15 at 19:12
  • Did you try putting the filter line outside of the from closure? Every example of filter I've seen with filter does not put it into a closure with from. – Brandon McKenzie Jun 04 '15 at 15:17
  • for me it replaces token but project.version is unspecified – JJ Roman Jun 22 '20 at 18:44