Modern Config Injection In Maven Plugins

24 Jan 2018

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

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.


Subscribe: Twitter , Facebook , RSS
Share on: Twitter , Facebook , Google+ , LinkedIn , Reddit .





Report any trackers to [email protected].