Skip to content

Commit 165e1eb

Browse files
authored
Fall back on SOURCE_DATE_EPOCH if it exists (3.x) (#281)
* Reproducible from environment * Exclude the SOURCE_DATE_EPOCH environment variable when running tests * Reduce a bit more the code duplication
1 parent 1289e57 commit 165e1eb

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,22 @@
115115
<scope>test</scope>
116116
</dependency>
117117
</dependencies>
118+
119+
<build>
120+
<pluginManagement>
121+
<plugins>
122+
<plugin>
123+
<groupId>org.apache.maven.plugins</groupId>
124+
<artifactId>maven-surefire-plugin</artifactId>
125+
<configuration>
126+
<redirectTestOutputToFile>true</redirectTestOutputToFile>
127+
<excludedEnvironmentVariables>
128+
<excludedEnvironmentVariable>SOURCE_DATE_EPOCH</excludedEnvironmentVariable>
129+
</excludedEnvironmentVariables>
130+
</configuration>
131+
</plugin>
132+
</plugins>
133+
</pluginManagement>
134+
</build>
135+
118136
</project>

src/main/java/org/apache/maven/archiver/MavenArchiver.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,15 @@ public Date configureReproducible(String outputTimestamp) {
755755
* section 4.4.6.
756756
*/
757757
public static Optional<Instant> parseBuildOutputTimestamp(String outputTimestamp) {
758-
// Fail-fast on nulls
759-
if (outputTimestamp == null) {
760-
return Optional.empty();
758+
final String sourceDateEpoch = System.getenv("SOURCE_DATE_EPOCH");
759+
// Fail fast on null and no timestamp configured (1 character configuration is useful to override
760+
// a full value during pom inheritance)
761+
if (outputTimestamp == null || (outputTimestamp.length() < 2 && !isNumeric(outputTimestamp))) {
762+
if (sourceDateEpoch == null) {
763+
return Optional.empty();
764+
} else {
765+
outputTimestamp = sourceDateEpoch;
766+
}
761767
}
762768

763769
// Number representing seconds since the epoch
@@ -771,12 +777,6 @@ public static Optional<Instant> parseBuildOutputTimestamp(String outputTimestamp
771777
return Optional.of(date);
772778
}
773779

774-
// no timestamp configured (1 character configuration is useful to override a full value during pom
775-
// inheritance)
776-
if (outputTimestamp.length() < 2) {
777-
return Optional.empty();
778-
}
779-
780780
try {
781781
// Parse the date in UTC such as '2011-12-03T10:15:30Z' or with an offset '2019-10-05T20:37:42+06:00'.
782782
final Instant date = OffsetDateTime.parse(outputTimestamp)

0 commit comments

Comments
 (0)