diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizer.java index 90b9ac0465..8ae32357ca 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizer.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizer.java @@ -25,16 +25,17 @@ * of Spring Boot being used. * * @author Andy Wilkinson + * @author Moritz Halbritter */ class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer { - 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() @@ -49,17 +50,40 @@ class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer 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); @@ -70,4 +94,10 @@ private void addMilestoneRepository(Build build) { build.pluginRepositories().add(SPRING_MILESTONES); } + private enum ReleaseType { + + GA, MILESTONE, SNAPSHOT + + } + } diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizerTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizerTests.java index 7631ee7fab..22a0523d63 100644 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizerTests.java +++ b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/SpringBootVersionRepositoriesBuildCustomizerTests.java @@ -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; @@ -30,6 +27,7 @@ * Tests for {@link SpringBootVersionRepositoriesBuildCustomizer}. * * @author Andy Wilkinson + * @author Moritz Halbritter */ class SpringBootVersionRepositoriesBuildCustomizerTests { @@ -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 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 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); } } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java index d27c2aa0a4..c9ccf521c0 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java @@ -16,6 +16,8 @@ package io.spring.initializr.generator.buildsystem; +import java.util.Objects; + /** * A Maven repository. * @@ -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;