1818 */
1919package org .apache .maven .archiver ;
2020
21- import java .io .BufferedReader ;
21+ import java .io .ByteArrayOutputStream ;
2222import java .io .File ;
2323import java .io .IOException ;
2424import java .io .InputStream ;
25- import java .io .PrintWriter ;
26- import java .io .StringReader ;
27- import java .io .StringWriter ;
25+ import java .io .Writer ;
26+ import java .nio .charset .StandardCharsets ;
2827import java .nio .file .Files ;
29- import java .util .ArrayList ;
30- import java .util .Collections ;
31- import java .util .List ;
28+ import java .nio .file .Path ;
29+ import java .util .Arrays ;
3230import java .util .Properties ;
31+ import java .util .stream .Collectors ;
3332
3433import org .apache .maven .execution .MavenSession ;
3534import org .apache .maven .project .MavenProject ;
3635import org .codehaus .plexus .archiver .Archiver ;
36+ import org .codehaus .plexus .util .io .CachingWriter ;
3737
3838/**
3939 * This class is responsible for creating the <code>pom.properties</code> file
@@ -51,44 +51,25 @@ private Properties loadPropertiesFile(File file) throws IOException {
5151 }
5252 }
5353
54- private boolean sameContents (Properties props , File file ) throws IOException {
55- if (!file .isFile ()) {
56- return false ;
54+ private void createPropertiesFile (Properties properties , Path outputFile ) throws IOException {
55+ Path outputDir = outputFile .getParent ();
56+ if (outputDir != null ) {
57+ Files .createDirectories (outputDir );
5758 }
58-
59- Properties fileProps = loadPropertiesFile (file );
60- return fileProps .equals (props );
61- }
62-
63- private void createPropertiesFile (Properties properties , File outputFile , boolean forceCreation )
64- throws IOException {
65- File outputDir = outputFile .getParentFile ();
66- if (outputDir != null && !outputDir .isDirectory () && !outputDir .mkdirs ()) {
67- throw new IOException ("Failed to create directory: " + outputDir );
68- }
69- if (!forceCreation && sameContents (properties , outputFile )) {
70- return ;
71- }
72-
73- try (PrintWriter pw = new PrintWriter (outputFile , "ISO-8859-1" );
74- StringWriter sw = new StringWriter ()) {
75-
76- properties .store (sw , null );
77-
78- List <String > lines = new ArrayList <>();
79- try (BufferedReader r = new BufferedReader (new StringReader (sw .toString ()))) {
80- String line ;
81- while ((line = r .readLine ()) != null ) {
82- if (!line .startsWith ("#" )) {
83- lines .add (line );
84- }
85- }
86- }
87-
88- Collections .sort (lines );
89- for (String l : lines ) {
90- pw .println (l );
91- }
59+ // For reproducible builds, sort the properties and drop comments.
60+ // The java.util.Properties class doesn't guarantee order so we have
61+ // to write the file using a Writer.
62+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
63+ properties .store (baos , null );
64+ // The encoding can be either UTF-8 or ISO-8859-1, as any non ascii character
65+ // is transformed into a \\uxxxx sequence anyway
66+ String output = Arrays .stream (
67+ baos .toString (StandardCharsets .ISO_8859_1 .name ()).split ("\\ r?\\ n" ))
68+ .filter (line -> !line .startsWith ("#" ))
69+ .sorted ()
70+ .collect (Collectors .joining ("\n " , "" , "\n " ));
71+ try (Writer writer = new CachingWriter (outputFile , StandardCharsets .ISO_8859_1 )) {
72+ writer .write (output );
9273 }
9374 }
9475
@@ -100,7 +81,6 @@ private void createPropertiesFile(Properties properties, File outputFile, boolea
10081 * @param archiver {@link org.codehaus.plexus.archiver.Archiver}
10182 * @param customPomPropertiesFile optional custom pom properties file
10283 * @param pomPropertiesFile The pom properties file.
103- * @param forceCreation force creation true/false
10484 * @throws org.codehaus.plexus.archiver.ArchiverException archiver exception.
10585 * @throws java.io.IOException IO exception.
10686 * @deprecated please use {@link #createPomProperties(MavenProject, Archiver, File, File, boolean)}
@@ -153,7 +133,7 @@ public void createPomProperties(
153133
154134 p .setProperty ("version" , version );
155135
156- createPropertiesFile (p , pomPropertiesFile , forceCreation );
136+ createPropertiesFile (p , pomPropertiesFile . toPath () );
157137
158138 archiver .addFile (pomPropertiesFile , "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
159139 }
0 commit comments