Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environmentVariables to gradle task quarkusDev #32662

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,6 +83,7 @@ public abstract class QuarkusDev extends QuarkusTask {
private final CompilerOptions compilerOptions = new CompilerOptions();

private final Property<File> workingDirectory;
private final MapProperty<String, String> environmentVariables;

private final Property<Boolean> preventNoVerify;
private final Property<Boolean> shouldPropagateJavaCompilerArgs;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -177,6 +181,16 @@ public void setWorkingDir(String workingDir) {
workingDirectory.set(getProject().file(workingDir));
}

@Input
public MapProperty<String, String> getEnvironmentVariables() {
return environmentVariables;
}

@Internal
public Map<String, String> getEnvVars() {
return environmentVariables.get();
}

@Input
public Property<Boolean> getPreventNoVerify() {
return preventNoVerify;
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions docs/src/main/asciidoc/gradle-tooling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -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'


Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}