Quick templating with gmaven and GStringTemplateEngine
16.06.2021 by Dirk OlmesAt work I have come across the requirement to generate some files based on the info in a pom.xml
. Maven’s resource filtering feature would be the first thing that comes to mind but unfortunately it’s not powerful enough for my use case. I had to generate a file based on the dependencies that are referenced in the project.
A bit of googling found all kinds of outdated or unsupported maven plugins but nothing that would fit my use case directly. Finally I gave up and started to hack something together in groovy.
As it turns out groovy comes with a templating engine built in: groovy.text.GStringTemplateEngine
. Using it is fairly straightforward from Maven:
....
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${project.basedir}/templateGenerator.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
The templateGenerator.groovy
Script is only a few lines long:
import java.io.File
import groovy.text.GStringTemplateEngine
def templateFile = "${project.basedir}/template.file" as File
def outputFile = "${project.build.directory}/dependencies.html" as File
outputFile.newWriter("UTF-8").withCloseable { writer ->
def engine = new GStringTemplateEngine()
def replacements = [ dependencies: project.dependencies ]
engine.createTemplate(templateFile).make(replacements).writeTo(writer)
}
The template file can contain any syntax that the GStringTemplate supports.
IMHO this approach supports the best of both worlds: with only a little groovy scripting magic you get the maximum flexibility of a templating engine that has access to all the internals of your project.
Comments
There are no comments yet.