From 2bc69764e9cbc5c1412b7dbbc809ce991b1a8f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Mon, 11 Dec 2023 16:20:04 +0100 Subject: [PATCH] Fix compile-no-fork with existing jar FileSystems (#547) FileSystems.newFileSystem on a dependency jar fails with a FileSystemAlreadyExistsException if another Maven plugin has already created it. Provide a wrapper that handles the exception, trying FileSystems.getFileSystem in that case. --- .../buildtools/maven/AbstractNativeImageMojo.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java index 4bd725408..fa5e563bd 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java @@ -64,6 +64,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.FileSystems; +import java.nio.file.FileSystemAlreadyExistsException; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.Files; @@ -293,13 +294,23 @@ protected void addArtifactToClasspath(Artifact artifact) throws MojoExecutionExc Optional.ofNullable(processSupportedArtifacts(artifact)).ifPresent(imageClasspath::add); } + private static FileSystem openFileSystem(URI uri) throws IOException { + FileSystem fs; + try { + fs = FileSystems.newFileSystem(uri, Collections.emptyMap()); + } catch (FileSystemAlreadyExistsException e) { + fs = FileSystems.getFileSystem(uri); + } + return fs; + } + protected void warnIfWrongMetaInfLayout(Path jarFilePath, Artifact artifact) throws MojoExecutionException { if (jarFilePath.toFile().isDirectory()) { logger.debug("Artifact `" + jarFilePath + "` is a directory."); return; } URI jarFileURI = URI.create("jar:" + jarFilePath.toUri()); - try (FileSystem jarFS = FileSystems.newFileSystem(jarFileURI, Collections.emptyMap())) { + try (FileSystem jarFS = openFileSystem(jarFileURI)) { Path nativeImageMetaInfBase = jarFS.getPath("/" + NATIVE_IMAGE_META_INF); if (Files.isDirectory(nativeImageMetaInfBase)) { try (Stream stream = Files.walk(nativeImageMetaInfBase)) {