Skip to content

Commit

Permalink
[MNG-8054] Ability to force build number
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Feb 16, 2024
1 parent 972b074 commit 6f6a071
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();

Expand All @@ -68,7 +71,7 @@ public Collection<? extends Metadata> prepare(Collection<? extends Artifact> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -74,4 +76,24 @@ void gregorianCalendarIsUsed() {
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);
}
}

0 comments on commit 6f6a071

Please sign in to comment.