Support Maven's outputTimestamp when repackaging jars and wars

Closes gh-20176
This commit is contained in:
Andy Wilkinson
2020-03-16 16:59:34 +00:00
parent df8c25e213
commit ca202ad59f
11 changed files with 319 additions and 6 deletions

View File

@@ -18,8 +18,11 @@ package org.springframework.boot.maven;
import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.FileTime;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
@@ -140,6 +143,15 @@ public class RepackageMojo extends AbstractPackagerMojo {
@Parameter
private Properties embeddedLaunchScriptProperties;
/**
* Timestamp for reproducible output archive entries, either formatted as ISO 8601
* (<code>yyyy-MM-dd'T'HH:mm:ssXXX</code>) or an {@code int} representing seconds
* since the epoch.
* @since 2.3.0
*/
@Parameter(defaultValue = "${project.build.outputTimestamp}")
private String outputTimestamp;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.project.getPackaging().equals("pom")) {
@@ -160,7 +172,7 @@ public class RepackageMojo extends AbstractPackagerMojo {
Libraries libraries = getLibraries(this.requiresUnpack);
try {
LaunchScript launchScript = getLaunchScript();
repackager.repackage(target, libraries, launchScript);
repackager.repackage(target, libraries, launchScript, parseOutputTimestamp());
}
catch (IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
@@ -168,6 +180,22 @@ public class RepackageMojo extends AbstractPackagerMojo {
updateArtifact(source, target, repackager.getBackupFile());
}
private FileTime parseOutputTimestamp() {
// Maven ignore a single-character timestamp as it is "useful to override a full
// value during pom inheritance"
if (this.outputTimestamp == null || this.outputTimestamp.length() < 2) {
return null;
}
long epochSeconds;
try {
epochSeconds = Long.parseLong(this.outputTimestamp);
}
catch (NumberFormatException ex) {
epochSeconds = OffsetDateTime.parse(this.outputTimestamp).toInstant().getEpochSecond();
}
return FileTime.from(epochSeconds, TimeUnit.SECONDS);
}
/**
* Return the source {@link Artifact} to repackage. If a classifier is specified and
* an artifact with that classifier exists, it is used. Otherwise, the main artifact