Use a fallback if no 'web' dependency is available

Closes gh-435
This commit is contained in:
Stephane Nicoll 2018-05-18 17:24:56 +02:00
parent 5b8c06dab1
commit 8336ee6ef6
3 changed files with 45 additions and 4 deletions

View File

@ -498,9 +498,11 @@ The same project can now be generated with `dependencies=ws` or
A "facet" is a label on a dependency which is used to drive a code modification in the A "facet" is a label on a dependency which is used to drive a code modification in the
generated project. In the standard Initializr generator, there is only one facet that is generated project. In the standard Initializr generator, there is only one facet that is
actually used (`web`), but custom installations might choose to use it for their own actually used (`web`), but custom installations might choose to use it for their own
purposes. The `web` facet is used to drive the inclusion of `spring-boot-starter-web` if purposes. The `web` facet is used to drive the inclusion of a dependency with id `web`
any other dependency with that facet is included. The value of the "facets" property of a (defaulting to `spring-boot-starter-web` if such dependency is not present) when any other
dependency is a list of strings. dependency with that facet is included.
The value of the "facets" property of a dependency is a list of strings.

View File

@ -272,7 +272,7 @@ public class ProjectRequest extends BasicProjectRequest {
if ("war".equals(getPackaging())) { if ("war".equals(getPackaging())) {
if (!hasWebFacet()) { if (!hasWebFacet()) {
// Need to be able to bootstrap the web app // Need to be able to bootstrap the web app
this.resolvedDependencies.add(metadata.getDependencies().get("web")); this.resolvedDependencies.add(determineWebDependency(metadata));
this.facets.add("web"); this.facets.add("web");
} }
// Add the tomcat starter in provided scope // Add the tomcat starter in provided scope
@ -286,6 +286,14 @@ public class ProjectRequest extends BasicProjectRequest {
} }
} }
private Dependency determineWebDependency(InitializrMetadata metadata) {
Dependency web = metadata.getDependencies().get("web");
if (web != null) {
return web;
}
return Dependency.withId("web").asSpringBootStarter("web");
}
/** /**
* Add a default dependency if the project does not define any dependency. * Add a default dependency if the project does not define any dependency.
*/ */

View File

@ -168,6 +168,37 @@ public class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
.hasSpringBootStarterTest().hasDependenciesCount(4); .hasSpringBootStarterTest().hasDependenciesCount(4);
} }
@Test
public void mavenWarPomWithoutWebFacetAndWithoutWebDependency() {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("core", "security", "data-jpa")
.build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setPackaging("war");
generateMavenPom(request).hasSpringBootStarterTomcat()
.hasSpringBootStarterDependency("data-jpa")
.hasSpringBootStarterDependency("web") // Added by war packaging
.hasSpringBootStarterTest().hasDependenciesCount(4);
}
@Test
public void mavenWarPomWithoutWebFacetAndWithCustomWebDependency() {
Dependency customWebStarter = Dependency.withId("web", "org.acme", "web-starter");
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("core", "security", "data-jpa")
.addDependencyGroup("acme", customWebStarter)
.build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setPackaging("war");
generateMavenPom(request).hasSpringBootStarterTomcat()
.hasSpringBootStarterDependency("data-jpa")
.hasDependency(customWebStarter) // Added by war packaging
.hasSpringBootStarterTest().hasDependenciesCount(4);
}
@Test @Test
public void gradleWarWithWebFacet() { public void gradleWarWithWebFacet() {
Dependency dependency = Dependency.withId("thymeleaf", "org.foo", "thymeleaf"); Dependency dependency = Dependency.withId("thymeleaf", "org.foo", "thymeleaf");