Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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<>();

Expand All @@ -68,7 +78,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);
}
}