您可以使用插件创建自己的自定义工具链。
一个完整的工作示例包含在maven-toolchains-plugin ITs 中,它是插件源代码树的一部分:
以下说明是对样本关键点的解释。
工具链包括:
要获得配置的工具链,插件使用ToolchainManager API 来获取预期的工具链,然后是工具链中的一些工具:
@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 );
}
自定义工具链实现需要在 toolchain-aware 插件和maven-toolchains-plugin之间共享:这是使用 Maven 扩展完成的:
<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>
请注意,仅当有多个插件使用该工具链时,将工具链与插件分开包装在其自己的工件中才有用。正如通常预期的那样,自定义工具链将仅由一个插件使用(最终提供多个目标),将工具链与插件打包在一个工件中更简单。