Fix provided scope with Gradle build

Make sure to register the necessary configuration entry for Gradle when
a provided dependency is added as the build will fail otherwise.

Interestingly enough, this was only taken care of if the packaging is war
as we add the tomcat starter explicitly. We rather now add any provided
dependencies to the build with no special handling there, adding the
tomcat server in the model if necessary.

Closes gh-164
This commit is contained in:
Stephane Nicoll
2015-12-10 11:18:50 +01:00
parent bda0135857
commit 6630e2fc99
4 changed files with 49 additions and 12 deletions

View File

@@ -169,10 +169,16 @@ class ProjectRequest {
* 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
resolvedDependencies << metadata.dependencies.get('web')
facets << 'web'
if (packaging == 'war') {
if (!hasWebFacet()) {
// Need to be able to bootstrap the web app
resolvedDependencies << metadata.dependencies.get('web')
facets << 'web'
}
// Add the tomcat starter in provided scope
def tomcat = new Dependency().asSpringBootStarter('tomcat')
tomcat.scope = Dependency.SCOPE_PROVIDED
resolvedDependencies << tomcat
}
if (!resolvedDependencies.find { it.starter }) {
// There's no starter so we add the default one

View File

@@ -34,15 +34,14 @@ repositories {
maven { url "${repo.url}" }<% }} %>
}
<% if (packaging=='war') { %>configurations {
<% if (providedDependencies) { %>configurations {
providedRuntime
}
<% } %>
dependencies {<% compileDependencies.each { %>
compile('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}')<% } %><% if (language=='groovy') { %>
compile('org.codehaus.groovy:groovy')<% } %><% runtimeDependencies.each { %>
runtime('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}')<% } %><% if (packaging=='war') { %>
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')<% } %><% providedDependencies.each { %>
runtime('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}')<% } %><% providedDependencies.each { %>
providedRuntime('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}')<% } %>
testCompile('org.springframework.boot:spring-boot-starter-test') <% testDependencies.each { %>
testCompile('${it.groupId}:${it.artifactId}${it.version ? ":$it.version" : ""}')<% } %>

View File

@@ -39,11 +39,6 @@
<artifactId>${it.artifactId}</artifactId><% if (it.version != null) { %>
<version>${it.version}</version><% } %>
<scope>runtime</scope>
</dependency><% } %><% if (packaging=='war') { %>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency><% } %><% providedDependencies.each { %>
<dependency>
<groupId>${it.groupId}</groupId>

View File

@@ -147,6 +147,41 @@ class ProjectGeneratorTests {
.hasDependenciesCount(4)
}
@Test
void gradleWarWithWebFacet() {
def dependency = new Dependency(id: 'thymeleaf', groupId: 'org.foo', artifactId: 'thymeleaf')
dependency.facets << 'web'
def metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup('core', 'web', 'security', 'data-jpa')
.addDependencyGroup('test', dependency).build()
projectGenerator.metadata = metadata
def request = createProjectRequest('thymeleaf')
request.packaging = 'war'
request.type = 'gradle-project'
generateProject(request).isJavaWarProject().isGradleProject().
gradleBuildAssert()
.contains("compile('org.foo:thymeleaf')") // This is tagged as web facet so it brings the web one
.doesNotContain("compile('org.springframework.boot:spring-boot-starter-web')")
.contains("testCompile('org.springframework.boot:spring-boot-starter-test')")
.contains("configurations {") // declare providedRuntime config
.contains("providedRuntime")
.contains("providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')")
}
@Test
void gradleWarPomWithoutWebFacet() {
def request = createProjectRequest('data-jpa')
request.packaging = 'war'
generateGradleBuild(request)
.contains("compile('org.springframework.boot:spring-boot-starter-data-jpa')")
.contains("compile('org.springframework.boot:spring-boot-starter-web')") // Added by war packaging
.contains("testCompile('org.springframework.boot:spring-boot-starter-test')")
.contains("configurations {") // declare providedRuntime config
.contains("providedRuntime")
.contains("providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')")
}
@Test
void springBoot11UseEnableAutoConfigurationJava() {
def request = createProjectRequest('web')
@@ -306,6 +341,8 @@ class ProjectGeneratorTests {
.contains("compile('org.springframework.boot:spring-boot-starter-web')")
.contains("compile('org.springframework.boot:spring-boot-starter-data-jpa')")
.contains("runtime('org.h2:h2')")
.contains("configurations {") // declare providedRuntime config
.contains("providedRuntime")
.contains("providedRuntime('javax.servlet:servlet-api')")
.contains("testCompile('org.hamcrest:hamcrest')")
}