mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Add a way to easily create a Spring Boot starter from id
This commit is contained in:
parent
79170178ff
commit
53a0484289
@ -45,9 +45,8 @@ class DefaultStarterBuildCustomizer implements BuildCustomizer<Build> {
|
|||||||
public void customize(Build build) {
|
public void customize(Build build) {
|
||||||
boolean hasStarter = this.buildResolver.dependencies(build).anyMatch(this::isValidStarter);
|
boolean hasStarter = this.buildResolver.dependencies(build).anyMatch(this::isValidStarter);
|
||||||
if (!hasStarter) {
|
if (!hasStarter) {
|
||||||
Dependency root = new Dependency();
|
Dependency root = Dependency.createSpringBootStarter("");
|
||||||
root.setId(DEFAULT_STARTER);
|
root.setId(DEFAULT_STARTER);
|
||||||
root.asSpringBootStarter("");
|
|
||||||
build.dependencies().add(DEFAULT_STARTER, MetadataBuildItemMapper.toDependency(root));
|
build.dependencies().add(DEFAULT_STARTER, MetadataBuildItemMapper.toDependency(root));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ public class WarPackagingWebStarterBuildCustomizer implements BuildCustomizer<Bu
|
|||||||
build.dependencies().add(dependency.getId(), MetadataBuildItemMapper.toDependency(dependency));
|
build.dependencies().add(dependency.getId(), MetadataBuildItemMapper.toDependency(dependency));
|
||||||
}
|
}
|
||||||
// Add the tomcat starter in provided scope
|
// Add the tomcat starter in provided scope
|
||||||
Dependency tomcat = new Dependency().asSpringBootStarter("tomcat");
|
Dependency tomcat = Dependency.createSpringBootStarter("tomcat");
|
||||||
tomcat.setScope(Dependency.SCOPE_PROVIDED);
|
tomcat.setScope(Dependency.SCOPE_PROVIDED);
|
||||||
build.dependencies().add("tomcat", MetadataBuildItemMapper.toDependency(tomcat));
|
build.dependencies().add("tomcat", MetadataBuildItemMapper.toDependency(tomcat));
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ public class WarPackagingWebStarterBuildCustomizer implements BuildCustomizer<Bu
|
|||||||
|
|
||||||
private Dependency determineWebDependency(InitializrMetadata metadata) {
|
private Dependency determineWebDependency(InitializrMetadata metadata) {
|
||||||
Dependency web = metadata.getDependencies().get("web");
|
Dependency web = metadata.getDependencies().get("web");
|
||||||
return (web != null) ? web : Dependency.withId("web").asSpringBootStarter("web");
|
return (web != null) ? web : Dependency.createSpringBootStarter("web");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -165,21 +165,6 @@ public class Dependency extends MetadataElement implements Describable {
|
|||||||
return this.groupId != null && this.artifactId != null;
|
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.
|
* 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);
|
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) {
|
public void updateCompatibilityRange(VersionParser versionParser) {
|
||||||
if (this.compatibilityRange != null) {
|
if (this.compatibilityRange != null) {
|
||||||
try {
|
try {
|
||||||
@ -450,6 +444,19 @@ public class Dependency extends MetadataElement implements Describable {
|
|||||||
return dependency;
|
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) {
|
public static Dependency withId(String id, String groupId, String artifactId, String version, String scope) {
|
||||||
Dependency dependency = new Dependency();
|
Dependency dependency = new Dependency();
|
||||||
dependency.setId(id);
|
dependency.setId(id);
|
||||||
|
@ -35,8 +35,7 @@ class DependencyTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createRootSpringBootStarter() {
|
void createRootSpringBootStarter() {
|
||||||
Dependency d = new Dependency();
|
Dependency d = Dependency.createSpringBootStarter("");
|
||||||
d.asSpringBootStarter("");
|
|
||||||
assertThat(d.getGroupId()).isEqualTo("org.springframework.boot");
|
assertThat(d.getGroupId()).isEqualTo("org.springframework.boot");
|
||||||
assertThat(d.getArtifactId()).isEqualTo("spring-boot-starter");
|
assertThat(d.getArtifactId()).isEqualTo("spring-boot-starter");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user