Add support for dependency facet

This commit allows any dependency to be tagged with a facet. A facet
is a simple name that can be used to further tune the project request
if necessary.

Prior to this commit, the list of dependencies that were related to the
web was hardcoded. This was used for special handling such as adding
a dependency automatically if necessary of creating additional
resources in the project.

This logic was moved to a standard 'web' facet that any dependency
can declare through configuration.

Fixes gh-30
This commit is contained in:
Stephane Nicoll 2014-08-19 13:28:27 +02:00
parent b5845ab3c4
commit ccbfde3b5e
6 changed files with 117 additions and 9 deletions

View File

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

View File

@ -159,6 +159,9 @@ class InitializrMetadata {
@JsonIgnore
List<String> aliases = []
@JsonIgnore
List<String> facets = []
@JsonIgnore
String groupId

View File

@ -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()
}

View File

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

View File

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

View File

@ -9,6 +9,8 @@ initializr:
content:
- name: Web
id: web
facets:
- web
- name: Security
id: security
- name: Data JPA