Skip to content

Commit 7ece2fe

Browse files
committed
[maven-release-plugin] Allow to override build date with SOURCE_DATE_EPOCH
in order to make builds reproducible. See https://reproducible-builds.org/ for why this is good and https://reproducible-builds.org/specs/source-date-epoch/ for the definition of this variable. Based on https://salsa.debian.org/java-team/maven-bundle-plugin/blob/master/debian/patches/use-changelog-date-as-pom.properties-timestamp.patch by Emmanuel Bourg <ebourg@apache.org>
1 parent cefcb13 commit 7ece2fe

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Arrays;
3333
import java.util.Collection;
3434
import java.util.Collections;
35+
import java.util.Date;
3536
import java.util.Enumeration;
3637
import java.util.HashMap;
3738
import java.util.HashSet;
@@ -1561,6 +1562,19 @@ private static StringBuffer printLicenses( List<License> licenses )
15611562
}
15621563

15631564

1565+
private static Date getReproducibleBuildDate() {
1566+
String envVariable = System.getenv("SOURCE_DATE_EPOCH");
1567+
if (envVariable == null) {
1568+
return null;
1569+
}
1570+
try {
1571+
return new Date(Long.parseLong(envVariable)*1000);
1572+
} catch (Exception e) {
1573+
return null;
1574+
}
1575+
}
1576+
1577+
15641578
/**
15651579
* @param jar
15661580
* @throws IOException
@@ -1579,7 +1593,8 @@ private void doMavenMetadata( MavenProject currentProject, Jar jar ) throws IOEx
15791593
jar.putResource( path + "/pom.xml", new FileResource( pomFile ) );
15801594
}
15811595

1582-
Properties p = new Properties();
1596+
java.util.Date buildDate = getReproducibleBuildDate();
1597+
Properties p = buildDate == null ? new Properties() : new TimestampedProperties(buildDate);
15831598
p.put( "version", currentProject.getVersion() );
15841599
p.put( "groupId", currentProject.getGroupId() );
15851600
p.put( "artifactId", currentProject.getArtifactId() );
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.apache.felix.bundleplugin;
2+
3+
import java.io.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.regex.*;
7+
8+
/**
9+
* Properties file timestamped with a specified date.
10+
*/
11+
class TimestampedProperties extends Properties
12+
{
13+
private Date date;
14+
15+
public TimestampedProperties(Date date) {
16+
this.date = date;
17+
}
18+
19+
@Override
20+
public void store(OutputStream out, String comments) throws IOException {
21+
store(new OutputStreamWriter(out, "ISO-8859-1"), comments);
22+
}
23+
24+
@Override
25+
public void store(Writer out, String comments) throws IOException {
26+
// store the properties file in memory
27+
StringWriter buffer = new StringWriter();
28+
super.store(buffer, comments);
29+
30+
// Replace the date on the second line of the file
31+
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
32+
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
33+
String[] lines = buffer.toString().split(Pattern.quote(System.getProperty("line.separator")));
34+
lines[1] = "#" + fmt.format(date);
35+
36+
// write the file
37+
BufferedWriter writer = new BufferedWriter(out);
38+
try {
39+
for (String line : lines) {
40+
writer.write(line);
41+
writer.newLine();
42+
}
43+
writer.flush();
44+
} finally {
45+
writer.close();
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)