Skip to content

Commit c802180

Browse files
committed
Use sorted properties to make build info output repeatable
Closes gh-14494
1 parent 6de14f7 commit c802180

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import java.util.Properties;
2323

2424
import org.gradle.testkit.runner.BuildResult;
25+
import org.gradle.testkit.runner.InvalidRunnerConfigurationException;
2526
import org.gradle.testkit.runner.TaskOutcome;
27+
import org.gradle.testkit.runner.UnexpectedBuildFailure;
2628
import org.junit.jupiter.api.TestTemplate;
2729
import org.junit.jupiter.api.extension.ExtendWith;
2830

2931
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
3032
import org.springframework.boot.gradle.testkit.GradleBuild;
33+
import org.springframework.boot.loader.tools.FileUtils;
3134

3235
import static org.assertj.core.api.Assertions.assertThat;
3336

@@ -92,6 +95,22 @@ public void notUpToDateWhenExecutedTwiceWithFixedTimeAndChangedProjectVersion()
9295
assertThat(result.task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
9396
}
9497

98+
@TestTemplate
99+
public void reproducibleOutputWithFixedTime() throws InvalidRunnerConfigurationException,
100+
UnexpectedBuildFailure, IOException, InterruptedException {
101+
assertThat(this.gradleBuild.build("buildInfo", "-PnullTime").task(":buildInfo")
102+
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
103+
File buildInfoProperties = new File(this.gradleBuild.getProjectDir(),
104+
"build/build-info.properties");
105+
String firstHash = FileUtils.sha1Hash(buildInfoProperties);
106+
assertThat(buildInfoProperties.delete()).isTrue();
107+
Thread.sleep(1500);
108+
assertThat(this.gradleBuild.build("buildInfo", "-PnullTime")
109+
.task(":buildInfo").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
110+
String secondHash = FileUtils.sha1Hash(buildInfoProperties);
111+
assertThat(firstHash).isEqualTo(secondHash);
112+
}
113+
95114
private Properties buildInfoProperties() {
96115
File file = new File(this.gradleBuild.getProjectDir(),
97116
"build/build-info.properties");

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/BuildPropertiesWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.Properties;
2626

27+
import org.springframework.core.CollectionFactory;
28+
2729
/**
2830
* A {@code BuildPropertiesWriter} writes the {@code build-info.properties} for
2931
* consumption by the Actuator.
@@ -68,7 +70,7 @@ private void createFileIfNecessary(File file) throws IOException {
6870
}
6971

7072
protected Properties createBuildInfo(ProjectDetails project) {
71-
Properties properties = new Properties();
73+
Properties properties = CollectionFactory.createSortedProperties(true);
7274
properties.put("build.group", project.getGroup());
7375
properties.put("build.artifact", project.getArtifact());
7476
properties.put("build.name", project.getName());

0 commit comments

Comments
 (0)