From 6f6a07118e5637d70b7f210472518a775c304a3d Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 16 Feb 2024 10:52:52 +0100 Subject: [PATCH 1/2] [MNG-8054] Ability to force build number For snapshot deploys --- https://issues.apache.org/jira/browse/MNG-8054 --- .../internal/RemoteSnapshotMetadata.java | 12 ++++++---- .../RemoteSnapshotMetadataGenerator.java | 5 +++- .../internal/RemoteSnapshotMetadataTest.java | 24 ++++++++++++++++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java index 578105719b1b..2ad4a379b0fe 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java @@ -44,17 +44,21 @@ final class RemoteSnapshotMetadata extends MavenSnapshotMetadata { private final Map versions = new LinkedHashMap<>(); - RemoteSnapshotMetadata(Artifact artifact, Date timestamp) { + private final Integer buildNumber; + + RemoteSnapshotMetadata(Artifact artifact, Date timestamp, Integer buildNumber) { super(createRepositoryMetadata(artifact), null, timestamp); + this.buildNumber = buildNumber; } - private RemoteSnapshotMetadata(Metadata metadata, File file, Date timestamp) { + private RemoteSnapshotMetadata(Metadata metadata, File file, Date timestamp, Integer buildNumber) { super(metadata, file, timestamp); + this.buildNumber = buildNumber; } @Override public MavenMetadata setFile(File file) { - return new RemoteSnapshotMetadata(metadata, file, timestamp); + return new RemoteSnapshotMetadata(metadata, file, timestamp, buildNumber); } public String getExpandedVersion(Artifact artifact) { @@ -73,7 +77,7 @@ protected void merge(Metadata recessive) { utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE); snapshot = new Snapshot(); - snapshot.setBuildNumber(getBuildNumber(recessive) + 1); + snapshot.setBuildNumber(buildNumber != null ? buildNumber : getBuildNumber(recessive) + 1); snapshot.setTimestamp(utcDateFormatter.format(timestamp)); Versioning versioning = new Versioning(); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java index 2fcef266455c..29bb0b2d23f1 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java @@ -42,8 +42,11 @@ class RemoteSnapshotMetadataGenerator implements MetadataGenerator { private final Date timestamp; + private final Integer buildNumber; + RemoteSnapshotMetadataGenerator(RepositorySystemSession session, DeployRequest request) { timestamp = (Date) ConfigUtils.getObject(session, new Date(), "maven.startTime"); + buildNumber = (Integer) ConfigUtils.getObject(session, null, "maven.buildNumber"); snapshots = new LinkedHashMap<>(); @@ -68,7 +71,7 @@ public Collection prepare(Collection art Object key = RemoteSnapshotMetadata.getKey(artifact); RemoteSnapshotMetadata snapshotMetadata = snapshots.get(key); if (snapshotMetadata == null) { - snapshotMetadata = new RemoteSnapshotMetadata(artifact, timestamp); + snapshotMetadata = new RemoteSnapshotMetadata(artifact, timestamp, buildNumber); snapshots.put(key, snapshotMetadata); } snapshotMetadata.bind(artifact); diff --git a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataTest.java b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataTest.java index 6d19de88b561..cc58b3d80d70 100644 --- a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataTest.java +++ b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataTest.java @@ -33,6 +33,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; class RemoteSnapshotMetadataTest { @@ -62,7 +63,8 @@ static String gregorianDate() { void gregorianCalendarIsUsed() { String dateBefore = gregorianDate(); - RemoteSnapshotMetadata metadata = new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date()); + RemoteSnapshotMetadata metadata = + new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date(), null); metadata.merge(new Metadata()); String dateAfter = gregorianDate(); @@ -74,4 +76,24 @@ void gregorianCalendarIsUsed() { Set expected = new HashSet<>(Arrays.asList(dateBefore, dateAfter)); assertTrue(expected.contains(datePart), "Expected " + datePart + " to be in " + expected); } + + @Test + void buildNumberNotSet() { + RemoteSnapshotMetadata metadata = + new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date(), null); + metadata.merge(new Metadata()); + + int buildNumber = metadata.metadata.getVersioning().getSnapshot().getBuildNumber(); + assertEquals(1, buildNumber); + } + + @Test + void buildNumberSet() { + RemoteSnapshotMetadata metadata = + new RemoteSnapshotMetadata(new DefaultArtifact("a:b:1-SNAPSHOT"), new Date(), 42); + metadata.merge(new Metadata()); + + int buildNumber = metadata.metadata.getVersioning().getSnapshot().getBuildNumber(); + assertEquals(42, buildNumber); + } } From 520882046c08c97b3efc4b6a483ddd2834cf5cb4 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 16 Feb 2024 12:04:49 +0100 Subject: [PATCH 2/2] Fix When value set via user properties, it is String not Integer --- .../internal/RemoteSnapshotMetadataGenerator.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java index 29bb0b2d23f1..d957b2978685 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadataGenerator.java @@ -46,7 +46,14 @@ class RemoteSnapshotMetadataGenerator implements MetadataGenerator { RemoteSnapshotMetadataGenerator(RepositorySystemSession session, DeployRequest request) { timestamp = (Date) ConfigUtils.getObject(session, new Date(), "maven.startTime"); - buildNumber = (Integer) ConfigUtils.getObject(session, null, "maven.buildNumber"); + Object bn = ConfigUtils.getObject(session, null, "maven.buildNumber"); + if (bn instanceof Integer) { + this.buildNumber = (Integer) bn; + } else if (bn instanceof String) { + this.buildNumber = Integer.valueOf((String) bn); + } else { + this.buildNumber = null; + } snapshots = new LinkedHashMap<>();