mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 01:58:16 +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;
|
private final int order;
|
||||||
|
|
||||||
public BillOfMaterials(String groupId, String artifactId, VersionReference version) {
|
protected BillOfMaterials(Builder builder) {
|
||||||
this(groupId, artifactId, version, Integer.MAX_VALUE);
|
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;
|
* Initialize a new BOM {@link Builder} with the specified coordinates.
|
||||||
this.artifactId = artifactId;
|
* @param groupId the group ID of the bom
|
||||||
this.version = version;
|
* @param artifactId the artifact ID of the bom
|
||||||
this.order = order;
|
* @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() {
|
public String getGroupId() {
|
||||||
return this.groupId;
|
return this.groupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the artifact ID of the bom.
|
||||||
|
* @return the artifact ID
|
||||||
|
*/
|
||||||
public String getArtifactId() {
|
public String getArtifactId() {
|
||||||
return this.artifactId;
|
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() {
|
public VersionReference getVersion() {
|
||||||
return this.version;
|
return this.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the order of this bom relative to other boms.
|
||||||
|
* @return the bom order
|
||||||
|
*/
|
||||||
public int getOrder() {
|
public int getOrder() {
|
||||||
return this.order;
|
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.LinkedHashMap;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import io.spring.initializr.generator.version.VersionReference;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link BuildItemContainer} implementation for boms.
|
* 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
|
* 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 id the id of the bom
|
||||||
* @param groupId the groupId
|
* @param builder the state of the bom
|
||||||
* @param artifactId the artifactId
|
|
||||||
* @param version the {@link VersionReference}
|
|
||||||
*/
|
*/
|
||||||
public void add(String id, String groupId, String artifactId, VersionReference version) {
|
public void add(String id, BillOfMaterials.Builder builder) {
|
||||||
add(id, new BillOfMaterials(groupId, artifactId, version));
|
add(id, builder.build());
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -31,8 +31,8 @@ class BomContainerTests {
|
|||||||
@Test
|
@Test
|
||||||
void addBom() {
|
void addBom() {
|
||||||
BomContainer container = createTestContainer();
|
BomContainer container = createTestContainer();
|
||||||
container.add("root", "org.springframework.boot", "spring-boot-dependencies",
|
container.add("root", BillOfMaterials.withCoordinates("org.springframework.boot", "spring-boot-dependencies")
|
||||||
VersionReference.ofProperty("spring-boot.version"));
|
.version(VersionReference.ofProperty("spring-boot.version")));
|
||||||
assertThat(container.ids()).containsOnly("root");
|
assertThat(container.ids()).containsOnly("root");
|
||||||
assertThat(container.items()).hasSize(1);
|
assertThat(container.items()).hasSize(1);
|
||||||
assertThat(container.isEmpty()).isFalse();
|
assertThat(container.isEmpty()).isFalse();
|
||||||
@@ -48,7 +48,8 @@ class BomContainerTests {
|
|||||||
@Test
|
@Test
|
||||||
void addBomWithOrder() {
|
void addBomWithOrder() {
|
||||||
BomContainer container = createTestContainer();
|
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.ids()).containsOnly("custom");
|
||||||
assertThat(container.items()).hasSize(1);
|
assertThat(container.items()).hasSize(1);
|
||||||
assertThat(container.isEmpty()).isFalse();
|
assertThat(container.isEmpty()).isFalse();
|
||||||
|
@@ -21,6 +21,7 @@ import java.io.StringWriter;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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;
|
||||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||||
@@ -397,7 +398,8 @@ class GroovyDslGradleBuildWriterTests {
|
|||||||
@Test
|
@Test
|
||||||
void gradleBuildWithBom() throws IOException {
|
void gradleBuildWithBom() throws IOException {
|
||||||
GradleBuild build = new GradleBuild();
|
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);
|
List<String> lines = generateBuild(build);
|
||||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||||
" mavenBom 'com.example:my-project-dependencies:1.0.0.RELEASE'", " }", "}");
|
" mavenBom 'com.example:my-project-dependencies:1.0.0.RELEASE'", " }", "}");
|
||||||
@@ -406,9 +408,10 @@ class GroovyDslGradleBuildWriterTests {
|
|||||||
@Test
|
@Test
|
||||||
void gradleBuildWithOrderedBoms() throws IOException {
|
void gradleBuildWithOrderedBoms() throws IOException {
|
||||||
GradleBuild build = new GradleBuild();
|
GradleBuild build = new GradleBuild();
|
||||||
build.boms().add("bom1", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"),
|
build.boms().add("bom1", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||||
5);
|
.version(VersionReference.ofValue("1.0.0.RELEASE")).order(5));
|
||||||
build.boms().add("bom2", "com.example", "root-dependencies", VersionReference.ofProperty("root.version"), 2);
|
build.boms().add("bom2", BillOfMaterials.withCoordinates("com.example", "root-dependencies")
|
||||||
|
.version(VersionReference.ofProperty("root.version")).order(2));
|
||||||
List<String> lines = generateBuild(build);
|
List<String> lines = generateBuild(build);
|
||||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||||
" mavenBom 'com.example:my-project-dependencies:1.0.0.RELEASE'",
|
" 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.Arrays;
|
||||||
import java.util.List;
|
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;
|
||||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||||
@@ -405,7 +406,8 @@ class KotlinDslGradleBuildWriterTests {
|
|||||||
@Test
|
@Test
|
||||||
void gradleBuildWithBom() throws IOException {
|
void gradleBuildWithBom() throws IOException {
|
||||||
GradleBuild build = new GradleBuild();
|
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);
|
List<String> lines = generateBuild(build);
|
||||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||||
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")", " }", "}");
|
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")", " }", "}");
|
||||||
@@ -414,9 +416,10 @@ class KotlinDslGradleBuildWriterTests {
|
|||||||
@Test
|
@Test
|
||||||
void gradleBuildWithOrderedBoms() throws IOException {
|
void gradleBuildWithOrderedBoms() throws IOException {
|
||||||
GradleBuild build = new GradleBuild();
|
GradleBuild build = new GradleBuild();
|
||||||
build.boms().add("bom1", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"),
|
build.boms().add("bom1", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||||
5);
|
.version(VersionReference.ofValue("1.0.0.RELEASE")).order(5));
|
||||||
build.boms().add("bom2", "com.example", "root-dependencies", VersionReference.ofProperty("root.version"), 2);
|
build.boms().add("bom2", BillOfMaterials.withCoordinates("com.example", "root-dependencies")
|
||||||
|
.version(VersionReference.ofProperty("root.version")).order(2));
|
||||||
List<String> lines = generateBuild(build);
|
List<String> lines = generateBuild(build);
|
||||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||||
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")",
|
" 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.io.StringWriter;
|
||||||
import java.util.function.Consumer;
|
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;
|
||||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||||
@@ -288,7 +289,8 @@ class MavenBuildWriterTests {
|
|||||||
void pomWithBom() throws Exception {
|
void pomWithBom() throws Exception {
|
||||||
MavenBuild build = new MavenBuild();
|
MavenBuild build = new MavenBuild();
|
||||||
build.settings().coordinates("com.example.demo", "demo");
|
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) -> {
|
generatePom(build, (pom) -> {
|
||||||
NodeAssert dependency = pom.nodeAtPath("/project/dependencyManagement/dependencies/dependency");
|
NodeAssert dependency = pom.nodeAtPath("/project/dependencyManagement/dependencies/dependency");
|
||||||
assertBom(dependency, "com.example", "my-project-dependencies", "1.0.0.RELEASE");
|
assertBom(dependency, "com.example", "my-project-dependencies", "1.0.0.RELEASE");
|
||||||
@@ -299,9 +301,10 @@ class MavenBuildWriterTests {
|
|||||||
void pomWithOrderedBoms() throws Exception {
|
void pomWithOrderedBoms() throws Exception {
|
||||||
MavenBuild build = new MavenBuild();
|
MavenBuild build = new MavenBuild();
|
||||||
build.settings().coordinates("com.example.demo", "demo");
|
build.settings().coordinates("com.example.demo", "demo");
|
||||||
build.boms().add("bom1", "com.example", "my-project-dependencies", VersionReference.ofValue("1.0.0.RELEASE"),
|
build.boms().add("bom1", BillOfMaterials.withCoordinates("com.example", "my-project-dependencies")
|
||||||
5);
|
.version(VersionReference.ofValue("1.0.0.RELEASE")).order(5));
|
||||||
build.boms().add("bom2", "com.example", "root-dependencies", VersionReference.ofProperty("root.version"), 2);
|
build.boms().add("bom2", BillOfMaterials.withCoordinates("com.example", "root-dependencies")
|
||||||
|
.version(VersionReference.ofProperty("root.version")).order(2));
|
||||||
generatePom(build, (pom) -> {
|
generatePom(build, (pom) -> {
|
||||||
NodeAssert dependencies = pom.nodeAtPath("/project/dependencyManagement/dependencies");
|
NodeAssert dependencies = pom.nodeAtPath("/project/dependencyManagement/dependencies");
|
||||||
NodeAssert firstBom = assertThat(dependencies).nodeAtPath("dependency[1]");
|
NodeAssert firstBom = assertThat(dependencies).nodeAtPath("dependency[1]");
|
||||||
|
@@ -78,8 +78,8 @@ public final class MetadataBuildItemMapper {
|
|||||||
}
|
}
|
||||||
VersionReference version = (bom.getVersionProperty() != null)
|
VersionReference version = (bom.getVersionProperty() != null)
|
||||||
? VersionReference.ofProperty(bom.getVersionProperty()) : VersionReference.ofValue(bom.getVersion());
|
? VersionReference.ofProperty(bom.getVersionProperty()) : VersionReference.ofValue(bom.getVersion());
|
||||||
return new io.spring.initializr.generator.buildsystem.BillOfMaterials(bom.getGroupId(), bom.getArtifactId(),
|
return io.spring.initializr.generator.buildsystem.BillOfMaterials
|
||||||
version, bom.getOrder());
|
.withCoordinates(bom.getGroupId(), bom.getArtifactId()).version(version).order(bom.getOrder()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user