elharo commented on code in PR #79:
URL: https://github.com/apache/maven-archiver/pull/79#discussion_r1860515352


##########
src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java:
##########
@@ -52,92 +48,54 @@ private Properties loadPropertiesFile(Path file) throws 
IOException {
         }
     }
 
-    private boolean sameContents(Properties props, Path file) throws 
IOException {
-        if (!Files.isRegularFile(file)) {
-            return false;
-        }
-
-        Properties fileProps = loadPropertiesFile(file);
-        return fileProps.equals(props);
-    }
-
-    private void createPropertiesFile(Properties properties, Path outputFile, 
boolean forceCreation)
-            throws IOException {
+    private void createPropertiesFile(Properties properties, Path outputFile) 
throws IOException {
         Path outputDir = outputFile.getParent();
-        if (outputDir != null && !Files.isDirectory(outputDir)) {
+        if (outputDir != null) {
             Files.createDirectories(outputDir);
         }
-        if (!forceCreation && sameContents(properties, 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.
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        properties.store(baos, null);
+        // The encoding can be either UTF-8 or ISO-8859-1, as any non ascii 
character
+        // is transformed into a \\uxxxx sequence anyway
+        String output = baos.toString(StandardCharsets.UTF_8)
+                .lines()
+                .filter(line -> !line.startsWith("#"))
+                .sorted()
+                .collect(Collectors.joining("\n", "", "\n")); // system 
independent new line
+        try (Writer pw = new CachingWriter(outputFile, 
StandardCharsets.UTF_8)) {

Review Comment:
   pw --> writer



##########
src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java:
##########
@@ -52,92 +48,54 @@ private Properties loadPropertiesFile(Path file) throws 
IOException {
         }
     }
 
-    private boolean sameContents(Properties props, Path file) throws 
IOException {
-        if (!Files.isRegularFile(file)) {
-            return false;
-        }
-
-        Properties fileProps = loadPropertiesFile(file);
-        return fileProps.equals(props);
-    }
-
-    private void createPropertiesFile(Properties properties, Path outputFile, 
boolean forceCreation)
-            throws IOException {
+    private void createPropertiesFile(Properties properties, Path outputFile) 
throws IOException {
         Path outputDir = outputFile.getParent();
-        if (outputDir != null && !Files.isDirectory(outputDir)) {
+        if (outputDir != null) {
             Files.createDirectories(outputDir);
         }
-        if (!forceCreation && sameContents(properties, 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.
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        properties.store(baos, null);
+        // The encoding can be either UTF-8 or ISO-8859-1, as any non ascii 
character
+        // is transformed into a \\uxxxx sequence anyway
+        String output = baos.toString(StandardCharsets.UTF_8)

Review Comment:
   Probably 8859-1 to avoid the risk of someone slipping in a byte order mark. 



##########
src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.shared.archiver;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Properties;
+
+import org.codehaus.plexus.archiver.jar.JarArchiver;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class PomPropertiesUtilTest {

Review Comment:
   There's an additional test in the latest draft of my PR you can adopt. 



-- 
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

Reply via email to