What is the proper way to author a Java-toolchain-aware task for tools other than java, javac, and javadoc?

The Toolchains for JVM projects (gradle.org) documentation is clear on how to make a task Java-toolchain-aware for JavaLauncher by using launcherFor. Looking at the documentation of JavaToolchainService I can also see you can get a JavaCompiler via compilerFor and a JavadocTool via javadocToolFor. That’s great if I want to directly use the java, javac, or javadoc executables.

But say I want to execute a different tool, such as jlink, jpackage, or jmod. One way I can think to implement this is like so:

import org.gradle.api.DefaultTask
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.TaskAction
import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.getByType
import org.gradle.process.ExecOperations
import javax.inject.Inject

abstract class JLinkTask : DefaultTask() {

    protected abstract val execOps: ExecOperations

    protected abstract val javaToolchainService: JavaToolchainService

    abstract val launcher: Property<JavaLauncher>

    init {
        val toolchain = project.extensions.getByType(JavaPluginExtension::class).toolchain
        val defaultLauncher = javaToolchainService.launcherFor(toolchain)

    fun execute() {
        val result = execOps.exec {
            // Is this a good idea?

            // ... other configuration ...

But is this part:


A good idea? Is there a better implementation? Should I not use toolchains for this?

Additionally, will toolchains be extended to support any Java tool, or perhaps simply provide a way (if there isn’t one already?) to get the installation path without going through, e.g., JavaLauncher (which implies the java tool will be used)?

Hopefully one day.
What you are after is Allow implementations of java.util.spi.ToolProvider to be discoverable via toolchains · Issue #15027 · gradle/gradle · GitHub.