Skip to content

Commit

Permalink
Merge pull request #1521 from inanemed
Browse files Browse the repository at this point in the history
* pr/1521:
  Polish "Revise repository addition logic for snapshot versions"
  Revise repository addition logic for snapshot versions

Closes gh-1521
  • Loading branch information
mhalbritter committed Apr 16, 2024
2 parents 5985ae0 + 63ec9cd commit 64dde52
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@
* of Spring Boot being used.
*
* @author Andy Wilkinson
* @author Moritz Halbritter
*/
class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer<Build> {

private static final MavenRepository SPRING_MILESTONES = MavenRepository
static final MavenRepository SPRING_MILESTONES = MavenRepository
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone")
.name("Spring Milestones")
.onlyReleases()
.build();

private static final MavenRepository SPRING_SNAPSHOTS = MavenRepository
static final MavenRepository SPRING_SNAPSHOTS = MavenRepository
.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
.name("Spring Snapshots")
.onlySnapshots()
Expand All @@ -49,17 +50,40 @@ class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer<Bu
@Override
public void customize(Build build) {
build.repositories().add("maven-central");
if (this.springBootVersion.getQualifier() != null) {
String qualifier = this.springBootVersion.getQualifier().getId();
if (!qualifier.equals("RELEASE")) {
addMilestoneRepository(build);
if (qualifier.contains("SNAPSHOT")) {
switch (getReleaseType()) {
case MILESTONE -> addMilestoneRepository(build);
case SNAPSHOT -> {
if (isMaintenanceRelease()) {
addSnapshotRepository(build);
}
else {
addMilestoneRepository(build);
addSnapshotRepository(build);
}
}
}
}

private ReleaseType getReleaseType() {
Version.Qualifier qualifier = this.springBootVersion.getQualifier();
if (qualifier == null) {
return ReleaseType.GA;
}
String id = qualifier.getId();
if ("RELEASE".equals(id)) {
return ReleaseType.GA;
}
if (id.contains("SNAPSHOT")) {
return ReleaseType.SNAPSHOT;
}
return ReleaseType.MILESTONE;
}

private boolean isMaintenanceRelease() {
Integer patch = this.springBootVersion.getPatch();
return patch != null && patch > 0;
}

private void addSnapshotRepository(Build build) {
build.repositories().add(SPRING_SNAPSHOTS);
build.pluginRepositories().add(SPRING_SNAPSHOTS);
Expand All @@ -70,4 +94,10 @@ private void addMilestoneRepository(Build build) {
build.pluginRepositories().add(SPRING_MILESTONES);
}

private enum ReleaseType {

GA, MILESTONE, SNAPSHOT

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package io.spring.initializr.generator.spring.build;

import java.util.List;
import java.util.stream.Collectors;

import io.spring.initializr.generator.buildsystem.MavenRepository;
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.version.Version;
Expand All @@ -30,6 +27,7 @@
* Tests for {@link SpringBootVersionRepositoriesBuildCustomizer}.
*
* @author Andy Wilkinson
* @author Moritz Halbritter
*/
class SpringBootVersionRepositoriesBuildCustomizerTests {

Expand All @@ -51,66 +49,58 @@ void addMavenCentralWhenUsingSemVerRelease() {
void addMavenCentralAndMilestonesWhenUsingMilestone() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.0.M1")).customize(build);
assertMavenCentralAndMilestonesRepositories(build);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_MILESTONES);
}

@Test
void addMavenCentralAndMilestonesWhenUsingSemVerMilestone() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.0-M1")).customize(build);
assertMavenCentralAndMilestonesRepositories(build);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_MILESTONES);
}

@Test
void addMavenCentralAndMilestonesWhenUsingReleaseCandidate() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.0.RC1")).customize(build);
assertMavenCentralAndMilestonesRepositories(build);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_MILESTONES);
}

@Test
void addMavenCentralAndMilestonesWhenUsingSemVerReleaseCandidate() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.0-RC1")).customize(build);
assertMavenCentralAndMilestonesRepositories(build);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_MILESTONES);
}

@Test
void addMavenCentralAndNonReleaseWhenUsingSnapshot() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.0.BUILD-SNAPSHOT")).customize(build);
assertNonReleaseRepositories(build);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_MILESTONES,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_SNAPSHOTS);
}

@Test
void addMavenCentralAndNonReleaseWhenUsingSemVerSnapshot() {
void firstSnapshotReleaseShouldAddMilestoneRepository() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.0-SNAPSHOT")).customize(build);
assertNonReleaseRepositories(build);
}

private void assertNonReleaseRepositories(MavenBuild build) {
List<MavenRepository> repositories = build.repositories().items().collect(Collectors.toList());
assertThat(repositories).hasSize(3);
assertThat(repositories.get(0)).isEqualTo(MavenRepository.MAVEN_CENTRAL);
assertThat(repositories.get(1)).hasFieldOrPropertyWithValue("id", "spring-milestones")
.hasFieldOrPropertyWithValue("name", "Spring Milestones")
.hasFieldOrPropertyWithValue("url", "https://repo.spring.io/milestone")
.hasFieldOrPropertyWithValue("snapshotsEnabled", false);
assertThat(repositories.get(2)).hasFieldOrPropertyWithValue("id", "spring-snapshots")
.hasFieldOrPropertyWithValue("name", "Spring Snapshots")
.hasFieldOrPropertyWithValue("url", "https://repo.spring.io/snapshot")
.hasFieldOrPropertyWithValue("snapshotsEnabled", true);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_MILESTONES,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_SNAPSHOTS);
}

private void assertMavenCentralAndMilestonesRepositories(MavenBuild build) {
List<MavenRepository> repositories = build.repositories().items().collect(Collectors.toList());
assertThat(repositories).hasSize(2);
assertThat(repositories.get(0)).isEqualTo(MavenRepository.MAVEN_CENTRAL);
assertThat(repositories.get(1)).hasFieldOrPropertyWithValue("id", "spring-milestones")
.hasFieldOrPropertyWithValue("name", "Spring Milestones")
.hasFieldOrPropertyWithValue("url", "https://repo.spring.io/milestone")
.hasFieldOrPropertyWithValue("snapshotsEnabled", false);
@Test
void maintenanceReleasesShouldNotAddMilestoneRepository() {
MavenBuild build = new MavenBuild();
new SpringBootVersionRepositoriesBuildCustomizer(Version.parse("2.1.1-SNAPSHOT")).customize(build);
assertThat(build.repositories().items()).containsExactly(MavenRepository.MAVEN_CENTRAL,
SpringBootVersionRepositoriesBuildCustomizer.SPRING_SNAPSHOTS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.spring.initializr.generator.buildsystem;

import java.util.Objects;

/**
* A Maven repository.
*
Expand Down Expand Up @@ -102,6 +104,25 @@ public boolean isSnapshotsEnabled() {
return this.snapshotsEnabled;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MavenRepository that = (MavenRepository) o;
return this.releasesEnabled == that.releasesEnabled && this.snapshotsEnabled == that.snapshotsEnabled
&& Objects.equals(this.id, that.id) && Objects.equals(this.name, that.name)
&& Objects.equals(this.url, that.url);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.name, this.url, this.releasesEnabled, this.snapshotsEnabled);
}

public static class Builder {

private String id;
Expand Down

0 comments on commit 64dde52

Please sign in to comment.