Polish "Use the Maven Archiver implementation for parsing outputTimestamp"

See gh-37438
This commit is contained in:
Moritz Halbritter
2023-09-22 11:12:00 +02:00
parent a1b08e1819
commit a3611ae9f3
3 changed files with 160 additions and 53 deletions

View File

@@ -16,91 +16,97 @@
package org.springframework.boot.maven;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
public final class MavenBuildOutputTimestamp {
import org.springframework.util.StringUtils;
private MavenBuildOutputTimestamp() {
// Utility class
}
// This implementation was copied from the maven-archiver
// https://github.com/apache/maven-archiver/blob/cc2f6a219f6563f450b0c00e8ccd651520b67406/src/main/java/org/apache/maven/archiver/MavenArchiver.java#L768
/**
* Parse output timestamp configured for Reproducible Builds' archive entries.
* <p>
* Either as {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME} or as a
* number representing seconds since the epoch (like <a href=
* "https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
* Implementation inspired by <a href=
* "https://github.com/apache/maven-archiver/blob/cc2f6a219f6563f450b0c00e8ccd651520b67406/src/main/java/org/apache/maven/archiver/MavenArchiver.java#L768">MavenArchiver</a>.
*
* @author Niels Basjes
* @author Moritz Halbritter
*/
class MavenBuildOutputTimestamp {
private static final Instant DATE_MIN = Instant.parse("1980-01-01T00:00:02Z");
private static final Instant DATE_MAX = Instant.parse("2099-12-31T23:59:59Z");
private final String timestamp;
/**
* Parse output timestamp configured for Reproducible Builds' archive entries.
*
* <p>
* Either as {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME} or as a
* number representing seconds since the epoch (like <a href=
* "https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
* @param outputTimestamp the value of {@code ${project.build.outputTimestamp}}
* (maybe {@code null})
* @return the parsed timestamp as an {@code Optional<Instant>}, {@code empty} if
* input is {@code null} or input contains only 1 character (not a number)
* Creates a new {@link MavenBuildOutputTimestamp}.
* @param timestamp timestamp or {@code null}
*/
MavenBuildOutputTimestamp(String timestamp) {
this.timestamp = timestamp;
}
/**
* Returns the parsed timestamp as an {@code FileTime}.
* @return the parsed timestamp as an {@code FileTime}, or {@code null}
* @throws IllegalArgumentException if the outputTimestamp is neither ISO 8601 nor an
* integer, or it's not within the valid range 1980-01-01T00:00:02Z to
* 2099-12-31T23:59:59Z
*/
public static Optional<Instant> parseBuildOutputTimestamp(String outputTimestamp) {
// Fail-fast on nulls
if (outputTimestamp == null) {
return Optional.empty();
FileTime toFileTime() {
Instant instant = toInstant();
if (instant == null) {
return null;
}
return FileTime.from(instant);
}
// Number representing seconds since the epoch
if (isNumeric(outputTimestamp)) {
return Optional.of(Instant.ofEpochSecond(Long.parseLong(outputTimestamp)));
/**
* Returns the parsed timestamp as an {@code Instant}.
* @return the parsed timestamp as an {@code Instant}, or {@code null}
* @throws IllegalArgumentException if the outputTimestamp is neither ISO 8601 nor an
* integer, or it's not within the valid range 1980-01-01T00:00:02Z to
* 2099-12-31T23:59:59Z
*/
Instant toInstant() {
if (!StringUtils.hasLength(this.timestamp)) {
return null;
}
// no timestamp configured (1 character configuration is useful to override a full
// value during pom
// inheritance)
if (outputTimestamp.length() < 2) {
return Optional.empty();
if (isNumeric(this.timestamp)) {
return Instant.ofEpochSecond(Long.parseLong(this.timestamp));
}
if (this.timestamp.length() < 2) {
return null;
}
try {
// Parse the date in UTC such as '2011-12-03T10:15:30Z' or with an offset
// '2019-10-05T20:37:42+06:00'.
final Instant date = OffsetDateTime.parse(outputTimestamp)
Instant instant = OffsetDateTime.parse(this.timestamp)
.withOffsetSameInstant(ZoneOffset.UTC)
.truncatedTo(ChronoUnit.SECONDS)
.toInstant();
if (date.isBefore(DATE_MIN) || date.isAfter(DATE_MAX)) {
throw new IllegalArgumentException(
"'" + date + "' is not within the valid range " + DATE_MIN + " to " + DATE_MAX);
if (instant.isBefore(DATE_MIN) || instant.isAfter(DATE_MAX)) {
throw new IllegalArgumentException(String
.format(String.format("'%s' is not within the valid range %s to %s", instant, DATE_MIN, DATE_MAX)));
}
return Optional.of(date);
return instant;
}
catch (DateTimeParseException pe) {
throw new IllegalArgumentException("Invalid project.build.outputTimestamp value '" + outputTimestamp + "'",
pe);
throw new IllegalArgumentException(String.format("Can't parse '%s' to instant", this.timestamp));
}
}
private static boolean isNumeric(String str) {
if (str.isEmpty()) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}

View File

@@ -21,7 +21,6 @@ import java.io.IOException;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
@@ -220,10 +219,13 @@ public class RepackageMojo extends AbstractPackagerMojo {
updateArtifact(source, target, repackager.getBackupFile());
}
private FileTime parseOutputTimestamp() {
return MavenBuildOutputTimestamp.parseBuildOutputTimestamp(this.outputTimestamp)
.map(instant -> FileTime.from(instant.getEpochSecond(), TimeUnit.SECONDS))
.orElse(null);
private FileTime parseOutputTimestamp() throws MojoExecutionException {
try {
return new MavenBuildOutputTimestamp(this.outputTimestamp).toFileTime();
}
catch (IllegalArgumentException ex) {
throw new MojoExecutionException("Invalid value for parameter 'outputTimestamp'", ex);
}
}
private Repackager getRepackager(File source) {