Skip to content

Commit 178bd88

Browse files
authored
Merge pull request #502 from joschi/issue-472-reproducible-builds
Enable reproducible builds by suppressing date comment in properties
2 parents e1f9366 + 136c1b9 commit 178bd88

File tree

5 files changed

+81
-188
lines changed

5 files changed

+81
-188
lines changed

core/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@
4848
<groupId>com.google.code.findbugs</groupId>
4949
<artifactId>jsr305</artifactId>
5050
</dependency>
51+
<dependency>
52+
<groupId>nu.studer</groupId>
53+
<artifactId>java-ordered-properties</artifactId>
54+
<version>1.0.4</version>
55+
</dependency>
5156
</dependencies>
52-
</project>
57+
</project>

core/src/main/java/pl/project13/core/PropertiesFileGenerator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import com.fasterxml.jackson.core.type.TypeReference;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import nu.studer.java.util.OrderedProperties;
2223
import org.sonatype.plexus.build.incremental.BuildContext;
2324
import pl.project13.core.log.LoggerBridge;
24-
import pl.project13.core.util.SortedProperties;
2525

2626
import javax.annotation.Nonnull;
2727
import java.io.*;
2828
import java.nio.charset.Charset;
2929
import java.nio.file.Files;
30+
import java.util.Comparator;
3031
import java.util.HashMap;
3132
import java.util.Map;
3233
import java.util.Properties;
@@ -86,8 +87,11 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
8687
if (shouldGenerate) {
8788
Files.createDirectories(gitPropsFile.getParentFile().toPath());
8889
try (OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
89-
SortedProperties sortedLocalProperties = new SortedProperties();
90-
sortedLocalProperties.putAll(localProperties);
90+
OrderedProperties sortedLocalProperties = new OrderedProperties.OrderedPropertiesBuilder()
91+
.withSuppressDateInComment(true)
92+
.withOrdering(Comparator.nullsLast(Comparator.naturalOrder()))
93+
.build();
94+
localProperties.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
9195
if (isJsonFormat) {
9296
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
9397
log.info("Writing json file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);

core/src/main/java/pl/project13/core/util/SortedProperties.java

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <[email protected]>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core;
19+
20+
import org.junit.Before;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.TemporaryFolder;
24+
import org.sonatype.plexus.build.incremental.BuildContext;
25+
import org.sonatype.plexus.build.incremental.DefaultBuildContext;
26+
import pl.project13.core.log.LoggerBridge;
27+
import pl.project13.core.log.StdOutLoggerBridge;
28+
29+
import java.io.IOException;
30+
import java.nio.charset.StandardCharsets;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.util.Properties;
34+
35+
import static java.nio.charset.StandardCharsets.UTF_8;
36+
import static org.junit.Assert.assertEquals;
37+
38+
public class PropertiesFileGeneratorTest {
39+
@Rule
40+
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
41+
42+
private final LoggerBridge loggerBridge = new StdOutLoggerBridge(false);
43+
private final BuildContext buildContext = new DefaultBuildContext();
44+
45+
private PropertiesFileGenerator propertiesFileGenerator;
46+
47+
@Before
48+
public void setUp() {
49+
propertiesFileGenerator = new PropertiesFileGenerator(loggerBridge, buildContext, "properties", "", "test");
50+
}
51+
52+
@Test
53+
public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitIdExecutionException, IOException {
54+
Properties properties = new Properties();
55+
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
56+
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
57+
58+
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
59+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
60+
61+
byte[] bytes = Files.readAllBytes(propertiesPath);
62+
String actualContent = new String(bytes, UTF_8);
63+
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
64+
+ "branch=develop\n"
65+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
66+
assertEquals(expectedContent, actualContent);
67+
}
68+
}

maven/src/test/java/pl/project13/core/util/SortedPropertiesTest.java

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)