diff --git a/core/deployment/src/main/java/io/quarkus/runner/bootstrap/AugmentActionImpl.java b/core/deployment/src/main/java/io/quarkus/runner/bootstrap/AugmentActionImpl.java index af2bb2e33d1d1..9512528a725cf 100644 --- a/core/deployment/src/main/java/io/quarkus/runner/bootstrap/AugmentActionImpl.java +++ b/core/deployment/src/main/java/io/quarkus/runner/bootstrap/AugmentActionImpl.java @@ -1,7 +1,6 @@ package io.quarkus.runner.bootstrap; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; @@ -32,6 +31,7 @@ import io.quarkus.bootstrap.app.QuarkusBootstrap; import io.quarkus.bootstrap.classloading.ClassLoaderEventListener; import io.quarkus.bootstrap.classloading.QuarkusClassLoader; +import io.quarkus.bootstrap.util.PropertyUtils; import io.quarkus.builder.BuildChainBuilder; import io.quarkus.builder.BuildResult; import io.quarkus.builder.item.BuildItem; @@ -237,8 +237,8 @@ private void writeArtifactResultMetadataFile(BuildSystemTargetBuildItem outputTa properties.put("metadata." + entry.getKey(), entry.getValue()); } } - try (FileOutputStream fos = new FileOutputStream(quarkusArtifactMetadataPath.toFile())) { - properties.store(fos, "Generated by Quarkus - Do not edit manually"); + try { + PropertyUtils.store(properties, quarkusArtifactMetadataPath, "Generated by Quarkus - Do not edit manually"); } catch (IOException e) { log.debug("Unable to write artifact result metadata file", e); } diff --git a/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/PropertyUtils.java b/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/PropertyUtils.java index 8751c22210ce4..c432f019cc028 100644 --- a/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/PropertyUtils.java +++ b/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/PropertyUtils.java @@ -61,12 +61,13 @@ public static final boolean getBoolean(String name, boolean notFoundValue) { * {@link Properties#store(Writer, String)} format but skipping the timestamp and comments. * * @param properties properties to store + * @param leadingComment a leading comment, it will be prepended by an hash * @param file target file * @throws IOException in case of a failure */ - public static void store(Properties properties, Path file) throws IOException { + public static void store(Properties properties, Path file, String leadingComment) throws IOException { try (BufferedWriter writer = Files.newBufferedWriter(file)) { - store(properties, writer); + store(properties, writer, leadingComment); } } @@ -75,11 +76,19 @@ public static void store(Properties properties, Path file) throws IOException { * {@link Properties#store(Writer, String)} format but skipping the timestamp and comments. * * @param properties properties to store + * @param leadingComment a leading comment, it will be prepended by an hash * @param writer target writer * @throws IOException in case of a failure */ - public static void store(Properties properties, Writer writer) throws IOException { + public static void store(Properties properties, Writer writer, String leadingComment) throws IOException { final List names = new ArrayList<>(properties.size()); + + if (leadingComment != null && !leadingComment.isBlank()) { + writer.write("# "); + writer.write(leadingComment); + writer.write(System.lineSeparator()); + } + for (var name : properties.keySet()) { names.add(name == null ? null : name.toString()); } @@ -97,16 +106,58 @@ public static void store(Properties properties, Writer writer) throws IOExceptio * @param file target file * @throws IOException in case of a failure */ - public static void store(Map properties, Path file) throws IOException { + public static void store(Map properties, Path file, String leadingComment) throws IOException { final List names = new ArrayList<>(properties.keySet()); Collections.sort(names); try (BufferedWriter writer = Files.newBufferedWriter(file)) { + if (leadingComment != null && !leadingComment.isBlank()) { + writer.write("# "); + writer.write(leadingComment); + writer.write(System.lineSeparator()); + } + for (String name : names) { store(writer, name, properties.get(name)); } } } + /** + * Stores properties into a file sorting the keys alphabetically and following + * {@link Properties#store(Writer, String)} format but skipping the timestamp and comments. + * + * @param properties properties to store + * @param file target file + * @throws IOException in case of a failure + */ + public static void store(Properties properties, Path file) throws IOException { + store(properties, file, null); + } + + /** + * Stores properties into a file sorting the keys alphabetically and following + * {@link Properties#store(Writer, String)} format but skipping the timestamp and comments. + * + * @param properties properties to store + * @param writer target writer + * @throws IOException in case of a failure + */ + public static void store(Properties properties, Writer writer) throws IOException { + store(properties, writer, null); + } + + /** + * Stores a map of strings into a file sorting the keys alphabetically and following + * {@link Properties#store(Writer, String)} format but skipping the timestamp and comments. + * + * @param properties properties to store + * @param file target file + * @throws IOException in case of a failure + */ + public static void store(Map properties, Path file) throws IOException { + store(properties, file, null); + } + /** * Writes a config option with its value to the target writer, * possibly applying some transformations, such as character escaping