Custom Toolchain for Gradle Maven Exec Plugin

I am using the Gradle Maven Exec plugin to run a maven build.
How can i make this task use Java 8?

I have configured the use of Java Toolchain. The Gradle build is using Java 11 to produce an application. The Maven build task is to produce a Java Web Start package, and is using Java 8.
Is there a way to make this plugin use Java 8 when invoking Maven?

task createWebstart(type: MavenExec) {
    goals 'clean', 'install'
}

I have tried as suggested by the documentation to specify custom tollchain for individual tasks:

https://docs.gradle.org/current/userguide/toolchains.html#specify_custom_toolchains_for_individual_tasks

task createWebstart(type: MavenExec) {
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(8)
    }
    goals 'clean', 'install'
}

Perhaps the plugin does not support Custom Toolchains

Could not set unknown property ‘javaLauncher’ for task ‘:createWebstart’ of type com.github.dkorotych.gradle.maven.exec.MavenExec.

There was MavenOptions for GlobalToolchains and Toolchains in the Plugin.

I tried using these, but not sure how it works.

def javaHome = javaToolchains.compilerFor {
    languageVersion = JavaLanguageVersion.of(8)
}.get().metadata.installationPath.file("bin/javac").asFile

task('createWebstart', type: MavenExec) {
    globalToolchains = javaHome
    goals 'clean', 'install'
}
> Task :createWebstart FAILED
[ERROR] Error executing Maven.
[ERROR] 1 problem was encountered while building the effective toolchains
[FATAL] Non-parseable toolchains /usr/java/jdk1.8.0_181/bin/javac: only whitespace content allowed before start tag and not  (position: START_DOCUMENT seen ... @1:1)  @ line 1, column 1

That toolchain setting has nothing to do with the Gradle JVM toolchains despite the same naming.
It is some feature of Maven where you can have toolchains in some XML file in the Maven user home directory and that file you can supply there if it is not in the standard location.
I don’t know if that would help with your issue as I avoid Maven whereever possible.

That task does not support Gradle JVM toolchains as far as I can see.

But from a very quick look at the sources of that plugin, it seems to call the mvn executable of the Maven installation. Those executables if I’m not completely mistaken should consider JAVA_HOME environment variable.
So you might have success with setting the JAVA_HOME environment variable to the toolchain installation you want to be used.
Something like this should probably do (untested):

task('createWebstart', type: MavenExec) {
    environment 'JAVA_HOME', "${javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(8) }.get().metadata.installationPath}"
    goals 'clean', 'install'
}