Add a way to easily create a Spring Boot starter from id

This commit is contained in:
Stephane Nicoll
2019-08-21 11:08:47 +02:00
parent 79170178ff
commit 53a0484289
4 changed files with 26 additions and 21 deletions

View File

@@ -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);

View File

@@ -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");
}