mirror of
https://gitee.com/dcren/initializr.git
synced 2026-02-25 21:22:58 +08:00
Model custom configuration for a Gradle dependency
Closes gh-850
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 : "")
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user