mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Use a Builder for MavenRepository
Closes gh-1013
This commit is contained in:
parent
285fec21a7
commit
664dd6c4eb
initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build
initializr-generator/src
main/java/io/spring/initializr/generator/buildsystem
test/java/io/spring/initializr/generator/buildsystem
initializr-metadata/src/main/java/io/spring/initializr/metadata/support
@ -28,6 +28,13 @@ import io.spring.initializr.generator.version.Version;
|
||||
*/
|
||||
class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer<Build> {
|
||||
|
||||
private static final MavenRepository SPRING_MILESTONES = MavenRepository
|
||||
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones").build();
|
||||
|
||||
private static final MavenRepository SPRING_SNAPSHOTS = MavenRepository
|
||||
.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot").name("Spring Snapshots")
|
||||
.snapshotsEnabled(true).build();
|
||||
|
||||
private final Version springBootVersion;
|
||||
|
||||
SpringBootVersionRepositoriesBuildCustomizer(Version springBootVersion) {
|
||||
@ -47,17 +54,13 @@ class SpringBootVersionRepositoriesBuildCustomizer implements BuildCustomizer<Bu
|
||||
}
|
||||
|
||||
private void addSnapshotRepository(Build build) {
|
||||
MavenRepository snapshotRepository = new MavenRepository("spring-snapshots", "Spring Snapshots",
|
||||
"https://repo.spring.io/snapshot", true);
|
||||
build.repositories().add(snapshotRepository);
|
||||
build.pluginRepositories().add(snapshotRepository);
|
||||
build.repositories().add(SPRING_SNAPSHOTS);
|
||||
build.pluginRepositories().add(SPRING_SNAPSHOTS);
|
||||
}
|
||||
|
||||
private void addMilestoneRepository(Build build) {
|
||||
MavenRepository milestoneRepository = new MavenRepository("spring-milestones", "Spring Milestones",
|
||||
"https://repo.spring.io/milestone");
|
||||
build.repositories().add(milestoneRepository);
|
||||
build.pluginRepositories().add(milestoneRepository);
|
||||
build.repositories().add(SPRING_MILESTONES);
|
||||
build.pluginRepositories().add(SPRING_MILESTONES);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ public class MavenRepository {
|
||||
/**
|
||||
* Maven Central.
|
||||
*/
|
||||
public static final MavenRepository MAVEN_CENTRAL = new MavenRepository("maven-central", "Maven Central",
|
||||
"https://repo.maven.apache.org/maven2");
|
||||
public static final MavenRepository MAVEN_CENTRAL = MavenRepository
|
||||
.withIdAndUrl("maven-central", "https://repo.maven.apache.org/maven2").name("Maven Central").build();
|
||||
|
||||
private final String id;
|
||||
|
||||
@ -37,31 +37,120 @@ public class MavenRepository {
|
||||
|
||||
private final boolean snapshotsEnabled;
|
||||
|
||||
public MavenRepository(String id, String name, String url) {
|
||||
this(id, name, url, false);
|
||||
protected MavenRepository(Builder builder) {
|
||||
this.id = builder.id;
|
||||
this.name = builder.name;
|
||||
this.url = builder.url;
|
||||
this.snapshotsEnabled = builder.snapshotsEnabled;
|
||||
}
|
||||
|
||||
public MavenRepository(String id, String name, String url, boolean snapshotsEnabled) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.snapshotsEnabled = snapshotsEnabled;
|
||||
/**
|
||||
* Initialize a new repository {@link Builder} with the specified id and url. The name
|
||||
* of the repository is initialized with the id.
|
||||
* @param id the identifier of the repository
|
||||
* @param url the url of the repository
|
||||
* @return a new builder
|
||||
*/
|
||||
public static Builder withIdAndUrl(String id, String url) {
|
||||
return new Builder(id, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the identifier of the repository.
|
||||
* @return the repository ID
|
||||
*/
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the repository.
|
||||
* @return the repository name
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the url of the repository.
|
||||
* @return the repository url
|
||||
*/
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether snapshots are enabled on the repository.
|
||||
* @return {@code true} to enable snapshots, {@code false} otherwise
|
||||
*/
|
||||
public boolean isSnapshotsEnabled() {
|
||||
return this.snapshotsEnabled;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String url;
|
||||
|
||||
private boolean snapshotsEnabled;
|
||||
|
||||
public Builder(String id, String url) {
|
||||
this.id = id;
|
||||
this.name = id;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the id of the repository.
|
||||
* @param id the identifier
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder id(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the repository.
|
||||
* @param name the name
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the url of the repository.
|
||||
* @param url the url
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder url(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether snapshots are enabled.
|
||||
* @param snapshotsEnabled whether snapshots are served by the repository
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder snapshotsEnabled(boolean snapshotsEnabled) {
|
||||
this.snapshotsEnabled = snapshotsEnabled;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a {@link MavenRepository} with the current state of this builder.
|
||||
* @return a {@link MavenRepository}
|
||||
*/
|
||||
public MavenRepository build() {
|
||||
return new MavenRepository(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,8 @@ import java.util.LinkedHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A {@link BuildItemContainer} implementation for repositories.
|
||||
* A {@link BuildItemContainer} implementation for {@link MavenRepository maven
|
||||
* repositories}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@ -39,25 +40,12 @@ public class MavenRepositoryContainer extends BuildItemContainer<String, MavenRe
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link MavenRepository repository} with snapshots disabled.
|
||||
* @param id the id of the repository
|
||||
* @param name the repository name
|
||||
* @param url the repository url
|
||||
* Register a {@link MavenRepository repository} with the specified
|
||||
* {@linkplain MavenRepository.Builder state}.
|
||||
* @param builder the state of the repository
|
||||
*/
|
||||
public void add(String id, String name, String url) {
|
||||
add(id, name, url, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link MavenRepository repository} and specify whether snapshots are
|
||||
* enabled.
|
||||
* @param id the id of the repository
|
||||
* @param name the repository name
|
||||
* @param url the repository url
|
||||
* @param snapshotsEnabled whether snapshots are enabled
|
||||
*/
|
||||
public void add(String id, String name, String url, boolean snapshotsEnabled) {
|
||||
add(id, new MavenRepository(id, name, url, snapshotsEnabled));
|
||||
public void add(MavenRepository.Builder builder) {
|
||||
add(builder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ class MavenRepositoryContainerTests {
|
||||
@Test
|
||||
void addMavenRepository() {
|
||||
MavenRepositoryContainer container = createTestContainer();
|
||||
container.add("test", "my repo", "https://example.com/releases");
|
||||
container.add(MavenRepository.withIdAndUrl("test", "https://example.com/releases").name("my repo"));
|
||||
assertThat(container.ids()).containsOnly("test");
|
||||
assertThat(container.items()).hasSize(1);
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
@ -45,7 +45,8 @@ class MavenRepositoryContainerTests {
|
||||
@Test
|
||||
void addMavenRepositoryInstance() {
|
||||
MavenRepositoryContainer container = createTestContainer();
|
||||
MavenRepository instance = new MavenRepository("test", "my repo", "https://example.com/releases");
|
||||
MavenRepository instance = MavenRepository.withIdAndUrl("test", "https://example.com/releases").name("my repo")
|
||||
.build();
|
||||
container.add(instance);
|
||||
assertThat(container.ids()).containsOnly("test");
|
||||
assertThat(container.items()).hasSize(1);
|
||||
@ -61,7 +62,8 @@ class MavenRepositoryContainerTests {
|
||||
@Test
|
||||
void addMavenRepositoryWithSnapshotsEnabled() {
|
||||
MavenRepositoryContainer container = createTestContainer();
|
||||
container.add("custom", "custom-snapshots", "https://example.com/snapshots", true);
|
||||
container.add(MavenRepository.withIdAndUrl("custom", "https://example.com/snapshots").name("custom-snapshots")
|
||||
.snapshotsEnabled(true));
|
||||
assertThat(container.ids()).containsOnly("custom");
|
||||
assertThat(container.items()).hasSize(1);
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
|
@ -25,6 +25,7 @@ import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
@ -114,7 +115,7 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.repositories().add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {", " maven { url 'https://repo.spring.io/milestone' }",
|
||||
"}");
|
||||
@ -123,7 +124,8 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithSnapshotRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/snapshot", true);
|
||||
build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
|
||||
.snapshotsEnabled(true));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {", " maven { url 'https://repo.spring.io/snapshot' }",
|
||||
"}");
|
||||
@ -132,7 +134,8 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.pluginRepositories()
|
||||
.add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).doesNotContain("repositories {");
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -53,7 +54,8 @@ class GroovyDslGradleSettingsWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.pluginRepositories().add(MavenRepository
|
||||
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones"));
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" maven { url 'https://repo.spring.io/milestone' }", " gradlePluginPortal()", " }",
|
||||
@ -66,7 +68,9 @@ class GroovyDslGradleSettingsWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithSnapshotPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/snapshot", true);
|
||||
build.pluginRepositories()
|
||||
.add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
|
||||
.name("Spring Snapshots").snapshotsEnabled(true));
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" maven { url 'https://repo.spring.io/snapshot' }", " gradlePluginPortal()", " }",
|
||||
|
@ -25,6 +25,7 @@ import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
@ -125,7 +126,7 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.repositories().add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/milestone\") }", "}");
|
||||
@ -134,7 +135,8 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithSnapshotRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/snapshot", true);
|
||||
build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
|
||||
.snapshotsEnabled(true));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/snapshot\") }", "}");
|
||||
@ -143,7 +145,8 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.pluginRepositories()
|
||||
.add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).doesNotContain("repositories {");
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -52,7 +53,8 @@ class KotlinDslGradleSettingsWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.pluginRepositories().add(MavenRepository
|
||||
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones"));
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/milestone\") }", " gradlePluginPortal()",
|
||||
@ -65,7 +67,9 @@ class KotlinDslGradleSettingsWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithSnapshotPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/snapshot", true);
|
||||
build.pluginRepositories()
|
||||
.add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
|
||||
.name("Spring Snapshots").snapshotsEnabled(true));
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/snapshot\") }", " gradlePluginPortal()",
|
||||
|
@ -23,6 +23,7 @@ import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
@ -472,7 +473,8 @@ class MavenBuildWriterTests {
|
||||
void pomWithRepository() throws Exception {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.repositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.repositories().add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone")
|
||||
.name("Spring Milestones"));
|
||||
generatePom(build, (pom) -> {
|
||||
assertThat(pom).textAtPath("/project/repositories/repository/id").isEqualTo("spring-milestones");
|
||||
assertThat(pom).textAtPath("/project/repositories/repository/name").isEqualTo("Spring Milestones");
|
||||
@ -487,7 +489,8 @@ class MavenBuildWriterTests {
|
||||
void pomWithPluginRepository() throws Exception {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones", "https://repo.spring.io/milestone");
|
||||
build.pluginRepositories().add(MavenRepository
|
||||
.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones"));
|
||||
generatePom(build, (pom) -> {
|
||||
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/id")
|
||||
.isEqualTo("spring-milestones");
|
||||
@ -504,7 +507,8 @@ class MavenBuildWriterTests {
|
||||
void pomWithSnapshotRepository() throws Exception {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.repositories().add("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/snapshot", true);
|
||||
build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
|
||||
.name("Spring Snapshots").snapshotsEnabled(true));
|
||||
generatePom(build, (pom) -> {
|
||||
assertThat(pom).textAtPath("/project/repositories/repository/id").isEqualTo("spring-snapshots");
|
||||
assertThat(pom).textAtPath("/project/repositories/repository/name").isEqualTo("Spring Snapshots");
|
||||
@ -519,7 +523,9 @@ class MavenBuildWriterTests {
|
||||
void pomWithSnapshotPluginRepository() throws Exception {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.pluginRepositories().add("spring-snapshots", "Spring Snapshots", "https://repo.spring.io/snapshot", true);
|
||||
build.pluginRepositories()
|
||||
.add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot")
|
||||
.name("Spring Snapshots").snapshotsEnabled(true));
|
||||
generatePom(build, (pom) -> {
|
||||
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/id").isEqualTo("spring-snapshots");
|
||||
assertThat(pom).textAtPath("/project/pluginRepositories/pluginRepository/name")
|
||||
|
@ -93,8 +93,9 @@ public final class MetadataBuildItemMapper {
|
||||
if (repository == null) {
|
||||
return null;
|
||||
}
|
||||
return new io.spring.initializr.generator.buildsystem.MavenRepository(id, repository.getName(),
|
||||
repository.getUrl().toExternalForm(), repository.isSnapshotsEnabled());
|
||||
return io.spring.initializr.generator.buildsystem.MavenRepository
|
||||
.withIdAndUrl(id, repository.getUrl().toExternalForm()).name(repository.getName())
|
||||
.snapshotsEnabled(repository.isSnapshotsEnabled()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user