diff --git a/initializr-service/application.yml b/initializr-service/application.yml index ed5b8bba..0058f67b 100644 --- a/initializr-service/application.yml +++ b/initializr-service/application.yml @@ -42,6 +42,8 @@ initializr: content: - name: Web id: web + facets: + - web - name: Websocket id: websocket - name: Rest Repositories @@ -52,12 +54,20 @@ initializr: content: - name: Freemarker id: freemarker + facets: + - web - name: Velocity id: velocity + facets: + - web - name: Groovy Templates id: groovy-templates + facets: + - web - name: Thymeleaf id: thymeleaf + facets: + - web - name: Social content: - name: Facebook diff --git a/initializr/src/main/groovy/io/spring/initializr/InitializrMetadata.groovy b/initializr/src/main/groovy/io/spring/initializr/InitializrMetadata.groovy index 02f494e7..8d812eb9 100644 --- a/initializr/src/main/groovy/io/spring/initializr/InitializrMetadata.groovy +++ b/initializr/src/main/groovy/io/spring/initializr/InitializrMetadata.groovy @@ -159,6 +159,9 @@ class InitializrMetadata { @JsonIgnore List aliases = [] + @JsonIgnore + List facets = [] + @JsonIgnore String groupId diff --git a/initializr/src/main/groovy/io/spring/initializr/ProjectGenerator.groovy b/initializr/src/main/groovy/io/spring/initializr/ProjectGenerator.groovy index 634d739f..994d0b3e 100644 --- a/initializr/src/main/groovy/io/spring/initializr/ProjectGenerator.groovy +++ b/initializr/src/main/groovy/io/spring/initializr/ProjectGenerator.groovy @@ -1,3 +1,19 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.spring.initializr import org.springframework.beans.factory.annotation.Autowired @@ -71,7 +87,7 @@ class ProjectGenerator { File test = new File(new File(dir, 'src/test/' + language), request.packageName.replace('.', '/')) test.mkdirs() - if (request.isWebStyle()) { + if (request.hasWebFacet()) { model.testAnnotations = '@WebAppConfiguration\n' model.testImports = 'import org.springframework.test.context.web.WebAppConfiguration;\n' } else { @@ -84,7 +100,7 @@ class ProjectGenerator { resources.mkdirs() new File(resources, 'application.properties').write('') - if (request.isWebStyle()) { + if (request.hasWebFacet()) { new File(dir, 'src/main/resources/templates').mkdirs() new File(dir, 'src/main/resources/static').mkdirs() } diff --git a/initializr/src/main/groovy/io/spring/initializr/ProjectRequest.groovy b/initializr/src/main/groovy/io/spring/initializr/ProjectRequest.groovy index 20da836d..2d71d77a 100644 --- a/initializr/src/main/groovy/io/spring/initializr/ProjectRequest.groovy +++ b/initializr/src/main/groovy/io/spring/initializr/ProjectRequest.groovy @@ -45,14 +45,12 @@ class ProjectRequest { String javaVersion def dependencies = [] + def facets = [] /** * Resolve this instance against the specified {@link InitializrMetadata} */ void resolve(InitializrMetadata metadata) { - if (packaging == 'war' && !isWebStyle()) { - style << 'web' - } if (style == null || style.size() == 0) { style = [] } @@ -71,14 +69,39 @@ class ProjectRequest { } dependency } + dependencies.each { + it.facets.each { + if (!facets.contains(it)) { + facets.add(it) + } + } + } + afterResolution(metadata) } - boolean isWebStyle() { - style.any { webStyle(it) } + /** + * Update this request once it has been resolved with the specified {@link InitializrMetadata}. + */ + protected afterResolution(InitializrMetadata metadata) { + if (packaging == 'war' && !hasWebFacet()) { + // Need to be able to bootstrap the web app + dependencies << metadata.getDependency('web') + facets << 'web' + } } - private boolean webStyle(String style) { - style.contains('web') || style.contains('thymeleaf') || style.contains('freemarker') || style.contains('velocity') || style.contains('groovy-template') + /** + * Specify if this request has the web facet enabled. + */ + boolean hasWebFacet() { + hasFacet('web') + } + + /** + * Specify if this request has the specified facet enabled + */ + boolean hasFacet(String facet) { + facets.contains(facet) } } diff --git a/initializr/src/test/groovy/io/spring/initializr/ProjectGeneratorTests.groovy b/initializr/src/test/groovy/io/spring/initializr/ProjectGeneratorTests.groovy index 1842c05f..f44926b4 100644 --- a/initializr/src/test/groovy/io/spring/initializr/ProjectGeneratorTests.groovy +++ b/initializr/src/test/groovy/io/spring/initializr/ProjectGeneratorTests.groovy @@ -50,6 +50,60 @@ class ProjectGeneratorTests { .hasSnapshotRepository().hasSpringBootStarterDependency('web') } + @Test + void mavenPomWithWebFacet() { + InitializrMetadata.Dependency dependency = new InitializrMetadata.Dependency() + dependency.id = 'thymeleaf' + dependency.groupId = 'org.foo' + dependency.artifactId = 'thymeleaf' + dependency.facets << 'web' + InitializrMetadata metadata = InitializrMetadataBuilder.withDefaults() + .addDependencyGroup('core', 'web', 'security', 'data-jpa') + .addDependencyGroup('test', dependency).get() + projectGenerator.metadata = metadata + + ProjectRequest request = createProjectRequest('thymeleaf') + generateMavenPom(request).hasStartClass('demo.Application') + .hasDependency('org.foo', 'thymeleaf') + .hasDependenciesCount(2) + + } + + @Test + void mavenWarPomWithWebFacet() { + InitializrMetadata.Dependency dependency = new InitializrMetadata.Dependency() + dependency.id = 'thymeleaf' + dependency.groupId = 'org.foo' + dependency.artifactId = 'thymeleaf' + dependency.facets << 'web' + InitializrMetadata metadata = InitializrMetadataBuilder.withDefaults() + .addDependencyGroup('core', 'web', 'security', 'data-jpa') + .addDependencyGroup('test', dependency).get() + projectGenerator.metadata = metadata + + ProjectRequest request = createProjectRequest('thymeleaf') + request.packaging = 'war' + generateMavenPom(request).hasStartClass('demo.Application') + .hasSpringBootStarterDependency('tomcat') + .hasDependency('org.foo', 'thymeleaf') // This is tagged as web facet so it brings the web one + .hasSpringBootStarterDependency('test') + .hasDependenciesCount(3) + + } + + @Test + void mavenWarPomWithoutWebFacet() { + ProjectRequest request = createProjectRequest('data-jpa') + request.packaging = 'war' + generateMavenPom(request).hasStartClass('demo.Application') + .hasSpringBootStarterDependency('tomcat') + .hasSpringBootStarterDependency('data-jpa') + .hasSpringBootStarterDependency('web') // Added by war packaging + .hasSpringBootStarterDependency('test') + .hasDependenciesCount(4) + + } + PomAssert generateMavenPom(ProjectRequest request) { String content = new String(projectGenerator.generateMavenPom(request)) return new PomAssert(content).validateProjectRequest(request) diff --git a/initializr/src/test/resources/application-test-default.yml b/initializr/src/test/resources/application-test-default.yml index c62c94f6..92af846c 100644 --- a/initializr/src/test/resources/application-test-default.yml +++ b/initializr/src/test/resources/application-test-default.yml @@ -9,6 +9,8 @@ initializr: content: - name: Web id: web + facets: + - web - name: Security id: security - name: Data JPA