Polish "Use the Maven Archiver implementation for parsing outputTimestamp"
See gh-37438
This commit is contained in:
parent
a1b08e1819
commit
a3611ae9f3
|
@ -16,91 +16,97 @@
|
||||||
|
|
||||||
package org.springframework.boot.maven;
|
package org.springframework.boot.maven;
|
||||||
|
|
||||||
|
import java.nio.file.attribute.FileTime;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public final class MavenBuildOutputTimestamp {
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
private MavenBuildOutputTimestamp() {
|
/**
|
||||||
// Utility class
|
* Parse output timestamp configured for Reproducible Builds' archive entries.
|
||||||
}
|
* <p>
|
||||||
|
* Either as {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME} or as a
|
||||||
// This implementation was copied from the maven-archiver
|
* number representing seconds since the epoch (like <a href=
|
||||||
// https://github.com/apache/maven-archiver/blob/cc2f6a219f6563f450b0c00e8ccd651520b67406/src/main/java/org/apache/maven/archiver/MavenArchiver.java#L768
|
* "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_MIN = Instant.parse("1980-01-01T00:00:02Z");
|
||||||
|
|
||||||
private static final Instant DATE_MAX = Instant.parse("2099-12-31T23:59:59Z");
|
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.
|
* Creates a new {@link MavenBuildOutputTimestamp}.
|
||||||
*
|
* @param timestamp timestamp or {@code null}
|
||||||
* <p>
|
*/
|
||||||
* Either as {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME} or as a
|
MavenBuildOutputTimestamp(String timestamp) {
|
||||||
* number representing seconds since the epoch (like <a href=
|
this.timestamp = timestamp;
|
||||||
* "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
|
* Returns the parsed timestamp as an {@code FileTime}.
|
||||||
* input is {@code null} or input contains only 1 character (not a number)
|
* @return the parsed timestamp as an {@code FileTime}, or {@code null}
|
||||||
* @throws IllegalArgumentException if the outputTimestamp is neither ISO 8601 nor an
|
* @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
|
* integer, or it's not within the valid range 1980-01-01T00:00:02Z to
|
||||||
* 2099-12-31T23:59:59Z
|
* 2099-12-31T23:59:59Z
|
||||||
*/
|
*/
|
||||||
public static Optional<Instant> parseBuildOutputTimestamp(String outputTimestamp) {
|
FileTime toFileTime() {
|
||||||
// Fail-fast on nulls
|
Instant instant = toInstant();
|
||||||
if (outputTimestamp == null) {
|
if (instant == null) {
|
||||||
return Optional.empty();
|
return null;
|
||||||
}
|
}
|
||||||
|
return FileTime.from(instant);
|
||||||
|
}
|
||||||
|
|
||||||
// Number representing seconds since the epoch
|
/**
|
||||||
if (isNumeric(outputTimestamp)) {
|
* Returns the parsed timestamp as an {@code Instant}.
|
||||||
return Optional.of(Instant.ofEpochSecond(Long.parseLong(outputTimestamp)));
|
* @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;
|
||||||
}
|
}
|
||||||
|
if (isNumeric(this.timestamp)) {
|
||||||
// no timestamp configured (1 character configuration is useful to override a full
|
return Instant.ofEpochSecond(Long.parseLong(this.timestamp));
|
||||||
// value during pom
|
}
|
||||||
// inheritance)
|
if (this.timestamp.length() < 2) {
|
||||||
if (outputTimestamp.length() < 2) {
|
return null;
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse the date in UTC such as '2011-12-03T10:15:30Z' or with an offset
|
Instant instant = OffsetDateTime.parse(this.timestamp)
|
||||||
// '2019-10-05T20:37:42+06:00'.
|
|
||||||
final Instant date = OffsetDateTime.parse(outputTimestamp)
|
|
||||||
.withOffsetSameInstant(ZoneOffset.UTC)
|
.withOffsetSameInstant(ZoneOffset.UTC)
|
||||||
.truncatedTo(ChronoUnit.SECONDS)
|
.truncatedTo(ChronoUnit.SECONDS)
|
||||||
.toInstant();
|
.toInstant();
|
||||||
|
if (instant.isBefore(DATE_MIN) || instant.isAfter(DATE_MAX)) {
|
||||||
if (date.isBefore(DATE_MIN) || date.isAfter(DATE_MAX)) {
|
throw new IllegalArgumentException(String
|
||||||
throw new IllegalArgumentException(
|
.format(String.format("'%s' is not within the valid range %s to %s", instant, DATE_MIN, DATE_MAX)));
|
||||||
"'" + date + "' is not within the valid range " + DATE_MIN + " to " + DATE_MAX);
|
|
||||||
}
|
}
|
||||||
return Optional.of(date);
|
return instant;
|
||||||
}
|
}
|
||||||
catch (DateTimeParseException pe) {
|
catch (DateTimeParseException pe) {
|
||||||
throw new IllegalArgumentException("Invalid project.build.outputTimestamp value '" + outputTimestamp + "'",
|
throw new IllegalArgumentException(String.format("Can't parse '%s' to instant", this.timestamp));
|
||||||
pe);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isNumeric(String str) {
|
private static boolean isNumeric(String str) {
|
||||||
|
|
||||||
if (str.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (char c : str.toCharArray()) {
|
for (char c : str.toCharArray()) {
|
||||||
if (!Character.isDigit(c)) {
|
if (!Character.isDigit(c)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ import java.io.IOException;
|
||||||
import java.nio.file.attribute.FileTime;
|
import java.nio.file.attribute.FileTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.maven.artifact.Artifact;
|
import org.apache.maven.artifact.Artifact;
|
||||||
|
@ -220,10 +219,13 @@ public class RepackageMojo extends AbstractPackagerMojo {
|
||||||
updateArtifact(source, target, repackager.getBackupFile());
|
updateArtifact(source, target, repackager.getBackupFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileTime parseOutputTimestamp() {
|
private FileTime parseOutputTimestamp() throws MojoExecutionException {
|
||||||
return MavenBuildOutputTimestamp.parseBuildOutputTimestamp(this.outputTimestamp)
|
try {
|
||||||
.map(instant -> FileTime.from(instant.getEpochSecond(), TimeUnit.SECONDS))
|
return new MavenBuildOutputTimestamp(this.outputTimestamp).toFileTime();
|
||||||
.orElse(null);
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
|
throw new MojoExecutionException("Invalid value for parameter 'outputTimestamp'", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Repackager getRepackager(File source) {
|
private Repackager getRepackager(File source) {
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.maven;
|
||||||
|
|
||||||
|
import java.nio.file.attribute.FileTime;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MavenBuildOutputTimestamp}.
|
||||||
|
*
|
||||||
|
* @author Moritz Halbritter
|
||||||
|
*/
|
||||||
|
class MavenBuildOutputTimestampTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldParseNull() {
|
||||||
|
assertThat(parse(null)).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldParseSingleDigit() {
|
||||||
|
assertThat(parse("0")).isEqualTo(Instant.parse("1970-01-01T00:00:00Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotParseSingleCharacter() {
|
||||||
|
assertThat(parse("a")).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldParseIso8601() {
|
||||||
|
assertThat(parse("2011-12-03T10:15:30Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldParseIso8601WithMilliseconds() {
|
||||||
|
assertThat(parse("2011-12-03T10:15:30.12345Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldFailIfIso8601BeforeMin() {
|
||||||
|
assertThatIllegalArgumentException().isThrownBy(() -> parse("1970-01-01T00:00:00Z"))
|
||||||
|
.withMessage(
|
||||||
|
"'1970-01-01T00:00:00Z' is not within the valid range 1980-01-01T00:00:02Z to 2099-12-31T23:59:59Z");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldFailIfIso8601AfterMax() {
|
||||||
|
assertThatIllegalArgumentException().isThrownBy(() -> parse("2100-01-01T00:00:00Z"))
|
||||||
|
.withMessage(
|
||||||
|
"'2100-01-01T00:00:00Z' is not within the valid range 1980-01-01T00:00:02Z to 2099-12-31T23:59:59Z");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldFailIfNotIso8601() {
|
||||||
|
assertThatIllegalArgumentException().isThrownBy(() -> parse("dummy"))
|
||||||
|
.withMessage("Can't parse 'dummy' to instant");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldParseIso8601WithOffset() {
|
||||||
|
assertThat(parse("2019-10-05T20:37:42+06:00")).isEqualTo(Instant.parse("2019-10-05T14:37:42Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldParseToFileTime() {
|
||||||
|
assertThat(parseFileTime(null)).isEqualTo(null);
|
||||||
|
assertThat(parseFileTime("0")).isEqualTo(FileTime.fromMillis(0));
|
||||||
|
assertThat(parseFileTime("2019-10-05T14:37:42Z")).isEqualTo(FileTime.fromMillis(1570286262000L));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Instant parse(String timestamp) {
|
||||||
|
return new MavenBuildOutputTimestamp(timestamp).toInstant();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FileTime parseFileTime(String timestamp) {
|
||||||
|
return new MavenBuildOutputTimestamp(timestamp).toFileTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue