gnodet commented on code in PR #77: URL: https://github.com/apache/maven-archiver/pull/77#discussion_r1859448643
########## src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java: ########## @@ -61,38 +60,38 @@ private boolean sameContents(Properties props, Path file) throws IOException { return fileProps.equals(props); } - private void createPropertiesFile(Properties properties, Path outputFile, boolean forceCreation) + private void createPropertiesFile(Properties unsortedProperties, Path outputFile, boolean forceCreation) throws IOException { Path outputDir = outputFile.getParent(); if (outputDir != null && !Files.isDirectory(outputDir)) { Files.createDirectories(outputDir); } - if (!forceCreation && sameContents(properties, outputFile)) { + if (!forceCreation && sameContents(unsortedProperties, outputFile)) { return; } - try (PrintWriter pw = new PrintWriter(outputFile.toFile(), StandardCharsets.ISO_8859_1.name()); - StringWriter sw = new StringWriter()) { - - properties.store(sw, null); - - List<String> lines = new ArrayList<>(); - try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) { - String line; - while ((line = r.readLine()) != null) { - if (!line.startsWith("#")) { - lines.add(line); - } - } - } - - Collections.sort(lines); - for (String l : lines) { - pw.println(l); + // For reproducible builds, sort the properties and drop comments. + // The java.util.Properties class doesn't guarantee order so we have + // to write the file using a Writer. + Set<String> propertyNames = unsortedProperties.stringPropertyNames(); + List<String> sortedPropertyNames = new ArrayList<>(propertyNames); + Collections.sort(sortedPropertyNames); + + try (Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { + for (String key : sortedPropertyNames) { + out.write(escape(key)); + out.write("="); + out.write(escape(unsortedProperties.getProperty(key))); + out.write('\n'); } } } + private static String escape(String s) { + String escaped = StringEscapeUtils.escapeJava(s); Review Comment: Unicode encoding is clearly not well supported in the current code, but the PR I pointed above fixes it. The reason is that calling `Properties.store(Writer)` bypasses the encoding, while calling `Properties.store(OutputStream)` correctly supports unicode. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org