Restore support for Gradle 3

While new scopes are available as of Gradle 3.4, the Spring Boot plugin
does not manage them in the `1.5.x` line. This commit introduces a
dedicated GradleBuildWriter for Gradle 3 that uses the previous scopes.

Closes gh-845
This commit is contained in:
Stephane Nicoll
2019-02-21 18:22:52 +01:00
parent de802b8383
commit e7ee22f73a
9 changed files with 234 additions and 40 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2012-2019 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.generator.buildsystem.gradle;
import io.spring.initializr.generator.buildsystem.DependencyScope;
/**
* A {@link GradleBuildWriter} suitable for Gradle 3.
*
* @author Stephane Nicoll
*/
public class Gradle3BuildWriter extends GradleBuildWriter {
protected String configurationForScope(DependencyScope type) {
switch (type) {
case ANNOTATION_PROCESSOR:
return "compileOnly";
case COMPILE:
return "compile";
case COMPILE_ONLY:
return "compileOnly";
case PROVIDED_RUNTIME:
return "providedRuntime";
case RUNTIME:
return "runtime";
case TEST_COMPILE:
return "testCompile";
case TEST_RUNTIME:
return "testRuntime";
default:
throw new IllegalStateException(
"Unrecognized dependency type '" + type + "'");
}
}
}

View File

@@ -218,6 +218,28 @@ public class GradleBuildWriter {
+ ((type != null) ? "@" + type : "") + quoteStyle;
}
protected String configurationForScope(DependencyScope type) {
switch (type) {
case ANNOTATION_PROCESSOR:
return "annotationProcessor";
case COMPILE:
return "implementation";
case COMPILE_ONLY:
return "compileOnly";
case PROVIDED_RUNTIME:
return "providedRuntime";
case RUNTIME:
return "runtimeOnly";
case TEST_COMPILE:
return "testImplementation";
case TEST_RUNTIME:
return "testRuntimeOnly";
default:
throw new IllegalStateException(
"Unrecognized dependency type '" + type + "'");
}
}
private void writeBoms(IndentingWriter writer, GradleBuild build) {
if (build.boms().isEmpty()) {
return;
@@ -342,26 +364,4 @@ public class GradleBuildWriter {
.sorted(DependencyComparator.INSTANCE).collect(Collectors.toList());
}
private String configurationForScope(DependencyScope type) {
switch (type) {
case ANNOTATION_PROCESSOR:
return "annotationProcessor";
case COMPILE:
return "implementation";
case COMPILE_ONLY:
return "compileOnly";
case PROVIDED_RUNTIME:
return "providedRuntime";
case RUNTIME:
return "runtimeOnly";
case TEST_COMPILE:
return "testImplementation";
case TEST_RUNTIME:
return "testRuntimeOnly";
default:
throw new IllegalStateException(
"Unrecognized dependency type '" + type + "'");
}
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright 2012-2019 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.generator.buildsystem.gradle;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import io.spring.initializr.generator.buildsystem.DependencyScope;
import io.spring.initializr.generator.io.IndentingWriter;
import io.spring.initializr.generator.version.VersionReference;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link Gradle3BuildWriter}.
*
* @author Stephane Nicoll
*/
class Gradle3BuildWriterTests {
@Test
void gradleBuildWithAnnotationProcessorDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("annotation-processor", "org.springframework.boot",
"spring-boot-configuration-processor",
DependencyScope.ANNOTATION_PROCESSOR);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" compileOnly 'org.springframework.boot:spring-boot-configuration-processor'",
"}");
}
@Test
void gradleBuildWithCompileDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", "org.springframework.boot",
"spring-boot-starter", DependencyScope.COMPILE);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" compile 'org.springframework.boot:spring-boot-starter'", "}");
}
@Test
void gradleBuildWithRuntimeDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("driver", "com.example", "jdbc-driver",
VersionReference.ofValue("1.0.0"), DependencyScope.RUNTIME);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" runtime 'com.example:jdbc-driver:1.0.0'", "}");
}
@Test
void gradleBuildWithProvidedRuntimeDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("tomcat", "org.springframework.boot",
"spring-boot-starter-tomcat", DependencyScope.PROVIDED_RUNTIME);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'",
"}");
}
@Test
void gradleBuildWithTestCompileDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("test", "org.springframework.boot",
"spring-boot-starter-test", DependencyScope.TEST_COMPILE);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" testCompile 'org.springframework.boot:spring-boot-starter-test'",
"}");
}
@Test
void gradleBuildWithCompileOnlyDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("test", "org.springframework.boot",
"spring-boot-starter-foobar", DependencyScope.COMPILE_ONLY);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" compileOnly 'org.springframework.boot:spring-boot-starter-foobar'",
"}");
}
@Test
void gradleBuildWithTestRuntimeDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("embed-mongo", "de.flapdoodle.embed",
"de.flapdoodle.embed.mongo", DependencyScope.TEST_RUNTIME);
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" testRuntime 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'", "}");
}
@Test
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", "org.springframework.boot",
"spring-boot-starter", null, DependencyScope.COMPILE, "tar.gz");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" compile 'org.springframework.boot:spring-boot-starter@tar.gz'", "}");
}
private List<String> generateBuild(GradleBuild build) throws IOException {
Gradle3BuildWriter writer = new Gradle3BuildWriter();
StringWriter out = new StringWriter();
writer.writeTo(new IndentingWriter(out), build);
return Arrays.asList(out.toString().split("\\r?\\n"));
}
}