Vaclav Kosar's face photo
Vaclav Kosar
Software, Machine Learning, & Business

Modern Config Injection In Maven Plugins

Maven Mojo constructor injection of config parameters via Guice JSR-330 support.

Default Maven injection framework based on Plexus is very limited. No constructor injection, all configuration parameters need to be part of a Mojo and supported annotations are very unfamiliar to new comers.

Thankfully there has been effort in Maven to towards modern and mainstream injection - JSR-330 support. Latest Maven versions support usage of annotations like @Inject thanks to running Guice injection framework under the hood replacing original Plexus.

However there are still many problems remaining unaddressed. Most painful for me was lack of configuration injection. That is lack of replacement for @Parameter annotation. Without it I could not inject properties into separate components encapsulating their parsing, transformation or composition.

Thankfully there is a way to provide custom support using Provider pattern tapping into Maven native configuration injection.

Say you have a configuration of a plugin:

<plugin>
  ...
  <configuration>
    <property1>value1</property1>
</configuration>
</plugin>

Then you would implement a provider:

class Property1Provider extends RequiredConfigProvider<String> {
    
    private String value;
    
    ...
}

The super class would call a Maven’s inner undocumented class CompositeBeanHelper providing injection even for complex objects like maps. I had to instantiate this class via reflaction i.e. Class.forName.

class RequiredConfigProvider<T> {
  // Inject the instance to extract values from. 
  @Inject 
  public RequiredConfigProvider(MojoExecution mojoExecution)
  ...  
  // Filter for a property in plugin configuration.
  mojoExecution.getPlugin().getConfiguration()
  ...
  // Set the value to the child e.g. Property1Provider.
  compositeBeanHelper.setProperty(...)
  ... 
}

Using above resolves most of the issues with full usage of JSR-330 i.e. Guice injection in Maven plugins.

Example Project: GitFlow Incremental Builder

GitFlow Incremental Builder for Maven, that speeds up you builds by building only git-changed parts, uses a Guice dependency injection for its Maven Plugin.

Created on 24 Jan 2018.

Let's connect





Privacy Policy How many days left in this quarter? Twitter Bullet Points to Copy & Paste