mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +08:00
Use a Builder for BillOfMaterials
Closes gh-1012
This commit is contained in:
@@ -33,31 +33,125 @@ public class BillOfMaterials {
|
||||
|
||||
private final int order;
|
||||
|
||||
public BillOfMaterials(String groupId, String artifactId, VersionReference version) {
|
||||
this(groupId, artifactId, version, Integer.MAX_VALUE);
|
||||
protected BillOfMaterials(Builder builder) {
|
||||
this.groupId = builder.groupId;
|
||||
this.artifactId = builder.artifactId;
|
||||
this.version = builder.version;
|
||||
this.order = builder.order;
|
||||
}
|
||||
|
||||
public BillOfMaterials(String groupId, String artifactId, VersionReference version, int order) {
|
||||
this.groupId = groupId;
|
||||
this.artifactId = artifactId;
|
||||
this.version = version;
|
||||
this.order = order;
|
||||
/**
|
||||
* Initialize a new BOM {@link Builder} with the specified coordinates.
|
||||
* @param groupId the group ID of the bom
|
||||
* @param artifactId the artifact ID of the bom
|
||||
* @return a new builder
|
||||
*/
|
||||
public static Builder withCoordinates(String groupId, String artifactId) {
|
||||
return new Builder(groupId, artifactId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the group ID of the bom.
|
||||
* @return the group ID
|
||||
*/
|
||||
public String getGroupId() {
|
||||
return this.groupId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the artifact ID of the bom.
|
||||
* @return the artifact ID
|
||||
*/
|
||||
public String getArtifactId() {
|
||||
return this.artifactId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@linkplain VersionReference version reference} of the bom. Can be a
|
||||
* fixed value or refer to a property.
|
||||
* @return the version reference
|
||||
*/
|
||||
public VersionReference getVersion() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the order of this bom relative to other boms.
|
||||
* @return the bom order
|
||||
*/
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for a Bill of Materials.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private String groupId;
|
||||
|
||||
private String artifactId;
|
||||
|
||||
private VersionReference version;
|
||||
|
||||
private int order = Integer.MAX_VALUE;
|
||||
|
||||
protected Builder(String groupId, String artifactId) {
|
||||
this.groupId = groupId;
|
||||
this.artifactId = artifactId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the group ID of the bom.
|
||||
* @param groupId the group ID
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder groupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the artifact ID of the bom.
|
||||
* @param artifactId the artifact ID
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder artifactId(String artifactId) {
|
||||
this.artifactId = artifactId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@linkplain VersionReference version} of the bom.
|
||||
* @param version the version
|
||||
* @return this for method chaining
|
||||
* @see VersionReference#ofProperty(String)
|
||||
* @see VersionReference#ofValue(String)
|
||||
*/
|
||||
public Builder version(VersionReference version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the order of the bom relative to other boms. By default the order is
|
||||
* {@code Integer.MAX_VALUE} which means the bom has the lowest priority.
|
||||
* @param order the order of the bom
|
||||
* @return this for method chaining
|
||||
*/
|
||||
public Builder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a {@link BillOfMaterials} with the current state of this builder.
|
||||
* @return a {@link BillOfMaterials}
|
||||
*/
|
||||
public BillOfMaterials build() {
|
||||
return new BillOfMaterials(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -19,8 +19,6 @@ package io.spring.initializr.generator.buildsystem;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
|
||||
/**
|
||||
* A {@link BuildItemContainer} implementation for boms.
|
||||
*
|
||||
@@ -34,27 +32,12 @@ public class BomContainer extends BuildItemContainer<String, BillOfMaterials> {
|
||||
|
||||
/**
|
||||
* Register a {@link BillOfMaterials} with the specified {@code id} and a
|
||||
* {@link Integer#MAX_VALUE} order.
|
||||
* {@linkplain BillOfMaterials.Builder state}.
|
||||
* @param id the id of the bom
|
||||
* @param groupId the groupId
|
||||
* @param artifactId the artifactId
|
||||
* @param version the {@link VersionReference}
|
||||
* @param builder the state of the bom
|
||||
*/
|
||||
public void add(String id, String groupId, String artifactId, VersionReference version) {
|
||||
add(id, new BillOfMaterials(groupId, artifactId, version));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link BillOfMaterials} with the specified {@code id} and a custom
|
||||
* order.
|
||||
* @param id the id of the bom
|
||||
* @param groupId the groupId
|
||||
* @param artifactId the artifactId
|
||||
* @param version the {@link VersionReference}
|
||||
* @param order the order of the bom
|
||||
*/
|
||||
public void add(String id, String groupId, String artifactId, VersionReference version, int order) {
|
||||
add(id, new BillOfMaterials(groupId, artifactId, version, order));
|
||||
public void add(String id, BillOfMaterials.Builder builder) {
|
||||
add(id, builder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -31,8 +31,8 @@ class BomContainerTests {
|
||||
@Test
|
||||
void addBom() {
|
||||
BomContainer container = createTestContainer();
|
||||
container.add("root", "org.springframework.boot", "spring-boot-dependencies",
|
||||
VersionReference.ofProperty("spring-boot.version"));
|
||||
container.add("root", BillOfMaterials.withCoordinates("org.springframework.boot", "spring-boot-dependencies")
|
||||
.version(VersionReference.ofProperty("spring-boot.version")));
|
||||
assertThat(container.ids()).containsOnly("root");
|
||||
assertThat(container.items()).hasSize(1);
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
@@ -48,7 +48,8 @@ class BomContainerTests {
|
||||
@Test
|
||||
void addBomWithOrder() {
|
||||
BomContainer container = createTestContainer();
|
||||
container.add("custom", "com.example", "acme", VersionReference.ofValue("1.0.0"), 42);
|
||||
container.add("custom", BillOfMaterials.withCoordinates("com.example", "acme")
|
||||
.version(VersionReference.ofValue("1.0.0")).order(42));
|
||||
assertThat(container.ids()).containsOnly("custom");
|
||||
assertThat(container.items()).hasSize(1);
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
|
@@ -21,6 +21,7 @@ import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
@@ -397,7 +398,8 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithBom() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.boms().add("test", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"));
|
||||
build.boms().add("test", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||
.version(VersionReference.ofValue("1.0.0.RELEASE")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||
" mavenBom 'com.example:my-project-dependencies:1.0.0.RELEASE'", " }", "}");
|
||||
@@ -406,9 +408,10 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithOrderedBoms() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.boms().add("bom1", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"),
|
||||
5);
|
||||
build.boms().add("bom2", "com.example", "root-dependencies", VersionReference.ofProperty("root.version"), 2);
|
||||
build.boms().add("bom1", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||
.version(VersionReference.ofValue("1.0.0.RELEASE")).order(5));
|
||||
build.boms().add("bom2", BillOfMaterials.withCoordinates("com.example", "root-dependencies")
|
||||
.version(VersionReference.ofProperty("root.version")).order(2));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||
" mavenBom 'com.example:my-project-dependencies:1.0.0.RELEASE'",
|
||||
|
@@ -21,6 +21,7 @@ import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
@@ -405,7 +406,8 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithBom() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.boms().add("test", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"));
|
||||
build.boms().add("test", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||
.version(VersionReference.ofValue("1.0.0.RELEASE")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")", " }", "}");
|
||||
@@ -414,9 +416,10 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithOrderedBoms() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.boms().add("bom1", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"),
|
||||
5);
|
||||
build.boms().add("bom2", "com.example", "root-dependencies", VersionReference.ofProperty("root.version"), 2);
|
||||
build.boms().add("bom1", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||
.version(VersionReference.ofValue("1.0.0.RELEASE")).order(5));
|
||||
build.boms().add("bom2", BillOfMaterials.withCoordinates("com.example", "root-dependencies")
|
||||
.version(VersionReference.ofProperty("root.version")).order(2));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")",
|
||||
|
@@ -19,6 +19,7 @@ package io.spring.initializr.generator.buildsystem.maven;
|
||||
import java.io.StringWriter;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
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;
|
||||
@@ -288,7 +289,8 @@ class MavenBuildWriterTests {
|
||||
void pomWithBom() throws Exception {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.boms().add("test", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"));
|
||||
build.boms().add("test", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||
.version(VersionReference.ofValue("1.0.0.RELEASE")));
|
||||
generatePom(build, (pom) -> {
|
||||
NodeAssert dependency = pom.nodeAtPath("/project/dependencyManagement/dependencies/dependency");
|
||||
assertBom(dependency, "com.example", "my-project-dependencies", "1.0.0.RELEASE");
|
||||
@@ -299,9 +301,10 @@ class MavenBuildWriterTests {
|
||||
void pomWithOrderedBoms() throws Exception {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.boms().add("bom1", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"),
|
||||
5);
|
||||
build.boms().add("bom2", "com.example", "root-dependencies", VersionReference.ofProperty("root.version"), 2);
|
||||
build.boms().add("bom1", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||
.version(VersionReference.ofValue("1.0.0.RELEASE")).order(5));
|
||||
build.boms().add("bom2", BillOfMaterials.withCoordinates("com.example", "root-dependencies")
|
||||
.version(VersionReference.ofProperty("root.version")).order(2));
|
||||
generatePom(build, (pom) -> {
|
||||
NodeAssert dependencies = pom.nodeAtPath("/project/dependencyManagement/dependencies");
|
||||
NodeAssert firstBom = assertThat(dependencies).nodeAtPath("dependency[1]");
|
||||
|
Reference in New Issue
Block a user