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..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 @@ -42,8 +42,18 @@ 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"); + 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<>(); @@ -68,7 +78,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); + } }