Search code examples
gradlepluginsfreemarker

Gradle template processing


I have a gradle build and want to make some text files a bit more dynamic, in the sense that they would always contain the current build's version number. This should be easy peasy once the files are treated as templates and the version number comes as a variable from gradle.

I have tried using Aaron Nies' template plugin like so:

plugins {
  id 'application'
  id "dev.anies.gradle.template" version "0.0.2"
}
...
tasks.register("template", TemplateTask) {
  data += [key: "value"]
  from('src/templates')
  into('build/templates')
}

But Gradle always complains

Could not get unknown property 'TemplateTask' for project ':app' of type org.gradle.api.Project.

I thought to have stuck to the documentation. What may be wrong here?

BTW, I do not have to use Freemarker Templates at this point. If someone shows how to use the builtin template engine that can replace variables I would accept that as answer as well.


Solution

  • Ok, so I got off the ground using Ant's ReplaceTokenFilter. After all I found the relevant line in the documentation:

    https://docs.gradle.org/current/userguide/working_with_files.html#sec:copying_files Example 36 (Filtering files as they are copied) uses the ReplaceTokenFilter. But unlike so many examples I found on the web it also contains the line

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

    which ultimately made it work for me.

    As for the FreeMarkerTemplates I still do not know, but my case is solved.