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

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 : The Ant Task - FMPP

<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 freemarker - Using fmpp in gradle to generate java files - Stack Overflow .

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.

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") 
    } 
}

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