mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-19 19:47:08 +08:00
Add a way to easily create a Spring Boot starter from id
This commit is contained in:
@@ -165,21 +165,6 @@ public class Dependency extends MetadataElement implements Describable {
|
||||
return this.groupId != null && this.artifactId != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define this dependency as a standard spring boot starter with the specified name.
|
||||
* If no name is specified, the root "spring-boot-starter" is assumed.
|
||||
* @param name the starter name or {@code null}
|
||||
* @return this instance
|
||||
*/
|
||||
public Dependency asSpringBootStarter(String name) {
|
||||
this.groupId = "org.springframework.boot";
|
||||
this.artifactId = (StringUtils.hasText(name) ? "spring-boot-starter-" + name : "spring-boot-starter");
|
||||
if (StringUtils.hasText(name)) {
|
||||
setId(name);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the dependency and complete its state based on the available information.
|
||||
*/
|
||||
@@ -213,6 +198,15 @@ public class Dependency extends MetadataElement implements Describable {
|
||||
updateCompatibilityRange(VersionParser.DEFAULT);
|
||||
}
|
||||
|
||||
private Dependency asSpringBootStarter(String name) {
|
||||
this.groupId = "org.springframework.boot";
|
||||
this.artifactId = (StringUtils.hasText(name) ? "spring-boot-starter-" + name : "spring-boot-starter");
|
||||
if (StringUtils.hasText(name)) {
|
||||
setId(name);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void updateCompatibilityRange(VersionParser versionParser) {
|
||||
if (this.compatibilityRange != null) {
|
||||
try {
|
||||
@@ -450,6 +444,19 @@ public class Dependency extends MetadataElement implements Describable {
|
||||
return dependency;
|
||||
}
|
||||
|
||||
public static Dependency createSpringBootStarter(String name) {
|
||||
return createSpringBootStarter(name, null);
|
||||
}
|
||||
|
||||
public static Dependency createSpringBootStarter(String name, String scope) {
|
||||
Dependency dependency = new Dependency();
|
||||
dependency.asSpringBootStarter(name);
|
||||
if (StringUtils.hasText(scope)) {
|
||||
dependency.setScope(scope);
|
||||
}
|
||||
return dependency;
|
||||
}
|
||||
|
||||
public static Dependency withId(String id, String groupId, String artifactId, String version, String scope) {
|
||||
Dependency dependency = new Dependency();
|
||||
dependency.setId(id);
|
||||
|
||||
@@ -35,8 +35,7 @@ class DependencyTests {
|
||||
|
||||
@Test
|
||||
void createRootSpringBootStarter() {
|
||||
Dependency d = new Dependency();
|
||||
d.asSpringBootStarter("");
|
||||
Dependency d = Dependency.createSpringBootStarter("");
|
||||
assertThat(d.getGroupId()).isEqualTo("org.springframework.boot");
|
||||
assertThat(d.getArtifactId()).isEqualTo("spring-boot-starter");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user