You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "algomaster99 (via GitHub)" <gi...@apache.org> on 2025/10/20 21:03:34 UTC

[PR] Backport sorting of properties to maven archiver 3.x (#79) [maven-archiver]

algomaster99 opened a new pull request, #328:
URL: https://github.com/apache/maven-archiver/pull/328

   Following this checklist to help us incorporate your
   contribution quickly and easily:
   
   This PR backports changes did for reproducibility in https://github.com/apache/maven-archiver/pull/79. It ensures that properties in `pom.properties` are sorted. [Sorting of properties is already implemented in JDK 18](https://github.com/openjdk/jdk/commit/af50772d39a063652895e79d474da6ebb992cae0#diff-51e68aac0417f181bbb319d812b07971e66e4fed129b73c8a2261f9f5bca03c4R924-R929) but it is also nice to have at the Maven plugin level.
   
   - [x] Your pull request should address just one issue, without pulling in other changes.
   - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line and body.
     Note that commits might be squashed by a maintainer on merge.
   - [ ] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
     This may not always be possible but is a best-practice.
   - [ ] Run `mvn verify` to make sure basic checks pass.
     A more thorough check will be performed on your pull request automatically.
   - [ ] You have run the integration tests successfully (`mvn -Prun-its verify`).
   
   If your pull request is about ~20 lines of code you don't need to sign an
   [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
   please ask on the developers list.
   
   To make clear that you license your contribution under
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
   - [x] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   - [x] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   


-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Backport sorting of properties to maven archiver 3.x (#79) [maven-archiver]

Posted by "algomaster99 (via GitHub)" <gi...@apache.org>.
algomaster99 commented on PR #328:
URL: https://github.com/apache/maven-archiver/pull/328#issuecomment-3474056777

   @hboutemy here is the backported patch.


-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Backport sorting of properties to maven archiver 3.x (#79) [maven-archiver]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #328:
URL: https://github.com/apache/maven-archiver/pull/328#discussion_r2636061943


##########
src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java:
##########
@@ -51,44 +51,25 @@ private Properties loadPropertiesFile(File file) throws IOException {
         }
     }
 
-    private boolean sameContents(Properties props, File file) throws IOException {
-        if (!file.isFile()) {
-            return false;
+    private void createPropertiesFile(Properties properties, Path outputFile) throws IOException {
+        Path outputDir = outputFile.getParent();
+        if (outputDir != null) {
+            Files.createDirectories(outputDir);
         }
-
-        Properties fileProps = loadPropertiesFile(file);
-        return fileProps.equals(props);
-    }
-
-    private void createPropertiesFile(Properties properties, File outputFile, boolean forceCreation)
-            throws IOException {
-        File outputDir = outputFile.getParentFile();
-        if (outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs()) {
-            throw new IOException("Failed to create directory: " + outputDir);
-        }
-        if (!forceCreation && sameContents(properties, outputFile)) {
-            return;
-        }
-
-        try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1");
-                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 = Arrays.stream(
+                        baos.toString(StandardCharsets.ISO_8859_1.name()).split("\\r?\\n"))

Review Comment:
   The baos.toString() method is called with a charset name string instead of using the Charset object directly. This is a deprecated usage pattern. Consider using baos.toString(StandardCharsets.ISO_8859_1) directly, which is available since Java 10.
   ```suggestion
                           baos.toString(StandardCharsets.ISO_8859_1).split("\\r?\\n"))
   ```



##########
src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java:
##########
@@ -51,44 +51,25 @@ private Properties loadPropertiesFile(File file) throws IOException {
         }
     }
 
-    private boolean sameContents(Properties props, File file) throws IOException {
-        if (!file.isFile()) {
-            return false;
+    private void createPropertiesFile(Properties properties, Path outputFile) throws IOException {
+        Path outputDir = outputFile.getParent();
+        if (outputDir != null) {
+            Files.createDirectories(outputDir);
         }
-
-        Properties fileProps = loadPropertiesFile(file);
-        return fileProps.equals(props);
-    }
-
-    private void createPropertiesFile(Properties properties, File outputFile, boolean forceCreation)
-            throws IOException {
-        File outputDir = outputFile.getParentFile();
-        if (outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs()) {
-            throw new IOException("Failed to create directory: " + outputDir);
-        }
-        if (!forceCreation && sameContents(properties, outputFile)) {
-            return;
-        }
-
-        try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1");
-                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 = Arrays.stream(
+                        baos.toString(StandardCharsets.ISO_8859_1.name()).split("\\r?\\n"))

Review Comment:
   The Properties.store(OutputStream) method uses ISO-8859-1 encoding by default, but this is not explicitly guaranteed by the API contract. If the Properties implementation or JDK changes this behavior, the subsequent toString with ISO_8859_1 may produce incorrect results. Consider using Properties.store(Writer) directly with an ISO-8859-1 OutputStreamWriter to ensure encoding consistency throughout the entire process.



##########
src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java:
##########
@@ -51,44 +51,25 @@ private Properties loadPropertiesFile(File file) throws IOException {
         }
     }
 
-    private boolean sameContents(Properties props, File file) throws IOException {
-        if (!file.isFile()) {
-            return false;
+    private void createPropertiesFile(Properties properties, Path outputFile) throws IOException {
+        Path outputDir = outputFile.getParent();
+        if (outputDir != null) {
+            Files.createDirectories(outputDir);
         }
-
-        Properties fileProps = loadPropertiesFile(file);
-        return fileProps.equals(props);
-    }
-
-    private void createPropertiesFile(Properties properties, File outputFile, boolean forceCreation)
-            throws IOException {
-        File outputDir = outputFile.getParentFile();
-        if (outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs()) {
-            throw new IOException("Failed to create directory: " + outputDir);
-        }
-        if (!forceCreation && sameContents(properties, outputFile)) {
-            return;
-        }
-
-        try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1");
-                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

Review Comment:
   The comment states "The encoding can be either UTF-8 or ISO-8859-1" but the code only uses ISO-8859-1 encoding. This comment is misleading as the code doesn't provide any UTF-8 path or option. Either the comment should be corrected to reflect that only ISO-8859-1 is used, or the implementation should support both encodings if that was the intent.
   ```suggestion
           // The properties file is written using ISO-8859-1; any non-ASCII character
           // is transformed into a \\uxxxx sequence by Properties.store(...)
   ```



##########
src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java:
##########
@@ -153,7 +133,7 @@ public void createPomProperties(
 
         p.setProperty("version", version);
 
-        createPropertiesFile(p, pomPropertiesFile, forceCreation);
+        createPropertiesFile(p, pomPropertiesFile.toPath());

Review Comment:
   The forceCreation parameter is now ignored in the new implementation. The old code used this parameter to check if the file already has the same content (via sameContents method) and would skip creation if forceCreation was false and content was identical. This behavioral change means the file will now always be written even if its content hasn't changed, potentially affecting build performance and incremental build behavior.



##########
src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java:
##########
@@ -51,44 +51,25 @@ private Properties loadPropertiesFile(File file) throws IOException {
         }
     }
 
-    private boolean sameContents(Properties props, File file) throws IOException {
-        if (!file.isFile()) {
-            return false;
+    private void createPropertiesFile(Properties properties, Path outputFile) throws IOException {
+        Path outputDir = outputFile.getParent();
+        if (outputDir != null) {
+            Files.createDirectories(outputDir);
         }
-
-        Properties fileProps = loadPropertiesFile(file);
-        return fileProps.equals(props);
-    }
-
-    private void createPropertiesFile(Properties properties, File outputFile, boolean forceCreation)
-            throws IOException {
-        File outputDir = outputFile.getParentFile();
-        if (outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs()) {
-            throw new IOException("Failed to create directory: " + outputDir);
-        }
-        if (!forceCreation && sameContents(properties, outputFile)) {
-            return;
-        }
-
-        try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1");
-                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 = Arrays.stream(
+                        baos.toString(StandardCharsets.ISO_8859_1.name()).split("\\r?\\n"))
+                .filter(line -> !line.startsWith("#"))
+                .sorted()
+                .collect(Collectors.joining("\n", "", "\n"));

Review Comment:
   The new sorting behavior introduced for reproducible builds lacks test coverage. While existing tests verify that properties are present with correct values, they don't verify that the properties are sorted in the output file. Consider adding a test that reads the pom.properties file as text and verifies that the property lines are in sorted order.



-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Backport sorting of properties to maven archiver 3.x (#79) [maven-archiver]

Posted by "slachiewicz (via GitHub)" <gi...@apache.org>.
slachiewicz commented on code in PR #328:
URL: https://github.com/apache/maven-archiver/pull/328#discussion_r2636137571


##########
src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java:
##########
@@ -153,7 +133,7 @@ public void createPomProperties(
 
         p.setProperty("version", version);
 
-        createPropertiesFile(p, pomPropertiesFile, forceCreation);
+        createPropertiesFile(p, pomPropertiesFile.toPath());

Review Comment:
   @algomaster99 please check this



-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Backport sorting of properties to maven archiver 3.x (#79) [maven-archiver]

Posted by "algomaster99 (via GitHub)" <gi...@apache.org>.
algomaster99 commented on PR #328:
URL: https://github.com/apache/maven-archiver/pull/328#issuecomment-3423713741

   I have to fix some compilation issues which I will take a look at later.


-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Backport sorting of properties to maven archiver 3.x (#79) [maven-archiver]

Posted by "slachiewicz (via GitHub)" <gi...@apache.org>.
slachiewicz merged PR #328:
URL: https://github.com/apache/maven-archiver/pull/328


-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org