Blog

  • 2024
  • 2023
  • 2022
  • 2021
  • 2020
  • 2019
  • 2018
  • 2017
  • 2016
  • 2015
  • 2014
  • 2013
  • 2012




Using complex data types in Maven plugin configurations is quite easy. There is a good and brief documentation within the Maven site that shows how to use them in your plugin: Mapping Complex Objects in Guide to Configuring Plug-ins.

What is not mentioned in this document is how the properties are injected. It seems that setters are used, if they are present. If not, injection occurs with the fields via reflection. So the following two configuration approaches are successful.

Field Injection

This example shows field injection together with a property type that is defined within the plugin’s project.

Configuration

The key contains (for simplicity) only one element, an identifier of the environment.

<configuration>
  <key>
    <environment>test</environment>
  </key>
</configuration>

Mojo Implementation

The field is required to be named key.

public final class MyMojo extends AbstractMojo {
  /**
   * @parameter
   */
  private Key key;
  ...
}

Property Type Implementation

It is important that the name of the type matches that of the parameter in the Mojo (the first letter is capitalized as is standard for Java Beans).

public class Key {
  private String environment;
 
  public String getEnvironment() {
    return environment;
  }
}

There is no setter, so Maven injects the value in the private field environment. The name of the field and the name of the property within the configuration are the same.

Setter Injection

This setter injection example shows the configuration of a property (a factory), whose type is defined outside of the Maven plugin project. The implementation attribute allows to select an implementation that is used as a complex property.

Configuration

The configuration references an implementation of the PropertySinkFactory interface.

<configuration>
  <propertySinkFactory 
      implementation="de.smartics.properties.config.transfer.filesystem.Factory">
    <targetFolder>${basedir}/target/testme</targetFolder>
    <outputFormat>xml</outputFormat>
  </propertySinkFactory>
</configuration>

Mojo Implementation

There is no naming constraint.

public final class MyMojo extends AbstractMojo {
  /**
   * @parameter
   */
  private PropertySinkFactory<?> propertySinkFactory
  ...
}

Property Type Implementation

There are no constraints on the property type name. The properties of the type have to match the element names of the configuration (e.g. targetFolder).

 package de.smartics.properties.config.transfer.filesystem;
 
import java.io.File;
import java.io.IOException;
 
import de.smartics.properties.api.config.transfer.PropertySinkFactory;
import de.smartics.util.io.FileFunction;
 
public final class Factory implements
    PropertySinkFactory<FileSystemPropertySink>
{
  private File targetFolder;
  private PropertiesFormat outputFormat = PropertiesFormat.NATIVE;
 
  public Factory() {
  }
 
  public void setTargetFolder(final String targetFolder) {
    this.targetFolder = new File(targetFolder);
    try
    {
      FileFunction.provideDirectory(this.targetFolder);
    }
    catch (final IOException e)
    {
      final String message =
          String.format("Cannot create target folder '%s'.",
              this.targetFolder.getAbsolutePath());
      throw new IllegalArgumentException(message, e);
    }
  }
 
  public void setOutputFormat(final String outputFormat) {
    this.outputFormat = PropertiesFormat.fromString(outputFormat);
  }
 
  ...
}

The example shows how String parameter values are translated to their domain types within the setter methods.

Conclusion

The examples show more detailed than the original documentation how complex properties can be used in Maven plugins.

 


Link

Link

Posts