From d1da13fbfc9f021370870e7c37674803778d37ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rniewski?= <13645939+mgorniew@users.noreply.github.com> Date: Fri, 15 May 2020 11:38:45 +0200 Subject: [PATCH] Add support for Gradle Kotlin DSL --- .../cli/commands/ListExtensionsCommand.java | 10 +- ...oModuleInMultiModuleKotlinProjectTest.java | 37 +++ .../gradle/QuarkusPluginFunctionalTest.java | 71 +++-- .../application/build.gradle.kts | 17 + .../acme/quarkus/sample/HelloResource.java | 22 ++ .../resources/META-INF/resources/index.html | 155 ++++++++++ .../src/main/resources/application.properties | 2 + .../build.gradle.kts | 42 +++ .../common/build.gradle.kts | 7 + .../main/java/org/acme/common/CommonBean.java | 11 + .../src/main/resources/META-INF/beans.xml | 9 + .../gradle.properties | 2 + .../settings.gradle.kts | 18 ++ ...> GroovyGradleBuildFileFromConnector.java} | 6 +- .../gradle/tasks/QuarkusListExtensions.java | 4 +- .../gradle/tasks/QuarkusPlatformTask.java | 19 +- ...st.java => GroovyGradleBuildFileTest.java} | 8 +- .../io/quarkus/maven/BuildFileMojoBase.java | 4 +- .../io/quarkus/maven/CreateProjectMojo.java | 14 +- .../java/build.gradle.kts-template.ftl | 34 ++ .../java/settings.gradle.kts-template.ftl | 12 + .../kotlin/build.gradle.kts-template.ftl | 51 +++ .../kotlin/settings.gradle.kts-template.ftl | 12 + .../scala/build.gradle.kts-template.ftl | 36 +++ .../scala/settings.gradle.kts-template.ftl | 12 + .../cli/commands/file/GradleBuildFile.java | 292 +++++++----------- .../commands/file/GroovyGradleBuildFile.java | 115 +++++++ .../commands/file/KotlinGradleBuildFile.java | 114 +++++++ .../java/io/quarkus/generators/BuildTool.java | 14 +- .../rest/BasicRestProjectGenerator.java | 16 +- .../cli/commands/AddGradleExtensionsTest.java | 4 +- .../commands/RemoveGradleExtensionsTest.java | 4 +- .../RemoveGradleKotlinDslExtensionsTest.java | 66 ++++ .../java/build.gradle.kts-template.ftl | 34 ++ .../java/settings.gradle.kts-template.ftl | 12 + .../kotlin/build.gradle.kts-template.ftl | 51 +++ .../kotlin/settings.gradle.kts-template.ftl | 12 + .../scala/build.gradle.kts-template.ftl | 36 +++ .../scala/settings.gradle.kts-template.ftl | 12 + 39 files changed, 1147 insertions(+), 250 deletions(-) create mode 100644 devtools/gradle/src/functionalTest/java/io/quarkus/gradle/AddExtensionToModuleInMultiModuleKotlinProjectTest.java create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/build.gradle.kts create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/java/org/acme/quarkus/sample/HelloResource.java create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/META-INF/resources/index.html create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/application.properties create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/build.gradle.kts create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/build.gradle.kts create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/java/org/acme/common/CommonBean.java create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/resources/META-INF/beans.xml create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/gradle.properties create mode 100644 devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/settings.gradle.kts rename devtools/gradle/src/main/java/io/quarkus/gradle/{GradleBuildFileFromConnector.java => GroovyGradleBuildFileFromConnector.java} (92%) rename devtools/gradle/src/test/java/io/quarkus/gradle/{GradleBuildFileTest.java => GroovyGradleBuildFileTest.java} (89%) create mode 100644 devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/build.gradle.kts-template.ftl create mode 100644 devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl create mode 100644 devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl create mode 100644 devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl create mode 100644 devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl create mode 100644 devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl create mode 100644 independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GroovyGradleBuildFile.java create mode 100644 independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/KotlinGradleBuildFile.java create mode 100644 independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleKotlinDslExtensionsTest.java create mode 100644 independent-projects/tools/common/src/test/resources/templates/basic-rest/java/build.gradle.kts-template.ftl create mode 100644 independent-projects/tools/common/src/test/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl create mode 100644 independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl create mode 100644 independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl create mode 100644 independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl create mode 100644 independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl diff --git a/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java b/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java index 7e0b1b2ebd3541..f0e7320910d661 100644 --- a/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java +++ b/devtools/aesh/src/main/java/io/quarkus/cli/commands/ListExtensionsCommand.java @@ -12,7 +12,8 @@ import org.aesh.io.Resource; import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; +import io.quarkus.cli.commands.file.KotlinGradleBuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.platform.tools.config.QuarkusPlatformConfig; @@ -47,9 +48,10 @@ public CommandResult execute(CommandInvocation commandInvocation) throws Command if (path != null) { File projectDirectory = new File(path.getAbsolutePath()); try (FileProjectWriter writer = new FileProjectWriter(projectDirectory)) { - if (new File(projectDirectory, "build.gradle").exists() - || new File(projectDirectory, "build.gradle.kts").exists()) { - buildFile = new GradleBuildFile(writer); + if (new File(projectDirectory, "build.gradle").exists()) { + buildFile = new GroovyGradleBuildFile(writer); + } else if (new File(projectDirectory, "build.gradle.kts").exists()) { + buildFile = new KotlinGradleBuildFile(writer); } else { buildFile = new MavenBuildFile(writer); } diff --git a/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/AddExtensionToModuleInMultiModuleKotlinProjectTest.java b/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/AddExtensionToModuleInMultiModuleKotlinProjectTest.java new file mode 100644 index 00000000000000..79679dfe99017c --- /dev/null +++ b/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/AddExtensionToModuleInMultiModuleKotlinProjectTest.java @@ -0,0 +1,37 @@ +package io.quarkus.gradle; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.junit.jupiter.api.Test; + +import io.quarkus.bootstrap.util.IoUtils; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class AddExtensionToModuleInMultiModuleKotlinProjectTest extends QuarkusGradleTestBase { + + @Test + public void testBasicMultiModuleBuild() throws Exception { + + final File projectDir = getProjectDir("add-extension-multi-module-kotlin-dsl"); + + BuildResult build = GradleRunner.create() + .forwardOutput() + .withPluginClasspath() + .withArguments(arguments(":application:addExtension", "--extensions=hibernate-orm")) + .withProjectDir(projectDir) + .build(); + + final Path applicationLib = projectDir.toPath().resolve("application").resolve("settings.gradle.kts"); + assertThat(applicationLib).doesNotExist(); + + final Path appBuild = projectDir.toPath().resolve("application").resolve("build.gradle.kts"); + assertThat(appBuild).exists(); + assertThat(IoUtils.readFile(appBuild)).contains("implementation(\"io.quarkus:quarkus-hibernate-orm\")"); + } +} diff --git a/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java b/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java index 62b06e729a44ca..f9bc60e707659b 100644 --- a/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java +++ b/devtools/gradle/src/functionalTest/java/io/quarkus/gradle/QuarkusPluginFunctionalTest.java @@ -1,31 +1,32 @@ package io.quarkus.gradle; -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; +import io.quarkus.cli.commands.CreateProject; +import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.generators.BuildTool; +import io.quarkus.generators.SourceType; import io.quarkus.platform.tools.config.QuarkusPlatformConfig; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.GradleRunner; import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.params.provider.EnumSource; -import io.quarkus.cli.commands.CreateProject; -import io.quarkus.cli.commands.writer.FileProjectWriter; -import io.quarkus.generators.BuildTool; -import io.quarkus.generators.SourceType; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; public class QuarkusPluginFunctionalTest extends QuarkusGradleTestBase { @@ -36,9 +37,10 @@ void setUp(@TempDir File projectRoot) { this.projectRoot = projectRoot; } - @Test - public void canRunListExtensions() throws IOException { - createProject(SourceType.JAVA); + @ParameterizedTest(name = "Create project with {0}") + @EnumSource(value = BuildTool.class, names = "GRADLE.*", mode = EnumSource.Mode.MATCH_ANY) + public void canRunListExtensions(BuildTool buildTool) throws IOException { + createProject(SourceType.JAVA, buildTool); BuildResult build = GradleRunner.create() .forwardOutput() @@ -51,9 +53,10 @@ public void canRunListExtensions() throws IOException { assertThat(build.getOutput()).contains("Quarkus - Core"); } - @Test - public void canGenerateConfig() throws IOException { - createProject(SourceType.JAVA); + @ParameterizedTest(name = "Create project with {0}") + @EnumSource(value = BuildTool.class, names = "GRADLE.*", mode = EnumSource.Mode.MATCH_ANY) + public void canGenerateConfig(BuildTool buildTool) throws IOException { + createProject(SourceType.JAVA, buildTool); BuildResult build = GradleRunner.create() .forwardOutput() @@ -67,10 +70,10 @@ public void canGenerateConfig() throws IOException { } - @ParameterizedTest(name = "Build {0} project") - @EnumSource(SourceType.class) - public void canBuild(SourceType sourceType) throws IOException, InterruptedException { - createProject(sourceType); + @ParameterizedTest(name = "Build {0} project created with {1}") + @ArgumentsSource(SourceTypeBuildToolProvider.class) + public void canBuild(SourceType sourceType, BuildTool buildTool) throws IOException, InterruptedException { + createProject(sourceType, buildTool); BuildResult build = GradleRunner.create() .forwardOutput() @@ -87,7 +90,7 @@ public void canBuild(SourceType sourceType) throws IOException, InterruptedExcep assertThat(buildDir.resolve("foo-1.0.0-SNAPSHOT-runner")).doesNotExist(); } - private void createProject(SourceType sourceType) throws IOException { + private void createProject(SourceType sourceType, BuildTool buildTool) throws IOException { Map context = new HashMap<>(); context.put("path", "/greeting"); assertThat(new CreateProject(new FileProjectWriter(projectRoot), @@ -95,11 +98,21 @@ private void createProject(SourceType sourceType) throws IOException { .groupId("com.acme.foo") .artifactId("foo") .version("1.0.0-SNAPSHOT") - .buildTool(BuildTool.GRADLE) + .buildTool(buildTool) .className("org.acme.GreetingResource") .sourceType(sourceType) .doCreateProject(context)) .withFailMessage("Project was not created") .isTrue(); } + + public static class SourceTypeBuildToolProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) { + List sourceTypes = Arrays.asList(SourceType.values()); + return Stream.of(BuildTool.GRADLE, BuildTool.GRADLE_KOTLIN_DSL) + .flatMap(buildTool -> sourceTypes.stream().map(sourceType -> Arguments.of(sourceType, buildTool))); + } + } } \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/build.gradle.kts b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/build.gradle.kts new file mode 100644 index 00000000000000..3b8089e95f69d5 --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/build.gradle.kts @@ -0,0 +1,17 @@ +plugins { + id("io.quarkus") +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(project(":common")) + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") + + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) +} \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/java/org/acme/quarkus/sample/HelloResource.java b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/java/org/acme/quarkus/sample/HelloResource.java new file mode 100644 index 00000000000000..092d251dc1ebbc --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/java/org/acme/quarkus/sample/HelloResource.java @@ -0,0 +1,22 @@ +package org.acme.quarkus.sample; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.acme.common.CommonBean; + +@Path("/hello") +public class HelloResource { + + @Inject + CommonBean common; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + return "hello " + common.getName(); + } +} \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/META-INF/resources/index.html b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/META-INF/resources/index.html new file mode 100644 index 00000000000000..77e09dffb5088d --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/META-INF/resources/index.html @@ -0,0 +1,155 @@ + + + + + my-quarkus-project - 1.0-SNAPSHOT + + + + + + +
+
+

Congratulations, you have created a new Quarkus application.

+ +

Why do you see this?

+ +

This page is served by Quarkus. The source is in + src/main/resources/META-INF/resources/index.html.

+ +

What can I do from here?

+ +

If not already done, run the application in dev mode using: mvn compile quarkus:dev. +

+
    +
  • Add REST resources, Servlets, functions and other services in src/main/java.
  • +
  • Your static assets are located in src/main/resources/META-INF/resources.
  • +
  • Configure your application in src/main/resources/application.properties. +
  • +
+ +

Do you like Quarkus?

+

Go give it a star on GitHub.

+ +

How do I get rid of this page?

+

Just delete the src/main/resources/META-INF/resources/index.html file.

+
+
+
+

Application

+
    +
  • GroupId: org.acme.quarkus.sample
  • +
  • ArtifactId: my-quarkus-project
  • +
  • Version: 1.0-SNAPSHOT
  • +
  • Quarkus Version: 999-SNAPSHOT
  • +
+
+ +
+
+ + + + \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/application.properties b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/application.properties new file mode 100644 index 00000000000000..3c1ac56a1ad0a8 --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/application/src/main/resources/application.properties @@ -0,0 +1,2 @@ +# Configuration file +# key = value \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/build.gradle.kts b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/build.gradle.kts new file mode 100644 index 00000000000000..ca16b378a91c8d --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/build.gradle.kts @@ -0,0 +1,42 @@ +buildscript { + + repositories { + jcenter() + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + +} + +plugins { + java +} + +group = "com.quarkus.demo" +version = "1.0" + + +subprojects { + + apply(plugin = "java") + + java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + tasks { + test { + dependsOn("cleanTest") + useJUnitPlatform() + setForkEvery(1) + } + } + + repositories { + jcenter() + mavenLocal() + mavenCentral() + } +} diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/build.gradle.kts b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/build.gradle.kts new file mode 100644 index 00000000000000..bcf58d676e495e --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + `java-library` +} + +dependencies { + +} \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/java/org/acme/common/CommonBean.java b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/java/org/acme/common/CommonBean.java new file mode 100644 index 00000000000000..ace88a5b0e4158 --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/java/org/acme/common/CommonBean.java @@ -0,0 +1,11 @@ +package org.acme.common; + +import javax.enterprise.context.RequestScoped; + +@RequestScoped +public class CommonBean { + + public String getName() { + return "common"; + } +} diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/resources/META-INF/beans.xml b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000000000..ddcbaf4800eb96 --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/common/src/main/resources/META-INF/beans.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/gradle.properties b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/gradle.properties new file mode 100644 index 00000000000000..ec2b6ef199c2ca --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/gradle.properties @@ -0,0 +1,2 @@ +quarkusPlatformArtifactId=quarkus-bom +quarkusPlatformGroupId=io.quarkus diff --git a/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/settings.gradle.kts b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/settings.gradle.kts new file mode 100644 index 00000000000000..e91806f676c2c5 --- /dev/null +++ b/devtools/gradle/src/functionalTest/resources/add-extension-multi-module-kotlin-dsl/settings.gradle.kts @@ -0,0 +1,18 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + jcenter() + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} + +rootProject.name = "quarkus-basic-multi-module-build" + +include(":common") +include(":application") + diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/GradleBuildFileFromConnector.java b/devtools/gradle/src/main/java/io/quarkus/gradle/GroovyGradleBuildFileFromConnector.java similarity index 92% rename from devtools/gradle/src/main/java/io/quarkus/gradle/GradleBuildFileFromConnector.java rename to devtools/gradle/src/main/java/io/quarkus/gradle/GroovyGradleBuildFileFromConnector.java index ad8a05bfd42e40..f47ae75079480c 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/GradleBuildFileFromConnector.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/GroovyGradleBuildFileFromConnector.java @@ -13,14 +13,14 @@ import org.gradle.tooling.model.eclipse.EclipseExternalDependency; import org.gradle.tooling.model.eclipse.EclipseProject; -import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; -public class GradleBuildFileFromConnector extends GradleBuildFile { +public class GroovyGradleBuildFileFromConnector extends GroovyGradleBuildFile { private List dependencies = null; - public GradleBuildFileFromConnector(ProjectWriter writer) { + public GroovyGradleBuildFileFromConnector(ProjectWriter writer) { super(writer); // we need to initialize here since there is no other single point of entry } diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java index 1644d9f8b5969c..88d81032fbafae 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusListExtensions.java @@ -8,7 +8,7 @@ import io.quarkus.cli.commands.ListExtensions; import io.quarkus.cli.commands.writer.FileProjectWriter; -import io.quarkus.gradle.GradleBuildFileFromConnector; +import io.quarkus.gradle.GroovyGradleBuildFileFromConnector; public class QuarkusListExtensions extends QuarkusPlatformTask { @@ -57,7 +57,7 @@ public QuarkusListExtensions() { @TaskAction public void listExtensions() { try { - new ListExtensions(new GradleBuildFileFromConnector(new FileProjectWriter(getProject().getProjectDir())), + new ListExtensions(new GroovyGradleBuildFileFromConnector(new FileProjectWriter(getProject().getProjectDir())), platformDescriptor()) .all(isAll()) .format(getFormat()) diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java index 82087c556b4fcf..8309863873ca9b 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusPlatformTask.java @@ -11,6 +11,8 @@ import org.gradle.api.tasks.Internal; import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; +import io.quarkus.cli.commands.file.KotlinGradleBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor; import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; @@ -65,9 +67,18 @@ protected QuarkusPlatformDescriptor platformDescriptor() { @Internal protected GradleBuildFile getGradleBuildFile() { - return getProject().getParent() == null - ? new GradleBuildFile(new FileProjectWriter(getProject().getProjectDir())) - : new GradleBuildFile(new FileProjectWriter(getProject().getProjectDir()), - new FileProjectWriter(getProject().getRootProject().getProjectDir())); + FileProjectWriter writer = new FileProjectWriter(getProject().getProjectDir()); + FileProjectWriter rootWriter = getProject().getParent() == null + ? writer + : new FileProjectWriter(getProject().getRootProject().getProjectDir()); + if (rootWriter.exists("settings.gradle.kts") + && writer.exists("build.gradle.kts")) { + return new KotlinGradleBuildFile(writer, rootWriter); + } else if (rootWriter.exists("settings.gradle") + && writer.exists("build.gradle")) { + return new GroovyGradleBuildFile(writer, rootWriter); + } + throw new GradleException( + "Mixed DSL is not supported. Both build and settings file need to use either Kotlin or Groovy DSL"); } } diff --git a/devtools/gradle/src/test/java/io/quarkus/gradle/GradleBuildFileTest.java b/devtools/gradle/src/test/java/io/quarkus/gradle/GroovyGradleBuildFileTest.java similarity index 89% rename from devtools/gradle/src/test/java/io/quarkus/gradle/GradleBuildFileTest.java rename to devtools/gradle/src/test/java/io/quarkus/gradle/GroovyGradleBuildFileTest.java index c2c7f63177d3f8..eefbea10106c70 100644 --- a/devtools/gradle/src/test/java/io/quarkus/gradle/GradleBuildFileTest.java +++ b/devtools/gradle/src/test/java/io/quarkus/gradle/GroovyGradleBuildFileTest.java @@ -22,16 +22,16 @@ import io.quarkus.cli.commands.writer.FileProjectWriter; -class GradleBuildFileTest { +class GroovyGradleBuildFileTest { - private static GradleBuildFileFromConnector buildFile; + private static GroovyGradleBuildFileFromConnector buildFile; @BeforeAll public static void beforeAll() throws URISyntaxException { - URL url = GradleBuildFileTest.class.getClassLoader().getResource("gradle-project"); + URL url = GroovyGradleBuildFileTest.class.getClassLoader().getResource("gradle-project"); URI uri = url.toURI(); Path gradleProjectPath = Paths.get(uri); - buildFile = new GradleBuildFileFromConnector(new FileProjectWriter(gradleProjectPath.toFile())); + buildFile = new GroovyGradleBuildFileFromConnector(new FileProjectWriter(gradleProjectPath.toFile())); } @Test diff --git a/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java b/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java index 940c4e75bc1329..7c5ff2d8c825e5 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/BuildFileMojoBase.java @@ -20,7 +20,7 @@ import io.quarkus.bootstrap.resolver.BootstrapAppModelResolver; import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver; import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor; @@ -111,7 +111,7 @@ public void execute() throws MojoExecutionException { } else if (new File(project.getBasedir(), "build.gradle").exists() || new File(project.getBasedir(), "build.gradle.kts").exists()) { // Gradle project - buildFile = new GradleBuildFile(fileProjectWriter); + buildFile = new GroovyGradleBuildFile(fileProjectWriter); } if (platformDescr == null) { diff --git a/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java index bd26c6867a537b..157e690e5b8625 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/CreateProjectMojo.java @@ -220,10 +220,16 @@ public void execute() throws MojoExecutionException { success = new AddExtensions(projectWriter, buildToolEnum, platform).extensions(extensions).execute() .isSuccess(); } - if (BuildTool.MAVEN.equals(buildToolEnum)) { - createMavenWrapper(createdDependenciesBuildFile, ToolsUtils.readQuarkusProperties(platform)); - } else if (BuildTool.GRADLE.equals(buildToolEnum)) { - createGradleWrapper(platform, projectWriter); + switch (buildToolEnum) { + case MAVEN: + createMavenWrapper(createdDependenciesBuildFile, ToolsUtils.readQuarkusProperties(platform)); + break; + case GRADLE: + case GRADLE_KOTLIN_DSL: + createGradleWrapper(platform, projectWriter); + break; + default: + throw new IllegalStateException("Unsupported build tool: " + buildToolEnum.name()); } } catch (Exception e) { throw new MojoExecutionException("Failed to generate Quarkus project", e); diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/build.gradle.kts-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/build.gradle.kts-template.ftl new file mode 100644 index 00000000000000..e3090dde05e72d --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/build.gradle.kts-template.ftl @@ -0,0 +1,34 @@ +plugins { + java + id("io.quarkus") +} + +repositories { + mavenLocal() + mavenCentral() +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") +} + +group = "${project_groupId}" +version = "${project_version}" + +tasks.withType { + options.encoding = "UTF-8" + options.compilerArgs.add("-parameters") +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl new file mode 100644 index 00000000000000..b138b98ad82f79 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl @@ -0,0 +1,12 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} +rootProject.name="${project_artifactId}" diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl new file mode 100644 index 00000000000000..ec34629eb43233 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl @@ -0,0 +1,51 @@ +plugins { + java + id("io.quarkus") + kotlin("jvm") version "${kotlin_version}" + kotlin("plugin.allopen") version "${kotlin_version}" +} + +repositories { + mavenLocal() + mavenCentral() +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) + implementation(kotlin("stdlib-jdk8")) + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") +} + +group = "${project_groupId}" +version = "${project_version}" + +quarkus { + setOutputDirectory("$projectDir/build/classes/kotlin/main") +} + +tasks.withType { + setSourceDir("$projectDir/src/main/kotlin") +} + +allOpen { + annotation("javax.ws.rs.Path") + annotation("javax.enterprise.context.ApplicationScoped") + annotation("io.quarkus.test.junit.QuarkusTest") +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +tasks.withType { + kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() + kotlinOptions.javaParameters = true +} diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl new file mode 100644 index 00000000000000..b138b98ad82f79 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl @@ -0,0 +1,12 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} +rootProject.name="${project_artifactId}" diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl new file mode 100644 index 00000000000000..1d2d29667a150a --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl @@ -0,0 +1,36 @@ +plugins { + scala + id("io.quarkus") +} + +repositories { + mavenLocal() + mavenCentral() +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) + implementation("org.scala-lang:scala-library:${scala_version}") + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") +} + +group = "${project_groupId}" +version = "${project_version}" + +tasks.withType { + scalaCompileOptions.encoding = "UTF-8" + sourceCompatibility = JavaVersion.VERSION_1_8.toString() + targetCompatibility = JavaVersion.VERSION_1_8.toString() +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl new file mode 100644 index 00000000000000..b138b98ad82f79 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl @@ -0,0 +1,12 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} +rootProject.name="${project_artifactId}" diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GradleBuildFile.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GradleBuildFile.java index 308863a642fe40..e70e0d9b962772 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GradleBuildFile.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GradleBuildFile.java @@ -17,50 +17,50 @@ import java.util.function.Consumer; import org.apache.maven.model.Dependency; -public class GradleBuildFile extends BuildFile { +public abstract class GradleBuildFile extends BuildFile { - private static final String BUILD_GRADLE_PATH = "build.gradle"; - private static final String SETTINGS_GRADLE_PATH = "settings.gradle"; private static final String GRADLE_PROPERTIES_PATH = "gradle.properties"; private final ProjectWriter rootWriter; private Model model; - public GradleBuildFile(ProjectWriter writer) { - super(writer, BuildTool.GRADLE); - rootWriter = writer; - } - - public GradleBuildFile(ProjectWriter writer, ProjectWriter rootWriter) { - super(writer, BuildTool.GRADLE); + GradleBuildFile(ProjectWriter writer, ProjectWriter rootWriter, BuildTool buildTool) { + super(writer, buildTool); this.rootWriter = rootWriter; } + protected abstract String getBuildGradlePath(); + + protected abstract String getSettingGradlePath(); + + protected abstract void completeBuildContent(String groupId, String version, QuarkusPlatformDescriptor platform, + Properties props) + throws IOException; + + protected abstract void completeSettingsContent(String artifactId) throws IOException; + protected void rootWrite(String fileName, String content) throws IOException { rootWriter.write(fileName, content); } + protected String getGradlePropertiesPath() { + return GRADLE_PROPERTIES_PATH; + } + protected ProjectWriter getRootWriter() { return rootWriter; } - @Override - public void close() throws IOException { - if (getWriter() == getRootWriter()) { - write(SETTINGS_GRADLE_PATH, getModel().getSettingsContent()); - } else { - rootWrite(SETTINGS_GRADLE_PATH, getModel().getSettingsContent()); - } - write(BUILD_GRADLE_PATH, getModel().getBuildContent()); - try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { - getModel().getPropertiesContent().store(out, "Gradle properties"); - if (getModel().isRootProperties()) { - rootWrite(GRADLE_PROPERTIES_PATH, out.toString(StandardCharsets.UTF_8.toString())); - } else { - write(GRADLE_PROPERTIES_PATH, out.toString(StandardCharsets.UTF_8.toString())); - } + protected Model getModel() throws IOException { + if (model == null) { + initModel(); } + return model; + } + + protected String getBuildContent() throws IOException { + return getModel().getBuildContent(); } @Override @@ -71,73 +71,7 @@ public void completeFile(String groupId, String artifactId, String version, Quar completeProperties(platform); } - private void completeBuildContent(String groupId, String version, QuarkusPlatformDescriptor platform, Properties props) - throws IOException { - final String buildContent = getModel().getBuildContent(); - StringBuilder res = new StringBuilder(buildContent); - if (!buildContent.contains("id 'io.quarkus'")) { - res.append("plugins {"); - res.append(System.lineSeparator()).append(" id 'java'").append(System.lineSeparator()); - res.append(System.lineSeparator()).append(" id 'io.quarkus'").append(System.lineSeparator()); - res.append("}"); - } - if (!containsBOM(platform.getBomGroupId(), platform.getBomArtifactId())) { - res.append(System.lineSeparator()); - res.append("dependencies {").append(System.lineSeparator()); - res.append( - " implementation enforcedPlatform(\"${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}\")") - .append(System.lineSeparator()); - res.append(" implementation 'io.quarkus:quarkus-resteasy'").append(System.lineSeparator()); - res.append(" testImplementation 'io.quarkus:quarkus-junit5'").append(System.lineSeparator()); - res.append(" testImplementation 'io.rest-assured:rest-assured'").append(System.lineSeparator()); - res.append("}").append(System.lineSeparator()); - - } - String groupLine = "group '" + groupId + "'"; - if (!buildContent.contains(groupLine)) { - res.append(System.lineSeparator()).append(groupLine) - .append(System.lineSeparator()); - } - String versionLine = "version '" + version + "'"; - if (!buildContent.contains(versionLine)) { - res.append(System.lineSeparator()).append(versionLine) - .append(System.lineSeparator()); - } - - res.append(System.lineSeparator()) - .append("test {").append(System.lineSeparator()) - .append(" systemProperty \"java.util.logging.manager\", \"org.jboss.logmanager.LogManager\"") - .append(System.lineSeparator()) - .append("}"); - - getModel().setBuildContent(res.toString()); - } - - private void completeSettingsContent(String artifactId) throws IOException { - final String settingsContent = getModel().getSettingsContent(); - final StringBuilder res = new StringBuilder(); - if (!settingsContent.contains("id 'io.quarkus'")) { - res.append(System.lineSeparator()); - res.append("pluginManagement {").append(System.lineSeparator()); - res.append(" repositories {").append(System.lineSeparator()); - res.append(" mavenLocal()").append(System.lineSeparator()); - res.append(" mavenCentral()").append(System.lineSeparator()); - res.append(" gradlePluginPortal()").append(System.lineSeparator()); - res.append(" }").append(System.lineSeparator()); - res.append(" plugins {").append(System.lineSeparator()); - res.append(" id 'io.quarkus' version \"${quarkusPluginVersion}\"").append(System.lineSeparator()); - res.append(" }").append(System.lineSeparator()); - res.append("}").append(System.lineSeparator()); - } - if (!settingsContent.contains("rootProject.name")) { - res.append(System.lineSeparator()).append("rootProject.name='").append(artifactId).append("'") - .append(System.lineSeparator()); - } - res.append(settingsContent); - getModel().setSettingsContent(res.toString()); - } - - private void completeProperties(QuarkusPlatformDescriptor platform) throws IOException { + protected void completeProperties(QuarkusPlatformDescriptor platform) throws IOException { Properties props = getModel().getPropertiesContent(); if (props.getProperty("quarkusPluginVersion") == null) { props.setProperty("quarkusPluginVersion", ToolsUtils.getPluginVersion(ToolsUtils.readQuarkusProperties(platform))); @@ -153,11 +87,31 @@ private void completeProperties(QuarkusPlatformDescriptor platform) throws IOExc } } + protected void readLineByLine(String content, Consumer lineConsumer) { + try (Scanner scanner = new Scanner(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)), + StandardCharsets.UTF_8.name())) { + while (scanner.hasNextLine()) { + String currentLine = scanner.nextLine(); + lineConsumer.accept(currentLine); + } + } + } + @Override - protected void addDependencyInBuildFile(Dependency dependency) throws IOException { - StringBuilder newBuildContent = new StringBuilder(); - readLineByLine(getModel().getBuildContent(), new AppendDependency(newBuildContent, dependency)); - getModel().setBuildContent(newBuildContent.toString()); + public String getProperty(String propertyName) throws IOException { + return getModel().getPropertiesContent().getProperty(propertyName); + } + + @Override + public List getDependencies() throws IOException { + return Collections.emptyList(); + } + + @Override + protected boolean hasDependency(Extension extension) throws IOException { + return getDependencies().stream() + .anyMatch(d -> extension.getGroupId().equals(d.getGroupId()) + && extension.getArtifactId().equals(d.getArtifactId())); } @Override @@ -177,10 +131,15 @@ && isDefinedInBom(platform.getManagedDependencies(), extension)) { return true; } + @Override + protected List getManagedDependencies() { + // Gradle tooling API only provide resolved dependencies. + return Collections.emptyList(); + } + @Override protected void removeDependencyFromBuildFile(Dependency dependency) throws IOException { - String depString = new StringBuilder("'").append(dependency.getGroupId()).append(":") - .append(dependency.getArtifactId()).toString(); + String depString = lineToRemoveDependency(dependency); StringBuilder newBuildContent = new StringBuilder(); Scanner scanner = new Scanner(getModel().getBuildContent()); while (scanner.hasNextLine()) { @@ -193,81 +152,9 @@ protected void removeDependencyFromBuildFile(Dependency dependency) throws IOExc getModel().setBuildContent(newBuildContent.toString()); } - private void readLineByLine(String content, Consumer lineConsumer) { - try (Scanner scanner = new Scanner(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)), - StandardCharsets.UTF_8.name())) { - while (scanner.hasNextLine()) { - String currentLine = scanner.nextLine(); - lineConsumer.accept(currentLine); - } - } - } - - private static class AppendDependency implements Consumer { - - private StringBuilder newContent; - private Dependency dependency; - - public AppendDependency(StringBuilder newContent, Dependency dependency) { - this.newContent = newContent; - this.dependency = dependency; - } - - @Override - public void accept(String currentLine) { - newContent.append(currentLine).append(System.lineSeparator()); - if (currentLine.startsWith("dependencies {")) { - newContent.append(" implementation '") - .append(dependency.getGroupId()) - .append(":") - .append(dependency.getArtifactId()); - if (dependency.getVersion() != null && !dependency.getVersion().isEmpty()) { - newContent.append(":") - .append(dependency.getVersion()); - } - newContent.append("'") - .append(System.lineSeparator()); - } - } - - } - - @Override - protected boolean hasDependency(Extension extension) throws IOException { - return getDependencies().stream() - .anyMatch(d -> extension.getGroupId().equals(d.getGroupId()) - && extension.getArtifactId().equals(d.getArtifactId())); - } - - @Override - protected boolean containsBOM(String groupId, String artifactId) throws IOException { - String buildContent = getModel().getBuildContent(); - return buildContent.contains("enforcedPlatform(\"${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:") - || buildContent.contains("enforcedPlatform(\"" + groupId + ":" + artifactId + ":"); - } + protected abstract String lineToRemoveDependency(Dependency dependency); - @Override - public List getDependencies() throws IOException { - return Collections.emptyList(); - } - - @Override - public String getProperty(String propertyName) throws IOException { - return getModel().getPropertiesContent().getProperty(propertyName); - } - - @Override - protected List getManagedDependencies() { - // Gradle tooling API only provide resolved dependencies. - return Collections.emptyList(); - } - - private Model getModel() throws IOException { - if (model == null) { - initModel(); - } - return model; - } + protected abstract String buildDependency(String configuration, Dependency dependency); protected void initModel() throws IOException { String settingsContent = ""; @@ -275,30 +162,30 @@ protected void initModel() throws IOException { Properties propertiesContent = new Properties(); boolean isRootProperties = false; if (getWriter() == getRootWriter()) { - if (getWriter().exists(SETTINGS_GRADLE_PATH)) { - final byte[] settings = getWriter().getContent(SETTINGS_GRADLE_PATH); + if (getWriter().exists(getSettingGradlePath())) { + final byte[] settings = getWriter().getContent(getSettingGradlePath()); settingsContent = new String(settings, StandardCharsets.UTF_8); } } else { - if (getRootWriter().exists(SETTINGS_GRADLE_PATH)) { - final byte[] settings = getRootWriter().getContent(SETTINGS_GRADLE_PATH); + if (getRootWriter().exists(getSettingGradlePath())) { + final byte[] settings = getRootWriter().getContent(getSettingGradlePath()); settingsContent = new String(settings, StandardCharsets.UTF_8); } } - if (getWriter().exists(BUILD_GRADLE_PATH)) { - final byte[] build = getWriter().getContent(BUILD_GRADLE_PATH); + if (getWriter().exists(getBuildGradlePath())) { + final byte[] build = getWriter().getContent(getBuildGradlePath()); buildContent = new String(build, StandardCharsets.UTF_8); } - if (getWriter().exists(GRADLE_PROPERTIES_PATH)) { - final byte[] properties = getWriter().getContent(GRADLE_PROPERTIES_PATH); + if (getWriter().exists(getGradlePropertiesPath())) { + final byte[] properties = getWriter().getContent(getGradlePropertiesPath()); propertiesContent.load(new ByteArrayInputStream(properties)); } if (!propertiesContent.containsKey("quarkusPluginVersion") && !propertiesContent.containsKey("quarkusPlatformGroupId") && !propertiesContent.containsKey("quarkusPlatformArtifactId") && !propertiesContent.containsKey("quarkusPlatformVersion")) { - if (getRootWriter().exists(GRADLE_PROPERTIES_PATH)) { - final byte[] properties = getRootWriter().getContent(GRADLE_PROPERTIES_PATH); + if (getRootWriter().exists(getGradlePropertiesPath())) { + final byte[] properties = getRootWriter().getContent(getGradlePropertiesPath()); propertiesContent.load(new ByteArrayInputStream(properties)); } isRootProperties = true; @@ -306,11 +193,46 @@ protected void initModel() throws IOException { this.model = new Model(settingsContent, buildContent, propertiesContent, isRootProperties); } - protected String getBuildContent() throws IOException { - return getModel().getBuildContent(); + @Override + protected boolean containsBOM(String groupId, String artifactId) throws IOException { + String buildContent = getModel().getBuildContent(); + return buildContent.contains("enforcedPlatform(\"${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:") + || buildContent.contains("enforcedPlatform(\"" + groupId + ":" + artifactId + ":"); + } + + @Override + public void close() throws IOException { + if (getWriter() == getRootWriter()) { + write(getSettingGradlePath(), getModel().getSettingsContent()); + } else { + rootWrite(getSettingGradlePath(), getModel().getSettingsContent()); + } + write(getBuildGradlePath(), getModel().getBuildContent()); + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + getModel().getPropertiesContent().store(out, "Gradle properties"); + if (getModel().isRootProperties()) { + rootWrite(getGradlePropertiesPath(), out.toString(StandardCharsets.UTF_8.toString())); + } else { + write(getGradlePropertiesPath(), out.toString(StandardCharsets.UTF_8.toString())); + } + } + } + + @Override + protected void addDependencyInBuildFile(Dependency dependency) throws IOException { + StringBuilder newBuildContent = new StringBuilder(); + readLineByLine(getModel().getBuildContent(), currentLine -> addDependency(newBuildContent, dependency, currentLine)); + getModel().setBuildContent(newBuildContent.toString()); + } + + protected void addDependency(StringBuilder newContent, Dependency dependency, String currentLine) { + newContent.append(currentLine).append(System.lineSeparator()); + if (currentLine.startsWith("dependencies {")) { + newContent.append(buildDependency(" implementation", dependency)).append(System.lineSeparator()); + } } - private static class Model { + static class Model { private String settingsContent; private String buildContent; private Properties propertiesContent; diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GroovyGradleBuildFile.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GroovyGradleBuildFile.java new file mode 100644 index 00000000000000..c0e36d9eeb0d70 --- /dev/null +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/GroovyGradleBuildFile.java @@ -0,0 +1,115 @@ +package io.quarkus.cli.commands.file; + +import io.quarkus.cli.commands.writer.ProjectWriter; +import io.quarkus.generators.BuildTool; +import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; +import java.io.IOException; +import java.util.Properties; +import org.apache.maven.model.Dependency; + +public class GroovyGradleBuildFile extends GradleBuildFile { + + private static final String BUILD_GRADLE_PATH = "build.gradle"; + private static final String SETTINGS_GRADLE_PATH = "settings.gradle"; + + public GroovyGradleBuildFile(ProjectWriter writer) { + this(writer, writer); + } + + public GroovyGradleBuildFile(ProjectWriter writer, ProjectWriter rootWriter) { + super(writer, rootWriter, BuildTool.GRADLE); + } + + @Override + protected String getBuildGradlePath() { + return BUILD_GRADLE_PATH; + } + + @Override + protected String getSettingGradlePath() { + return SETTINGS_GRADLE_PATH; + } + + @Override + protected void completeBuildContent(String groupId, String version, QuarkusPlatformDescriptor platform, Properties props) + throws IOException { + final String buildContent = getModel().getBuildContent(); + StringBuilder res = new StringBuilder(buildContent); + if (!buildContent.contains("id 'io.quarkus'")) { + res.append("plugins {"); + res.append(System.lineSeparator()).append(" id 'java'").append(System.lineSeparator()); + res.append(System.lineSeparator()).append(" id 'io.quarkus'").append(System.lineSeparator()); + res.append("}"); + } + if (!containsBOM(platform.getBomGroupId(), platform.getBomArtifactId())) { + res.append(System.lineSeparator()); + res.append("dependencies {").append(System.lineSeparator()); + res.append( + " implementation enforcedPlatform(\"${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}\")") + .append(System.lineSeparator()); + res.append(" implementation 'io.quarkus:quarkus-resteasy'").append(System.lineSeparator()); + res.append(" testImplementation 'io.quarkus:quarkus-junit5'").append(System.lineSeparator()); + res.append(" testImplementation 'io.rest-assured:rest-assured'").append(System.lineSeparator()); + res.append("}").append(System.lineSeparator()); + + } + String groupLine = "group '" + groupId + "'"; + if (!buildContent.contains(groupLine)) { + res.append(System.lineSeparator()).append(groupLine) + .append(System.lineSeparator()); + } + String versionLine = "version '" + version + "'"; + if (!buildContent.contains(versionLine)) { + res.append(System.lineSeparator()).append(versionLine) + .append(System.lineSeparator()); + } + + res.append(System.lineSeparator()) + .append("test {").append(System.lineSeparator()) + .append(" systemProperty \"java.util.logging.manager\", \"org.jboss.logmanager.LogManager\"") + .append(System.lineSeparator()) + .append("}"); + + getModel().setBuildContent(res.toString()); + } + + @Override + protected void completeSettingsContent(String artifactId) throws IOException { + final String settingsContent = getModel().getSettingsContent(); + final StringBuilder res = new StringBuilder(); + if (!settingsContent.contains("id 'io.quarkus'")) { + res.append(System.lineSeparator()); + res.append("pluginManagement {").append(System.lineSeparator()); + res.append(" repositories {").append(System.lineSeparator()); + res.append(" mavenLocal()").append(System.lineSeparator()); + res.append(" mavenCentral()").append(System.lineSeparator()); + res.append(" gradlePluginPortal()").append(System.lineSeparator()); + res.append(" }").append(System.lineSeparator()); + res.append(" plugins {").append(System.lineSeparator()); + res.append(" id 'io.quarkus' version \"${quarkusPluginVersion}\"").append(System.lineSeparator()); + res.append(" }").append(System.lineSeparator()); + res.append("}").append(System.lineSeparator()); + } + if (!settingsContent.contains("rootProject.name")) { + res.append(System.lineSeparator()).append("rootProject.name='").append(artifactId).append("'") + .append(System.lineSeparator()); + } + res.append(settingsContent); + getModel().setSettingsContent(res.toString()); + } + + @Override + protected String lineToRemoveDependency(Dependency dependency) { + return "'" + dependency.getGroupId() + ":" + dependency.getArtifactId(); + } + + @Override + protected String buildDependency(String configuration, Dependency dependency) { + if (dependency.getVersion() != null && !dependency.getVersion().isEmpty()) { + return String.format("%s '%s:%s:%s'", configuration, dependency.getGroupId(), dependency.getArtifactId(), + dependency.getVersion()); + } + return String.format("%s '%s:%s'", configuration, dependency.getGroupId(), dependency.getArtifactId()); + } + +} diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/KotlinGradleBuildFile.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/KotlinGradleBuildFile.java new file mode 100644 index 00000000000000..5023fa802784ae --- /dev/null +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/file/KotlinGradleBuildFile.java @@ -0,0 +1,114 @@ +package io.quarkus.cli.commands.file; + +import io.quarkus.cli.commands.writer.ProjectWriter; +import io.quarkus.generators.BuildTool; +import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor; +import java.io.IOException; +import java.util.Properties; +import org.apache.maven.model.Dependency; + +public class KotlinGradleBuildFile extends GradleBuildFile { + + private static final String BUILD_GRADLE_PATH = "build.gradle.kts"; + private static final String SETTINGS_GRADLE_PATH = "settings.gradle.kts"; + + public KotlinGradleBuildFile(ProjectWriter writer) { + this(writer, writer); + } + + public KotlinGradleBuildFile(ProjectWriter writer, ProjectWriter rootWriter) { + super(writer, rootWriter, BuildTool.GRADLE_KOTLIN_DSL); + } + + @Override + protected String getBuildGradlePath() { + return BUILD_GRADLE_PATH; + } + + @Override + protected String getSettingGradlePath() { + return SETTINGS_GRADLE_PATH; + } + + @Override + protected void completeBuildContent(String groupId, String version, QuarkusPlatformDescriptor platform, Properties props) + throws IOException { + final String buildContent = getModel().getBuildContent(); + StringBuilder res = new StringBuilder(buildContent); + if (!buildContent.contains("id(\"io.quarkus\")")) { + res.append("plugins {"); + res.append(System.lineSeparator()).append(" java").append(System.lineSeparator()); + res.append(System.lineSeparator()).append(" id(\"io.quarkus\")").append(System.lineSeparator()); + res.append("}"); + } + if (!containsBOM(platform.getBomGroupId(), platform.getBomArtifactId())) { + res.append(System.lineSeparator()); + res.append("dependencies {").append(System.lineSeparator()); + res.append( + " implementation(enforcedPlatform(\"${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}\"))") + .append(System.lineSeparator()); + res.append(" implementation(\"io.quarkus:quarkus-resteasy\")").append(System.lineSeparator()); + res.append(" testImplementation(\"io.quarkus:quarkus-junit5\")").append(System.lineSeparator()); + res.append(" testImplementation(\"io.rest-assured:rest-assured\")").append(System.lineSeparator()); + res.append("}").append(System.lineSeparator()); + + } + String groupLine = "group = \"" + groupId + "\""; + if (!buildContent.contains(groupLine)) { + res.append(System.lineSeparator()).append(groupLine) + .append(System.lineSeparator()); + } + String versionLine = "version = \"" + version + "\""; + if (!buildContent.contains(versionLine)) { + res.append(System.lineSeparator()).append(versionLine) + .append(System.lineSeparator()); + } + + res.append(System.lineSeparator()) + .append("tasks.withType {").append(System.lineSeparator()) + .append(" systemProperty(\"java.util.logging.manager\", \"org.jboss.logmanager.LogManager\")") + .append(System.lineSeparator()) + .append("}"); + + getModel().setBuildContent(res.toString()); + } + + @Override + protected void completeSettingsContent(String artifactId) throws IOException { + final String settingsContent = getModel().getSettingsContent(); + final StringBuilder res = new StringBuilder(); + if (!settingsContent.contains("id(\"io.quarkus\")")) { + res.append(System.lineSeparator()); + res.append("pluginManagement {").append(System.lineSeparator()); + res.append(" val quarkusPluginVersion: String by settings"); + res.append(" repositories {").append(System.lineSeparator()); + res.append(" mavenLocal()").append(System.lineSeparator()); + res.append(" mavenCentral()").append(System.lineSeparator()); + res.append(" gradlePluginPortal()").append(System.lineSeparator()); + res.append(" }").append(System.lineSeparator()); + res.append(" plugins {").append(System.lineSeparator()); + res.append(" id(\"io.quarkus\") version quarkusPluginVersion").append(System.lineSeparator()); + res.append(" }").append(System.lineSeparator()); + res.append("}").append(System.lineSeparator()); + } + if (!settingsContent.contains("rootProject.name")) { + res.append(System.lineSeparator()).append("rootProject.name=\"").append(artifactId).append("\"") + .append(System.lineSeparator()); + } + res.append(settingsContent); + getModel().setSettingsContent(res.toString()); + } + + @Override + protected String lineToRemoveDependency(Dependency dependency) { + return "\"" + dependency.getGroupId() + ":" + dependency.getArtifactId(); + } + + protected String buildDependency(String configuration, Dependency dependency) { + if (dependency.getVersion() != null && !dependency.getVersion().isEmpty()) { + return String.format("%s(\"%s:%s:%s\")", configuration, dependency.getGroupId(), dependency.getArtifactId(), + dependency.getVersion()); + } + return String.format("%s(\"%s:%s\")", configuration, dependency.getGroupId(), dependency.getArtifactId()); + } +} diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java index 2f07a880ed7c69..b5d38068661b24 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/BuildTool.java @@ -1,7 +1,8 @@ package io.quarkus.generators; import io.quarkus.cli.commands.file.BuildFile; -import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; +import io.quarkus.cli.commands.file.KotlinGradleBuildFile; import io.quarkus.cli.commands.file.MavenBuildFile; import io.quarkus.cli.commands.writer.ProjectWriter; import java.io.IOException; @@ -19,7 +20,12 @@ public enum BuildTool { /** Gradle build tool */ GRADLE("\n# Gradle\n.gradle/\nbuild/", "build", - new String[] { "build.gradle", "settings.gradle", "gradle.properties" }); + new String[] { "build.gradle", "settings.gradle", "gradle.properties" }), + + /** Gradle build tool with Kotlin DSL */ + GRADLE_KOTLIN_DSL("\n# Gradle\n.gradle/\nbuild/", + "build", + new String[] { "build.gradle.kts", "settings.gradle.kts", "gradle.properties" }); private final String gitIgnoreEntries; @@ -55,7 +61,9 @@ public String getBuildDirectory() { public BuildFile createBuildFile(final ProjectWriter writer) throws IOException { switch (this) { case GRADLE: - return new GradleBuildFile(writer); + return new GroovyGradleBuildFile(writer); + case GRADLE_KOTLIN_DSL: + return new KotlinGradleBuildFile(writer); case MAVEN: default: return new MavenBuildFile(writer); diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java index ad20146203a150..aee08e1ca72907 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java @@ -77,12 +77,14 @@ private boolean initProject() throws IOException { testMainPath = writer.mkdirs(type.getTestSrcDir()); // for gradle we want to place the native tests in under 'src/native-test/java' // since gradle's idiomatic way of running integration tests is to create a new source set - if (getBuildTool() == BuildTool.GRADLE) { - nativeTestMainPath = writer.mkdirs(type.getTestSrcDir().replace("test", "native-test")); - } else { - nativeTestMainPath = testMainPath; + switch (getBuildTool()) { + case GRADLE: + case GRADLE_KOTLIN_DSL: + nativeTestMainPath = writer.mkdirs(type.getTestSrcDir().replace("test", "native-test")); + break; + default: + nativeTestMainPath = testMainPath; } - return newProject; } @@ -148,7 +150,8 @@ private void generate(final String templateName, QuarkusCommandInvocation invoca // do some nasty replacements for Java target if we want to generate Java 11 projects if ("11".equals(invocation.getProperty(JAVA_TARGET))) { - if (BuildTool.GRADLE.equals(invocation.getBuildTool())) { + if (BuildTool.GRADLE.equals(invocation.getBuildTool()) + || BuildTool.GRADLE_KOTLIN_DSL.equals(invocation.getBuildTool())) { template = template.replace("JavaVersion.VERSION_1_8", "JavaVersion.VERSION_11"); } else { template = template.replace("1.8", @@ -193,6 +196,7 @@ private void createReadme() throws IOException { generate("templates/README.maven.ftl", invocation, readme, "read me"); break; case GRADLE: + case GRADLE_KOTLIN_DSL: generate("templates/README.gradle.ftl", invocation, readme, "read me"); break; default: diff --git a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/AddGradleExtensionsTest.java b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/AddGradleExtensionsTest.java index feef0d0bb0cbc3..5261979ec3bc78 100644 --- a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/AddGradleExtensionsTest.java +++ b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/AddGradleExtensionsTest.java @@ -1,6 +1,6 @@ package io.quarkus.cli.commands; -import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.generators.BuildTool; import java.io.IOException; @@ -21,7 +21,7 @@ protected List createProject() throws IOException, QuarkusCommandExcepti CreateProjectTest.delete(getProjectPath().toFile()); final FileProjectWriter writer = new FileProjectWriter(getProjectPath().toFile()); new CreateProject(writer, getPlatformDescriptor()) - .buildFile(new GradleBuildFile(writer)) + .buildFile(new GroovyGradleBuildFile(writer)) .groupId("org.acme") .artifactId("add-gradle-extension-test") .version("0.0.1-SNAPSHOT") diff --git a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleExtensionsTest.java b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleExtensionsTest.java index 5662d6cda23435..80b9b017c52b0b 100644 --- a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleExtensionsTest.java +++ b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleExtensionsTest.java @@ -1,6 +1,6 @@ package io.quarkus.cli.commands; -import io.quarkus.cli.commands.file.GradleBuildFile; +import io.quarkus.cli.commands.file.GroovyGradleBuildFile; import io.quarkus.cli.commands.writer.FileProjectWriter; import io.quarkus.generators.BuildTool; import java.io.IOException; @@ -21,7 +21,7 @@ protected List createProject() throws IOException, QuarkusCommandExcepti CreateProjectTest.delete(getProjectPath().toFile()); final FileProjectWriter writer = new FileProjectWriter(getProjectPath().toFile()); new CreateProject(writer, getPlatformDescriptor()) - .buildFile(new GradleBuildFile(writer)) + .buildFile(new GroovyGradleBuildFile(writer)) .groupId("org.acme") .artifactId("add-gradle-extension-test") .version("0.0.1-SNAPSHOT") diff --git a/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleKotlinDslExtensionsTest.java b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleKotlinDslExtensionsTest.java new file mode 100644 index 00000000000000..eb817c9d6046c9 --- /dev/null +++ b/independent-projects/tools/common/src/test/java/io/quarkus/cli/commands/RemoveGradleKotlinDslExtensionsTest.java @@ -0,0 +1,66 @@ +package io.quarkus.cli.commands; + +import io.quarkus.cli.commands.file.KotlinGradleBuildFile; +import io.quarkus.cli.commands.writer.FileProjectWriter; +import io.quarkus.generators.BuildTool; +import java.io.IOException; +import java.nio.file.Files; +import java.util.HashSet; +import java.util.List; +import org.junit.jupiter.api.Disabled; + +class RemoveGradleKotlinDslExtensionsTest extends AbstractRemoveExtensionsTest> { + + @Disabled + void addExtensionTwiceInTwoBatches() throws IOException { + //FIXME This is currently not working + } + + @Override + protected List createProject() throws IOException, QuarkusCommandException { + CreateProjectTest.delete(getProjectPath().toFile()); + final FileProjectWriter writer = new FileProjectWriter(getProjectPath().toFile()); + new CreateProject(writer, getPlatformDescriptor()) + .buildFile(new KotlinGradleBuildFile(writer)) + .groupId("org.acme") + .artifactId("add-gradle-extension-test") + .version("0.0.1-SNAPSHOT") + .execute(); + return readProject(); + } + + @Override + protected List readProject() throws IOException { + return Files.readAllLines(getProjectPath().resolve("build.gradle.kts")); + } + + @Override + protected QuarkusCommandOutcome addExtensions(final List extensions) throws IOException, QuarkusCommandException { + return new AddExtensions(new FileProjectWriter(getProjectPath().toFile()), BuildTool.GRADLE_KOTLIN_DSL, + getPlatformDescriptor()) + .extensions(new HashSet<>(extensions)) + .execute(); + } + + @Override + protected QuarkusCommandOutcome removeExtensions(final List extensions) + throws IOException, QuarkusCommandException { + return new RemoveExtensions(new FileProjectWriter(getProjectPath().toFile()), BuildTool.GRADLE_KOTLIN_DSL, + getPlatformDescriptor()) + .extensions(new HashSet<>(extensions)) + .execute(); + } + + @Override + protected long countDependencyOccurrences(final List buildFile, final String groupId, final String artifactId, + final String version) { + return buildFile.stream() + .filter(d -> d.equals(getBuildFileDependencyString(groupId, artifactId, version))) + .count(); + } + + private static String getBuildFileDependencyString(final String groupId, final String artifactId, final String version) { + final String versionPart = version != null ? ":" + version : ""; + return " implementation(\"" + groupId + ":" + artifactId + versionPart + "\")"; + } +} diff --git a/independent-projects/tools/common/src/test/resources/templates/basic-rest/java/build.gradle.kts-template.ftl b/independent-projects/tools/common/src/test/resources/templates/basic-rest/java/build.gradle.kts-template.ftl new file mode 100644 index 00000000000000..e3090dde05e72d --- /dev/null +++ b/independent-projects/tools/common/src/test/resources/templates/basic-rest/java/build.gradle.kts-template.ftl @@ -0,0 +1,34 @@ +plugins { + java + id("io.quarkus") +} + +repositories { + mavenLocal() + mavenCentral() +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") +} + +group = "${project_groupId}" +version = "${project_version}" + +tasks.withType { + options.encoding = "UTF-8" + options.compilerArgs.add("-parameters") +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/independent-projects/tools/common/src/test/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl b/independent-projects/tools/common/src/test/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl new file mode 100644 index 00000000000000..b138b98ad82f79 --- /dev/null +++ b/independent-projects/tools/common/src/test/resources/templates/basic-rest/java/settings.gradle.kts-template.ftl @@ -0,0 +1,12 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} +rootProject.name="${project_artifactId}" diff --git a/independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl b/independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl new file mode 100644 index 00000000000000..ec34629eb43233 --- /dev/null +++ b/independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/build.gradle.kts-template.ftl @@ -0,0 +1,51 @@ +plugins { + java + id("io.quarkus") + kotlin("jvm") version "${kotlin_version}" + kotlin("plugin.allopen") version "${kotlin_version}" +} + +repositories { + mavenLocal() + mavenCentral() +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) + implementation(kotlin("stdlib-jdk8")) + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") +} + +group = "${project_groupId}" +version = "${project_version}" + +quarkus { + setOutputDirectory("$projectDir/build/classes/kotlin/main") +} + +tasks.withType { + setSourceDir("$projectDir/src/main/kotlin") +} + +allOpen { + annotation("javax.ws.rs.Path") + annotation("javax.enterprise.context.ApplicationScoped") + annotation("io.quarkus.test.junit.QuarkusTest") +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +tasks.withType { + kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() + kotlinOptions.javaParameters = true +} diff --git a/independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl b/independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl new file mode 100644 index 00000000000000..b138b98ad82f79 --- /dev/null +++ b/independent-projects/tools/common/src/test/resources/templates/basic-rest/kotlin/settings.gradle.kts-template.ftl @@ -0,0 +1,12 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} +rootProject.name="${project_artifactId}" diff --git a/independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl b/independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl new file mode 100644 index 00000000000000..1d2d29667a150a --- /dev/null +++ b/independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/build.gradle.kts-template.ftl @@ -0,0 +1,36 @@ +plugins { + scala + id("io.quarkus") +} + +repositories { + mavenLocal() + mavenCentral() +} + +val quarkusPlatformGroupId: String by project +val quarkusPlatformArtifactId: String by project +val quarkusPlatformVersion: String by project + +dependencies { + implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")) + implementation("org.scala-lang:scala-library:${scala_version}") + implementation("io.quarkus:quarkus-resteasy") + + testImplementation("io.quarkus:quarkus-junit5") + testImplementation("io.rest-assured:rest-assured") +} + +group = "${project_groupId}" +version = "${project_version}" + +tasks.withType { + scalaCompileOptions.encoding = "UTF-8" + sourceCompatibility = JavaVersion.VERSION_1_8.toString() + targetCompatibility = JavaVersion.VERSION_1_8.toString() +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl b/independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl new file mode 100644 index 00000000000000..b138b98ad82f79 --- /dev/null +++ b/independent-projects/tools/common/src/test/resources/templates/basic-rest/scala/settings.gradle.kts-template.ftl @@ -0,0 +1,12 @@ +pluginManagement { + val quarkusPluginVersion: String by settings + repositories { + mavenLocal() + mavenCentral() + gradlePluginPortal() + } + plugins { + id("io.quarkus") version quarkusPluginVersion + } +} +rootProject.name="${project_artifactId}"