How to use freemarker in gradle to generate .java files?


(Jiaqi Guan) #1

I’m now planning to migrate my project from Ant to Gradle. And I’d like to convert the ant to Gradle completely.
Previously, I used the FMPP Ant task in ant build.xml to generate it. More details in : http://fmpp.sourceforge.net/ant.html

<target name="build">
    <fmpp
          sourceRoot="src" outputRoot="out"
          data="tdd(data/style.tdd), birds:csv(data/birds.csv)"
    />
</target>

Now I want to use Gradle to do it. I found a way to use the ant.task in gradle like https://stackoverflow.com/questions/13206979/using-fmpp-in-gradle-to-generate-java-files .

task generateFmppSources(dependsOn: ":unix4j-tools:compileJava") << {
    println "Generating sources...."
    ant.taskdef(name:'fmpp', classname:'fmpp.tools.AntTask', classpath:configurations.pmd.asPath);
    ant.fmpp configuration:"src/main/resources/codegen/config.fmpp", sourceRoot:"src/main/resources/codegen/templates", outputRoot:"target/generated-sources/main/java";
}

Is there any other way to do it in Gradle? I mean in a true Gradle way. No more Ant.

Thank you.


(uklance) #2

Tweaking your code to support task caching

task generateFmppSources(dependsOn: ":unix4j-tools:compileJava") {
    inputs.file "src/main/resources/codegen/config.fmpp"
    inputs.dir "src/main/resources/codegen/templates"
    outputs.dir "$buildDir/generated-sources/main/java"
    doLast {
        println "Generating sources...."
        ant.taskdef(name:'fmpp', classname:'fmpp.tools.AntTask', classpath:configurations.pmd.asPath);
        ant.fmpp( configuration:"src/main/resources/codegen/config.fmpp", sourceRoot:"src/main/resources/codegen/templates", outputRoot:"$buildDir/generated-sources/main/java") 
    } 
}

(uklance) #3

There’s a couple of freemarker plugins on the plugin portal