[MNG-8054] Ability to force build number (#1414)

For snapshot deploys

---

https://issues.apache.org/jira/browse/MNG-8054
This commit is contained in:
Tamas Cservenak 2024-02-16 12:43:34 +01:00 committed by GitHub
parent 972b074656
commit 7b60c8d7dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 6 deletions

View File

@ -44,17 +44,21 @@ final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
private final Map<String, SnapshotVersion> 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 @@ final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
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();

View File

@ -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 @@ class RemoteSnapshotMetadataGenerator implements MetadataGenerator {
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);

View File

@ -33,6 +33,7 @@ import org.junit.jupiter.api.AfterEach;
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 @@ class RemoteSnapshotMetadataTest {
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 @@ class RemoteSnapshotMetadataTest {
Set<String> 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);
}
}