mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-15 23:13:30 +08:00
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:
parent
de802b8383
commit
e7ee22f73a
@ -35,17 +35,17 @@ import io.spring.initializr.generator.spring.build.BuildWriter;
|
||||
*/
|
||||
public class GradleBuildProjectContributor implements BuildWriter, ProjectContributor {
|
||||
|
||||
private final GradleBuildWriter buildWriter;
|
||||
|
||||
private final GradleBuild build;
|
||||
|
||||
private final IndentingWriterFactory indentingWriterFactory;
|
||||
|
||||
private final GradleBuildWriter buildWriter;
|
||||
|
||||
GradleBuildProjectContributor(GradleBuild build,
|
||||
GradleBuildProjectContributor(GradleBuildWriter buildWriter, GradleBuild build,
|
||||
IndentingWriterFactory indentingWriterFactory) {
|
||||
this.buildWriter = buildWriter;
|
||||
this.build = build;
|
||||
this.indentingWriterFactory = indentingWriterFactory;
|
||||
this.buildWriter = new GradleBuildWriter();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -20,8 +20,10 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.BuildItemResolver;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.Gradle3BuildWriter;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildWriter;
|
||||
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
|
||||
import io.spring.initializr.generator.condition.ConditionalOnLanguage;
|
||||
import io.spring.initializr.generator.condition.ConditionalOnPackaging;
|
||||
@ -104,8 +106,9 @@ public class GradleProjectGenerationConfiguration {
|
||||
|
||||
@Bean
|
||||
public GradleBuildProjectContributor gradleBuildProjectContributor(
|
||||
GradleBuild build) {
|
||||
return new GradleBuildProjectContributor(build, this.indentingWriterFactory);
|
||||
GradleBuildWriter buildWriter, GradleBuild build) {
|
||||
return new GradleBuildProjectContributor(buildWriter, build,
|
||||
this.indentingWriterFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,6 +118,11 @@ public class GradleProjectGenerationConfiguration {
|
||||
@ConditionalOnGradleVersion("3")
|
||||
static class Gradle3ProjectGenerationConfiguration {
|
||||
|
||||
@Bean
|
||||
public Gradle3BuildWriter gradleBuildWriter() {
|
||||
return new Gradle3BuildWriter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GradleWrapperContributor gradle3WrapperContributor() {
|
||||
return new GradleWrapperContributor("3");
|
||||
@ -146,6 +154,11 @@ public class GradleProjectGenerationConfiguration {
|
||||
@ConditionalOnGradleVersion("4")
|
||||
static class Gradle4ProjectGenerationConfiguration {
|
||||
|
||||
@Bean
|
||||
public GradleBuildWriter gradleBuildWriter() {
|
||||
return new GradleBuildWriter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GradleWrapperContributor gradle4WrapperContributor() {
|
||||
return new GradleWrapperContributor("4");
|
||||
|
@ -23,6 +23,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildWriter;
|
||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||
import io.spring.initializr.generator.io.SimpleIndentStrategy;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -42,7 +43,7 @@ class GradleBuildProjectContributorTests {
|
||||
void gradleBuildIsContributedInProjectStructure(@TempDir Path projectDir)
|
||||
throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
new GradleBuildProjectContributor(build,
|
||||
new GradleBuildProjectContributor(new GradleBuildWriter(), build,
|
||||
IndentingWriterFactory.withDefaultSettings()).contribute(projectDir);
|
||||
Path buildGradle = projectDir.resolve("build.gradle");
|
||||
assertThat(buildGradle).isRegularFile();
|
||||
@ -80,8 +81,8 @@ class GradleBuildProjectContributorTests {
|
||||
private List<String> generateBuild(GradleBuild build,
|
||||
IndentingWriterFactory indentingWriterFactory) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
new GradleBuildProjectContributor(build, indentingWriterFactory)
|
||||
.writeBuild(writer);
|
||||
new GradleBuildProjectContributor(new GradleBuildWriter(), build,
|
||||
indentingWriterFactory).writeBuild(writer);
|
||||
return Arrays.asList(writer.toString().split("\\r?\\n"));
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation 'org.codehaus.groovy:groovy'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
compile 'org.springframework.boot:spring-boot-starter'
|
||||
compile 'org.codehaus.groovy:groovy'
|
||||
testCompile 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
@ -22,6 +22,6 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
compile 'org.springframework.boot:spring-boot-starter'
|
||||
testCompile 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
@ -23,10 +23,10 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation 'org.jetbrains.kotlin:kotlin-reflect'
|
||||
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
compile 'org.springframework.boot:spring-boot-starter'
|
||||
compile 'org.jetbrains.kotlin:kotlin-reflect'
|
||||
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
|
||||
testCompile 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
|
@ -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 + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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 + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user