At 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.


Generating test coverage for Sonar with maven, surefire and jacoco

11.05.2020 by Dirk Olmes

At work we’re using Sonar to keep our code quality under control. One integral part of code quality is test coverage and Sonar offers coverage metrics in the UI. However, the Sonar docs on code coverage are a bit sparse (at best) and don’t tell you the exact …

read more

Deploying files with maven-deploy-plugin

11.02.2019 by Dirk Olmes

We recently had an outage of our Nexus instance at work. The file system went corrupt, nexus would not start up properly complaining about a corrupted Orient DB.

The blob store was largely left intact. In hindsight I should have tried to rebuild the OrientDB from the blob store using …

read more

Default JDK for cross JDK project profiles with Maven

25.04.2009 by Dirk Olmes

In my previous blog post about Cross JDK project files with Maven I described a way to generate a custom JDK name into the Eclipse project files using the maven-eclipse-plugin.

That approach still had one shortcoming: you would either have to rename your JDK to match the default configured in …

read more

maven-assembly-plugin vs system scope dependencies

18.04.2008 by Dirk Olmes

I was playing around with Gigaspaces the other day. Since GS jars are not available on any Maven repo and GS comes with everything you need prepackaged I used Maven dependencies with system scope to pull the required jars into my build.

Then I tried to assemble a simple zip …

read more

Cross JDK project files with Maven (continued)

21.03.2008 by Dirk Olmes

In my last blog entry I described the steps for cross-JDK Eclipse project files.

Unfortunately there’s more Eclipse internals involved when dealing with cross platform issues. It turns out that the correct JRE_CONTAINER for Linux and Windows is

org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug …
read more

Cross JDK project files with Maven

19.03.2008 by Dirk Olmes

Recently we switched the Mule build to be based on JDK5 for some modules. This requires the Maven build to be JDK aware. While the JDK auto-activation of profiles get you to build the project from the commandline, IDE integration was a bit of pain.

Now that the maven-eclipse-plugin has …

read more

Skipping test execution but not test compilation

17.05.2007 by Dirk Olmes

In more complicated Maven builds you might package your tests along with your normal code to use in other modules (see plugin doc to the maven-jar-plugin how to do it). For normal development you might not want to execute the unit tests every time you build. Compiling the source with …

read more