From 10ca8f217ba2cdad67f62ccc39fbf6a387a54c7d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sat, 19 Dec 2020 08:03:17 +0100 Subject: [PATCH] Hacking See gh-1159 --- .../buildsystem/gradle/GradleBuildWriter.java | 14 ++++++++++++++ .../gradle/GroovyDslGradleBuildWriter.java | 2 +- .../gradle/KotlinDslGradleBuildWriter.java | 2 +- .../gradle/GroovyDslGradleBuildWriterTests.java | 9 +++++++++ .../gradle/KotlinDslGradleBuildWriterTests.java | 9 +++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java index 3cf9324a7c..259b2779e5 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java @@ -51,6 +51,12 @@ */ public abstract class GradleBuildWriter { + /** + * Dependency types that are mapped to a jar. + */ + private static final List JAR_DEPENDENCY_TYPES = Arrays.asList("test-jar", "maven-plugin", "ejb", + "ejb-client", "java-source", "javadoc"); + /** * Write a {@linkplain GradleBuild build.gradle} using the specified * {@linkplain IndentingWriter writer}. @@ -138,6 +144,14 @@ private void writeDependencies(IndentingWriter writer, GradleBuild build) { } } + protected String determineDependencyType(Dependency dependency) { + String type = dependency.getType(); + if (type != null && JAR_DEPENDENCY_TYPES.contains(type)) { + return null; + } + return type; + } + private Predicate hasScope(DependencyScope... validScopes) { return (scope) -> Arrays.asList(validScopes).contains(scope); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java index 780dbf4ba7..c7097af3cb 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java @@ -135,7 +135,7 @@ protected void writeDependency(IndentingWriter writer, Dependency dependency) { String quoteStyle = determineQuoteStyle(dependency.getVersion()); String version = determineVersion(dependency.getVersion()); String classifier = dependency.getClassifier(); - String type = dependency.getType(); + String type = determineDependencyType(dependency); boolean hasExclusions = !dependency.getExclusions().isEmpty(); writer.print(configurationForDependency(dependency)); writer.print((hasExclusions) ? "(" : " "); diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java index 3da0170b39..9ed4e8533d 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java @@ -155,7 +155,7 @@ protected void writeConfiguration(IndentingWriter writer, GradleConfiguration co protected void writeDependency(IndentingWriter writer, Dependency dependency) { String version = determineVersion(dependency.getVersion()); String classifier = dependency.getClassifier(); - String type = dependency.getType(); + String type = determineDependencyType(dependency); writer.print(configurationForDependency(dependency) + "(\"" + dependency.getGroupId() + ":" + dependency.getArtifactId() + ((version != null) ? ":" + version : "") + ((classifier != null) ? ":" + classifier : "") + ((type != null) ? "@" + type : "") + "\")"); diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java index 6a3ac266ca..29aeee448c 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java @@ -416,6 +416,15 @@ void gradleBuildWithNonNullArtifactTypeAndClassifierDependency() { "}"); } + @Test + void gradleBuildWithMavenSpecialJarTypeDependency() { + GradleBuild build = new GradleBuild(); + build.dependencies().add("root", Dependency.withCoordinates("com.example", "acme") + .scope(DependencyScope.TEST_COMPILE).classifier("tests").type("test-jar")); + List lines = generateBuild(build); + assertThat(lines).containsSequence("dependencies {", " testImplementation 'com.example:acme:tests'", "}"); + } + @Test void gradleBuildWithOrderedDependencies() { GradleBuild build = new GradleBuild(); diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java index ba7cc22c47..87d06ae1f6 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java @@ -396,6 +396,15 @@ void gradleBuildWithClassifierDependency() { assertThat(lines).containsSequence("dependencies {", " implementation(\"com.example:acme:test-jar\")", "}"); } + @Test + void gradleBuildWithSpecialJarTypeDependency() { + GradleBuild build = new GradleBuild(); + build.dependencies().add("root", Dependency.withCoordinates("com.example", "acme") + .scope(DependencyScope.TEST_COMPILE).classifier("tests").type("test-jar")); + List lines = generateBuild(build); + assertThat(lines).containsSequence("dependencies {", " testImplementation(\"com.example:acme:tests\")", "}"); + } + @Test void gradleBuildWithExclusions() { GradleBuild build = new GradleBuild();