Model custom configuration for a Gradle dependency

Closes gh-850
This commit is contained in:
Stephane Nicoll
2019-05-31 10:04:21 +02:00
parent d32b2f6a20
commit 31b60a6955
8 changed files with 125 additions and 5 deletions

View File

@@ -16,6 +16,7 @@
package io.spring.initializr.generator.buildsystem.gradle;
import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.buildsystem.DependencyScope;
/**
@@ -25,7 +26,15 @@ import io.spring.initializr.generator.buildsystem.DependencyScope;
*/
public class Gradle3BuildWriter extends GroovyDslGradleBuildWriter {
protected String configurationForScope(DependencyScope type) {
@Override
protected String configurationForScope(Dependency dependency) {
if (dependency instanceof GradleDependency) {
String configuration = ((GradleDependency) dependency).getConfiguration();
if (configuration != null) {
return configuration;
}
}
DependencyScope type = dependency.getScope();
switch (type) {
case ANNOTATION_PROCESSOR:
return "compileOnly";

View File

@@ -157,7 +157,14 @@ public abstract class GradleBuildWriter {
protected abstract void writeDependency(IndentingWriter writer,
Dependency dependency);
protected String configurationForScope(DependencyScope type) {
protected String configurationForScope(Dependency dependency) {
if (dependency instanceof GradleDependency) {
String configuration = ((GradleDependency) dependency).getConfiguration();
if (configuration != null) {
return configuration;
}
}
DependencyScope type = dependency.getScope();
switch (type) {
case ANNOTATION_PROCESSOR:
return "annotationProcessor";

View File

@@ -0,0 +1,68 @@
/*
* 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
*
* https://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.Dependency;
/**
* A {@link Dependency} with specific settings for the Gradle build system.
*
* @author Stephane Nicoll
*/
public class GradleDependency extends Dependency {
private final String configuration;
public GradleDependency(Builder builder) {
super(builder);
this.configuration = builder.configuration;
}
public static Builder withCoordinates(String groupId, String artifactId) {
return new Builder(groupId, artifactId);
}
public String getConfiguration() {
return this.configuration;
}
/**
* Builder for a Gradle dependency.
*
* @see GradleDependency#withCoordinates(String, String)
*/
public static class Builder extends Dependency.Builder<Builder> {
private String configuration;
protected Builder(String groupId, String artifactId) {
super(groupId, artifactId);
}
public Builder configuration(String configuration) {
this.configuration = configuration;
return self();
}
@Override
public Dependency build() {
return new GradleDependency(this);
}
}
}

View File

@@ -130,7 +130,7 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
String version = determineVersion(dependency.getVersion());
String type = dependency.getType();
boolean hasExclusions = !dependency.getExclusions().isEmpty();
writer.print(configurationForScope(dependency.getScope()));
writer.print(configurationForScope(dependency));
writer.print((hasExclusions) ? "(" : " ");
writer.print(quoteStyle + dependency.getGroupId() + ":"
+ dependency.getArtifactId() + ((version != null) ? ":" + version : "")

View File

@@ -135,8 +135,8 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
protected void writeDependency(IndentingWriter writer, Dependency dependency) {
String version = determineVersion(dependency.getVersion());
String type = dependency.getType();
writer.print(configurationForScope(dependency.getScope()) + "(\""
+ dependency.getGroupId() + ":" + dependency.getArtifactId()
writer.print(configurationForScope(dependency) + "(\"" + dependency.getGroupId()
+ ":" + dependency.getArtifactId()
+ ((version != null) ? ":" + version : "")
+ ((type != null) ? "@" + type : "") + "\")");
if (!dependency.getExclusions().isEmpty()) {

View File

@@ -124,6 +124,18 @@ class Gradle3BuildWriterTests {
" compile 'org.springframework.boot:spring-boot-starter@tar.gz'", "}");
}
@Test
void gradleBuildWithCustomDependencyConfiguration() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("test", GradleDependency
.withCoordinates("org.springframework.boot", "spring-boot-starter-foobar")
.scope(DependencyScope.RUNTIME).configuration("myRuntime"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" myRuntime 'org.springframework.boot:spring-boot-starter-foobar'",
"}");
}
private List<String> generateBuild(GradleBuild build) throws IOException {
Gradle3BuildWriter writer = new Gradle3BuildWriter();
StringWriter out = new StringWriter();

View File

@@ -412,6 +412,18 @@ class GroovyDslGradleBuildWriterTests {
" }", "}");
}
@Test
void gradleBuildWithCustomDependencyConfiguration() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("test", GradleDependency
.withCoordinates("org.springframework.boot", "spring-boot-starter-foobar")
.scope(DependencyScope.RUNTIME).configuration("myRuntime"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" myRuntime 'org.springframework.boot:spring-boot-starter-foobar'",
"}");
}
@Test
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
GradleBuild build = new GradleBuild();

View File

@@ -416,6 +416,18 @@ class KotlinDslGradleBuildWriterTests {
" }", "}");
}
@Test
void gradleBuildWithCustomDependencyConfiguration() throws IOException {
GradleBuild build = new GradleBuild();
build.dependencies().add("test", GradleDependency
.withCoordinates("org.springframework.boot", "spring-boot-starter-foobar")
.scope(DependencyScope.RUNTIME).configuration("myRuntime"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" myRuntime(\"org.springframework.boot:spring-boot-starter-foobar\")",
"}");
}
@Test
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
GradleBuild build = new GradleBuild();