From 8ffa353b4bb9ea27003c502073e7a2d9c15d5868 Mon Sep 17 00:00:00 2001 From: Fouad Almalki Date: Sat, 15 Apr 2023 15:00:06 +0300 Subject: [PATCH] add environmentVariables to gradle task quarkusDev --- .../io/quarkus/gradle/tasks/QuarkusDev.java | 15 ++++++++++ docs/src/main/asciidoc/gradle-tooling.adoc | 25 ++++++++++++++++ .../build.gradle | 29 +++++++++++++++++++ .../gradle.properties | 2 ++ .../settings.gradle | 19 ++++++++++++ .../acme/quarkus/sample/HelloResource.java | 18 ++++++++++++ .../AddEnvironmentVariablesDevModeTest.java | 20 +++++++++++++ 7 files changed, 128 insertions(+) create mode 100644 integration-tests/gradle/src/main/resources/add-envitonment-variables-app/build.gradle create mode 100644 integration-tests/gradle/src/main/resources/add-envitonment-variables-app/gradle.properties create mode 100644 integration-tests/gradle/src/main/resources/add-envitonment-variables-app/settings.gradle create mode 100644 integration-tests/gradle/src/main/resources/add-envitonment-variables-app/src/main/java/org/acme/quarkus/sample/HelloResource.java create mode 100644 integration-tests/gradle/src/test/java/io/quarkus/gradle/devmode/AddEnvironmentVariablesDevModeTest.java diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java index dc4bf01c0e669..afcdc38d5960e 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusDev.java @@ -32,6 +32,7 @@ import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.provider.ListProperty; +import org.gradle.api.provider.MapProperty; import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.CompileClasspath; @@ -82,6 +83,7 @@ public abstract class QuarkusDev extends QuarkusTask { private final CompilerOptions compilerOptions = new CompilerOptions(); private final Property workingDirectory; + private final MapProperty environmentVariables; private final Property preventNoVerify; private final Property shouldPropagateJavaCompilerArgs; @@ -114,6 +116,8 @@ public QuarkusDev( workingDirectory = objectFactory.property(File.class); workingDirectory.convention(getProject().provider(() -> QuarkusPluginExtension.getLastFile(getCompilationOutput()))); + environmentVariables = objectFactory.mapProperty(String.class, String.class); + preventNoVerify = objectFactory.property(Boolean.class); preventNoVerify.convention(false); @@ -177,6 +181,16 @@ public void setWorkingDir(String workingDir) { workingDirectory.set(getProject().file(workingDir)); } + @Input + public MapProperty getEnvironmentVariables() { + return environmentVariables; + } + + @Internal + public Map getEnvVars() { + return environmentVariables.get(); + } + @Input public Property getPreventNoVerify() { return preventNoVerify; @@ -307,6 +321,7 @@ public void startDev() { if (outputFile == null) { getProject().exec(action -> { action.commandLine(runner.args()).workingDir(getWorkingDirectory().get()); + action.environment(getEnvVars()); action.setStandardInput(System.in) .setErrorOutput(System.out) .setStandardOutput(System.out); diff --git a/docs/src/main/asciidoc/gradle-tooling.adoc b/docs/src/main/asciidoc/gradle-tooling.adoc index e0308c69dc3d3..097d49343181c 100644 --- a/docs/src/main/asciidoc/gradle-tooling.adoc +++ b/docs/src/main/asciidoc/gradle-tooling.adoc @@ -208,6 +208,31 @@ By default, `quarkusDev` sets the debug host to `localhost` (for security reason include::{includes}/devtools/dev-parameters.adoc[] :!dev-additional-parameters: ==== + +You also can add environment variables to the development environment: + +[role="primary asciidoc-tabs-sync-groovy"] +.Groovy DSL +**** +[source,groovy] +---- +quarkusDev { + environmentVariables = [FOO_VALUE: 'abc', BAR_VALUE: 'def'] +} +---- +**** + +[role="secondary asciidoc-tabs-sync-kotlin"] +.Kotlin DSL +**** +[source,kotlin] +---- +tasks.quarkusDev { + environmentVariables.set(mapOf("FOO_VALUE" to "abc", "BAR_VALUE" to "def")) +} +---- +**** + The plugin also exposes a `quarkusDev` configuration. Using this configuration to declare a dependency will restrict the usage of that dependency to development mode. The `quarkusDev` configuration can be used as following: diff --git a/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/build.gradle b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/build.gradle new file mode 100644 index 0000000000000..dd581f4bd0eab --- /dev/null +++ b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/build.gradle @@ -0,0 +1,29 @@ +plugins { + id 'java' + id 'io.quarkus' +} + +group = 'com.quarkus.demo' +version = '1.0' + +repositories { + mavenLocal { + content { + includeGroupByRegex 'io.quarkus.*' + } + } + mavenCentral() +} + +quarkusDev { + environmentVariables = [FOO_VALUE: 'abc', BAR_VALUE: 'def'] +} + +dependencies { + implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}") + implementation 'io.quarkus:quarkus-resteasy' +} + +test { + systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager" +} \ No newline at end of file diff --git a/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/gradle.properties b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/gradle.properties new file mode 100644 index 0000000000000..8f063b7d88ba4 --- /dev/null +++ b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/gradle.properties @@ -0,0 +1,2 @@ +quarkusPlatformArtifactId=quarkus-bom +quarkusPlatformGroupId=io.quarkus \ No newline at end of file diff --git a/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/settings.gradle b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/settings.gradle new file mode 100644 index 0000000000000..d50360e06f19d --- /dev/null +++ b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/settings.gradle @@ -0,0 +1,19 @@ +pluginManagement { + repositories { + mavenLocal { + content { + includeGroupByRegex 'io.quarkus.*' + } + } + mavenCentral() + gradlePluginPortal() + } + //noinspection GroovyAssignabilityCheck + plugins { + id 'io.quarkus' version "${quarkusPluginVersion}" + } +} + +rootProject.name = 'add-envitonment-variables-app' + + diff --git a/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/src/main/java/org/acme/quarkus/sample/HelloResource.java b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/src/main/java/org/acme/quarkus/sample/HelloResource.java new file mode 100644 index 0000000000000..7a3366673e03e --- /dev/null +++ b/integration-tests/gradle/src/main/resources/add-envitonment-variables-app/src/main/java/org/acme/quarkus/sample/HelloResource.java @@ -0,0 +1,18 @@ +package org.acme.quarkus.sample; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; + +@Path("/hello") +public class HelloResource { + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String hello() { + String foo = System.getenv("FOO_VALUE"); // abc + String bar = System.getenv("BAR_VALUE"); // def + return foo + bar; // abcdef + } +} diff --git a/integration-tests/gradle/src/test/java/io/quarkus/gradle/devmode/AddEnvironmentVariablesDevModeTest.java b/integration-tests/gradle/src/test/java/io/quarkus/gradle/devmode/AddEnvironmentVariablesDevModeTest.java new file mode 100644 index 0000000000000..5a034aca44497 --- /dev/null +++ b/integration-tests/gradle/src/test/java/io/quarkus/gradle/devmode/AddEnvironmentVariablesDevModeTest.java @@ -0,0 +1,20 @@ +package io.quarkus.gradle.devmode; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddEnvironmentVariablesDevModeTest extends QuarkusDevGradleTestBase { + + @Override + protected String projectDirectoryName() { + return "add-envitonment-variables-app"; + } + + @Override + protected String[] buildArguments() { + return new String[] { "clean", "quarkusDev" }; + } + + protected void testDevMode() throws Exception { + assertThat(getHttpResponse("/hello")).contains("abcdef"); + } +} \ No newline at end of file