annotate content/Maven/gmaven-string-template-engine.md @ 109:ee048ed76ea1

Datums- und Syntex Fix
author Dirk Olmes <dirk.olmes@codedo.de>
date Fri, 18 Jun 2021 07:24:04 +0200
parents 712e2e9cf8b1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
107
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
1 Title: Quick templating with gmaven and GStringTemplateEngine
109
ee048ed76ea1 Datums- und Syntex Fix
Dirk Olmes <dirk.olmes@codedo.de>
parents: 107
diff changeset
2 Date: 2021-06-16
107
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
3 Lang: en
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
4
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
5 At work I have come across the requirement to generate some files based on the info in a `pom.xml`. Maven's [resource filtering](https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html) 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.
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
6
109
ee048ed76ea1 Datums- und Syntex Fix
Dirk Olmes <dirk.olmes@codedo.de>
parents: 107
diff changeset
7 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.
107
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
8
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
9 As it turns out groovy comes with a templating engine built in: `groovy.text.GStringTemplateEngine`. Using it is fairly straightforward from Maven:
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
10
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
11 :::xml
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
12 ....
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
13 <plugin>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
14 <groupId>org.codehaus.gmaven</groupId>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
15 <artifactId>groovy-maven-plugin</artifactId>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
16 <version>2.1.1</version>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
17 <executions>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
18 <execution>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
19 <phase>generate-resources</phase>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
20 <goals>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
21 <goal>execute</goal>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
22 </goals>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
23 <configuration>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
24 <source>${project.basedir}/templateGenerator.groovy</source>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
25 </configuration>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
26 </execution>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
27 </executions>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
28 </plugin>
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
29
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
30 The `templateGenerator.groovy` Script is only a few lines long:
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
31
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
32 :::java
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
33 import java.io.File
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
34 import groovy.text.GStringTemplateEngine
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
35
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
36 def templateFile = "${project.basedir}/template.file" as File
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
37 def outputFile = "${project.build.directory}/dependencies.html" as File
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
38 outputFile.newWriter("UTF-8").withCloseable { writer ->
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
39 def engine = new GStringTemplateEngine()
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
40 def replacements = [ dependencies: project.dependencies ]
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
41 engine.createTemplate(templateFile).make(replacements).writeTo(writer)
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
42 }
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
43
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
44 The template file can contain any syntax that the [GStringTemplate](https://docs.groovy-lang.org/latest/html/api/groovy/text/GStringTemplateEngine.html) supports.
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
45
712e2e9cf8b1 new blog post
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
46 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.