diff --git a/README.md b/README.md index eda979e..1e6f2fc 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Since the plugin is available on [Gradle's Plugin Portal](https://plugins.gradle ```gradle plugins { - id "io.github.krakowski.jextract" version "0.2.6" + id "io.github.krakowski.jextract" version "0.2.7" } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 62f18a8..1c6f275 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { } group = "io.github.krakowski" -version = "0.2.6" +version = "0.2.7" repositories { mavenCentral() diff --git a/src/main/kotlin/io/github/krakowski/jextract/JextractTask.kt b/src/main/kotlin/io/github/krakowski/jextract/JextractTask.kt index ad34e07..06d6ba6 100644 --- a/src/main/kotlin/io/github/krakowski/jextract/JextractTask.kt +++ b/src/main/kotlin/io/github/krakowski/jextract/JextractTask.kt @@ -7,6 +7,7 @@ import org.gradle.api.GradleException import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.* +import org.gradle.api.tasks.Optional import org.gradle.internal.os.OperatingSystem import org.gradle.kotlin.dsl.newInstance import org.gradle.kotlin.dsl.property @@ -14,6 +15,9 @@ import java.io.File import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths +import java.util.* +import kotlin.NoSuchElementException +import kotlin.collections.ArrayList abstract class JextractTask : DefaultTask() { @@ -47,19 +51,23 @@ abstract class JextractTask : DefaultTask() { val operatingSystem = OperatingSystem.current() val executable = if (operatingSystem.isWindows) WINDOWS_EXECUTABLE else UNIX_EXECUTABLE - // Search for jextract in PATH - System.getenv(ENV_PATH).split(File.pathSeparator).forEach { pathEntry -> - val exPath = Paths.get(pathEntry, executable) - if (Files.exists(exPath)) return@findExecutable exPath - } - - // Use bundled jextract binary as a fallback + // Try bundled jextract binary first to ensure compatibility with the currently used JDK val bundledExecutable = Paths.get(toolchain.get(), "bin", executable) if (Files.exists(bundledExecutable)) { return bundledExecutable } - throw GradleException("jextract binary could not be found in PATH or at ${bundledExecutable}") + // Search for jextract in PATH if JDK has no bundled binary + val pathExecutable = System.getenv(ENV_PATH) + .split(File.pathSeparator) + .map { path -> Paths.get(path, executable) } + .filter { path -> Files.exists(path) } + + try { + return pathExecutable.first() + } catch (exception: NoSuchElementException) { + throw GradleException("jextract binary could not be found in PATH or at ${bundledExecutable}") + } } @TaskAction