You can create your own custom toolchains with plugins using them.
A full working sample is included in maven-toolchains-plugin ITs, which are part of the plugin source tree:
Following instructions are explanations of key points of the sample.
A toolchain consists in:
To get a configured toolchain, a plugin uses ToolchainManager API to get expected toolchain, then some tool in the toolchain:
@Component
private ToolchainManager toolchainManager;
@Parameter( defaultValue = "${session}", required = true, readonly = true )
private MavenSession session;
public void execute()
throws MojoExecutionException
{
// get the custom toolchain
CustomToolchain toolchain = (CustomToolchain) toolchainManager.getToolchainFromBuildContext( "custom", session );
if ( toolchain == null )
{
throw new MojoExecutionException( "Could not find 'custom' toolchain: please check maven-toolchains-plugin configuration." );
}
getLog().info( "Found 'custom' toolchain in build context." );
// get a tool from the toolchain
String path = toolchain.findTool( "tool" );
getLog().info( "Found expected tool named 'tool' at following location: " + path );
}This code uses Maven Plugin Tool Java 5 Annotations.
The custom toolchain implementation needs to be shared between the toolchain-aware plugin and maven-toolchains-plugin: this is done using Maven extension:
<plugin>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<extensions>true</extensions><!-- to share the custom toolchain with maven-toolchains-plugin -->
</plugin><project>
<build>
<extensions>
<extension>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</extension>
</extensions>
</build>
</project>Notice that packaging a toolchain in its own artifact separate from plugin is only useful when there are multiple plugins using the toolchain. As it is expected in general that a custom toolchain will be used by only one plugin (eventually providing multiple goals), it is simpler to package the toolchain with the plugin in only one artifact.