From c16c6bc3b5384764813d6aa8f30973759971eebe Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 17 Oct 2022 18:58:42 -0700 Subject: [PATCH] Fix reachability argfile format Update the argfile to a format that actually works when passed to the `native-image` command. See gh-32738 --- .../boot/gradle/tasks/bundling/BootZipCopyAction.java | 5 ++--- .../boot/gradle/tasks/bundling/BootJarTests.java | 4 ++-- .../org/springframework/boot/loader/tools/Packager.java | 5 ++--- .../boot/loader/tools/AbstractPackagerTests.java | 8 ++++---- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java index 04f34e2c65..654caf45ae 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java @@ -369,15 +369,14 @@ class BootZipCopyAction implements CopyAction { } } } - // https://docs.oracle.com/en/java/javase/18/docs/specs/man/java.html#java-command-line-argument-files if (excludes != null) { List args = new ArrayList<>(); for (String exclude : excludes) { int lastSlash = exclude.lastIndexOf('/'); String jar = (lastSlash != -1) ? exclude.substring(lastSlash + 1) : exclude; args.add("--exclude-config"); - args.add("\"" + Pattern.quote(jar).replace("\\", "\\\\") + "\""); - args.add("\"^/META-INF/native-image/.*\""); + args.add(Pattern.quote(jar)); + args.add("^/META-INF/native-image/.*"); } ZipEntryContentWriter writer = ZipEntryContentWriter.fromLines(BootZipCopyAction.this.encoding, args); writeEntry("META-INF/native-image/argfile", writer, true); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java index 8a67aa4630..ee8efd74cc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java @@ -189,8 +189,8 @@ class BootJarTests extends AbstractBootArchiveTests { void nativeImageArgFileWithExcludesIsWritten() throws IOException { try (JarFile jarFile = new JarFile(createLayeredJar(true))) { assertThat(entryLines(jarFile, "META-INF/native-image/argfile")).containsExactly("--exclude-config", - "\"\\\\Qfirst-library.jar\\\\E\"", "\"^/META-INF/native-image/.*\"", "--exclude-config", - "\"\\\\Qsecond-library.jar\\\\E\"", "\"^/META-INF/native-image/.*\""); + "\\Qfirst-library.jar\\E", "^/META-INF/native-image/.*", "--exclude-config", + "\\Qsecond-library.jar\\E", "^/META-INF/native-image/.*"); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java index 845e4d4978..3304565f3e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Packager.java @@ -241,15 +241,14 @@ public abstract class Packager { } } } - // https://docs.oracle.com/en/java/javase/18/docs/specs/man/java.html#java-command-line-argument-files if (!excludes.isEmpty()) { List args = new ArrayList<>(); for (String exclude : excludes) { int lastSlash = exclude.lastIndexOf('/'); String jar = (lastSlash != -1) ? exclude.substring(lastSlash + 1) : exclude; args.add("--exclude-config"); - args.add("\"" + Pattern.quote(jar).replace("\\", "\\\\") + "\""); - args.add("\"^/META-INF/native-image/.*\""); + args.add(Pattern.quote(jar)); + args.add("^/META-INF/native-image/.*"); } String contents = args.stream().collect(Collectors.joining("\n")) + "\n"; writer.writeEntry("META-INF/native-image/argfile", diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/AbstractPackagerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/AbstractPackagerTests.java index 6d7b80a329..6d5b3b96ed 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/AbstractPackagerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/AbstractPackagerTests.java @@ -643,11 +643,11 @@ abstract class AbstractPackagerTests

{ List expected = new ArrayList<>(); expected.add("--exclude-config"); - expected.add("\"\\\\Q" + libraryOne.getName() + "\\\\E\""); - expected.add("\"^/META-INF/native-image/.*\""); + expected.add("\\Q" + libraryOne.getName() + "\\E"); + expected.add("^/META-INF/native-image/.*"); expected.add("--exclude-config"); - expected.add("\"\\\\Q" + libraryTwo.getName() + "\\\\E\""); - expected.add("\"^/META-INF/native-image/.*\""); + expected.add("\\Q" + libraryTwo.getName() + "\\E"); + expected.add("^/META-INF/native-image/.*"); assertThat(getPackagedEntryContent("META-INF/native-image/argfile")) .isEqualTo(expected.stream().collect(Collectors.joining("\n")) + "\n"); }