Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FELIX-6203] [maven-release-plugin] Allow to override build date with SOURCE_DATE_EPOCH #209

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -1564,6 +1565,19 @@ private static StringBuffer printLicenses( List<License> licenses )
}


private static Date getReproducibleBuildDate() {
String envVariable = System.getenv("SOURCE_DATE_EPOCH");
if (envVariable == null) {
return null;
}
try {
return new Date(Long.parseLong(envVariable)*1000);
} catch (Exception e) {
return null;
}
}


/**
* @param jar
* @throws IOException
Expand All @@ -1582,7 +1596,8 @@ private void doMavenMetadata( MavenProject currentProject, Jar jar ) throws IOEx
jar.putResource( path + "/pom.xml", new FileResource( pomFile ) );
}

Properties p = new Properties();
java.util.Date buildDate = getReproducibleBuildDate();
Properties p = buildDate == null ? new Properties() : new TimestampedProperties(buildDate);
p.put( "version", currentProject.getVersion() );
p.put( "groupId", currentProject.getGroupId() );
p.put( "artifactId", currentProject.getArtifactId() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.apache.felix.bundleplugin;

import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

/**
* Properties file timestamped with a specified date.
*/
class TimestampedProperties extends Properties
{
private Date date;

public TimestampedProperties(Date date) {
this.date = date;
}

@Override
public void store(OutputStream out, String comments) throws IOException {
store(new OutputStreamWriter(out, "ISO-8859-1"), comments);
}

@Override
public void store(Writer out, String comments) throws IOException {
// store the properties file in memory
StringWriter buffer = new StringWriter();
super.store(buffer, comments);

// Replace the date on the second line of the file
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
String[] lines = buffer.toString().split(Pattern.quote(System.getProperty("line.separator")));
lines[1] = "#" + fmt.format(date);

// write the file
BufferedWriter writer = new BufferedWriter(out);
try {
for (String line : lines) {
writer.write(line);
writer.newLine();
}
writer.flush();
} finally {
writer.close();
}
}
}