Starting with Gradle version 7.6, JVM auto-provisioning logic gets decoupled from Gradle’s lifecycle and moves into independent plugins, hosted on the Plugin Portal. For details on how toolchain auto-provisioning is using these plugins, see here. This page is intended for authors of such plugins.
Code examples will be provided in Java, but plugin authors should feel free to use all the various ways of developing Gradle plugins. |
Core logic
The main thing toolchain resolver plugins need to provide is the logic of mapping a toolchain request to a download URI. They do this by implementing JavaToolchainResolver:
public abstract class JavaToolchainResolverImplementation implements JavaToolchainResolver { (1)
public Optional<URI> resolve(JavaToolchainRequest request) { (2)
// custom mapping logic goes here
}
}
1 | The implementation needs to be abstract because JavaToolchainResolver is a build service and those do have abstract methods with dynamic implementations provided by Gradle at runtime. |
2 | The mapping method returns a URI wrapped in an Optional , which is empty if, and only if, this resolver implementation can’t provide a download link for the requested toolchain. |
Plugin application
To wire the JavaToolchainResolver
implementation into Gradle a settings plugin is needed (Plugin<Settings>
):
public abstract class JavaToolchainResolverPlugin implements Plugin<Settings> { (1)
@Inject
protected abstract JavaToolchainResolverRegistry getToolchainResolverRegistry(); (2)
void apply(Settings settings) {
settings.getPlugins().apply("jvm-toolchain-management"); (3)
JavaToolchainResolverRegistry registry = getToolchainResolverRegistry();
registry.register(JavaToolchainResolverImplementation.class); (4)
}
}
1 | The plugin needs to be a settings plugin and is abstract because it uses property injection. |
2 | Access is needed to the JavaToolchainResolverRegistry Gradle service to register the resolver implementation. |
3 | These plugins are required to auto apply the jvm-toolchain-management base plugin, which dynamically adds the jvm block to toolchainManagement , thus making the registered toolchain repositories usable from the build. |
4 | The last step is to register the toolchain resolver with the registry service injected by Gradle. |