Skip to content

Commit

Permalink
Gradle: pass system properties as a parameter to QuarkusWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky committed Nov 10, 2023
1 parent 43b68c1 commit 2227695
Show file tree
Hide file tree
Showing 25 changed files with 391 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.quarkus.gradle.QuarkusPlugin;
import io.quarkus.gradle.tasks.worker.BuildWorker;
import io.quarkus.maven.dependency.GACTV;
import io.smallrye.config.common.utils.ConfigSourceUtil;

/**
* Base class for the {@link QuarkusBuildDependencies}, {@link QuarkusBuildCacheableAppParts}, {@link QuarkusBuild} tasks
Expand Down Expand Up @@ -228,6 +229,7 @@ void generateBuild() {

workQueue.submit(BuildWorker.class, params -> {
params.getBuildSystemProperties().putAll(extension().buildSystemProperties(appModel.getAppArtifact()));
params.getSystemProperties().putAll(ConfigSourceUtil.propertiesToMap(System.getProperties()));
params.getBaseName().set(extension().finalName());
params.getTargetDirectory().set(buildDir.toFile());
params.getAppModel().set(appModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.gradle.tasks.worker.CodeGenWorker;
import io.quarkus.runtime.LaunchMode;
import io.smallrye.config.common.utils.ConfigSourceUtil;

@CacheableTask
public abstract class QuarkusGenerateCode extends QuarkusTask {
Expand Down Expand Up @@ -113,6 +114,7 @@ public void generateCode() {

workQueue.submit(CodeGenWorker.class, params -> {
params.getBuildSystemProperties().putAll(configMap);
params.getSystemProperties().putAll(ConfigSourceUtil.propertiesToMap(System.getProperties()));
params.getBaseName().set(extension().finalName());
params.getTargetDirectory().set(buildDir);
params.getAppModel().set(appModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class BuildWorker extends QuarkusWorker<BuildWorkerParams> {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildWorker.class);

@Override
public void execute() {
protected void doExecute() {
BuildWorkerParams params = getParameters();
Properties props = buildSystemProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class CodeGenWorker extends QuarkusWorker<CodeGenWorkerParams> {
public static final String INIT_AND_RUN = "initAndRun";

@Override
public void execute() {
protected void doExecute() {
CodeGenWorkerParams params = getParameters();
Properties props = buildSystemProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface QuarkusParams extends WorkParameters {

MapProperty<String, String> getBuildSystemProperties();

MapProperty<String, String> getSystemProperties();

Property<String> getBaseName();

Property<ApplicationModel> getAppModel();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.quarkus.gradle.tasks.worker;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import org.gradle.workers.WorkAction;
Expand Down Expand Up @@ -34,4 +37,42 @@ CuratedApplication createAppCreationContext() throws BootstrapException {
.setIsolateDeployment(true)
.build().bootstrap();
}

@Override
public final void execute() {
var originalProps = setAllAndReturnOverridden(getParameters().getSystemProperties().get(), System.getProperties());
try {
doExecute();
} finally {
for (var p : originalProps.entrySet()) {
if (p.getValue() == null) {
System.clearProperty(p.getKey());
} else {
System.setProperty(p.getKey(), p.getValue());
}
}
}
}

protected abstract void doExecute();

private static Map<String, String> setAllAndReturnOverridden(Map<?, ?> src, Properties target) {
var originals = new HashMap<String, String>();
for (var p : src.entrySet()) {
if (p.getKey() == null) {
continue;
}
var name = p.getKey().toString();
if (name.startsWith("sun.")
|| name.startsWith("java.")) {
continue;
}
var newValue = p.getValue() == null ? null : p.getValue().toString();
var originalValue = target.setProperty(name, newValue);
if (!Objects.equals(newValue, originalValue)) {
originals.put(name, (String) originalValue);
}
}
return originals;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins{
id "java"
id "io.quarkus"
}



group 'io.quarkus.test.application'
version '1.0-SNAPSHOT'


repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy-reactive'
implementation ('org.acme.extensions:example-extension')

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
}

test {
useJUnitPlatform()
}
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,20 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
gradlePluginPortal()
}
//noinspection GroovyAssignabilityCheck
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}

includeBuild('../extensions/example-extension'){

}
rootProject.name='application'
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.acme;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusIntegrationTest
public class ExampleResourceIT {

@Test
public void testHelloEndpoint() {
given()
.when().get("/hello")
.then()
.statusCode(200)
.body(is("hello cheburashka"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.acme.quarkus.sample;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.acme.example.extension.runtime.ExampleBuildOptions;

@Path("/hello")
public class HelloResource {

@Inject
ExampleBuildOptions buildOptions;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello " + buildOptions.name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins{
id 'java-library'
id 'maven-publish'
}
subprojects {subProject->
apply plugin: 'java-library'
apply plugin: 'maven-publish'

group 'org.acme.extensions'
version '1.0-SNAPSHOT'
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.acme.extensions'
artifactId = subProject.name
version = '1.0-SNAPSHOT'
from components.java
}
}
}
}

publishing {
publications {
maven(MavenPublication) {
groupId = 'org.acme.extensions'
artifactId = rootProject.name
version = '1.0-SNAPSHOT'
from components.java
}
}
}
group 'org.acme.extensions'
version '1.0-SNAPSHOT'
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
id 'java'
id 'java-library'
}
repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation platform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
annotationProcessor "io.quarkus:quarkus-extension-processor:${quarkusPlatformVersion}"


api project(':example-extension') // why: https://quarkus.io/guides/building-my-first-extension
implementation 'io.quarkus:quarkus-arc-deployment'
}

java {
// withJavadocJar()
withSourcesJar()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.acme.example.extension.deployment;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot
public class ExampleConfig {

/**
* name
*/
@ConfigItem(defaultValue = "none")
String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.acme.example.extension.deployment;

import jakarta.inject.Singleton;

import org.acme.example.extension.runtime.ExampleBuildOptions;
import org.acme.example.extension.runtime.ExampleRecorder;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;

class ExampleProcessor {

private static final String FEATURE = "example";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
SyntheticBeanBuildItem syntheticBean(ExampleRecorder recorder, ExampleConfig config) {
return SyntheticBeanBuildItem.configure(ExampleBuildOptions.class)
.scope(Singleton.class)
.runtimeValue(recorder.buildOptions(config.name))
.done();
}
}
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,20 @@

plugins {
id 'io.quarkus.extension'
}

quarkusExtension {
deploymentModule = 'example-extension-deployment'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation platform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
annotationProcessor "io.quarkus:quarkus-extension-processor:${quarkusPlatformVersion}"
implementation 'io.quarkus:quarkus-arc'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.acme.example.extension.runtime;

public class ExampleBuildOptions {

public final String name;

public ExampleBuildOptions(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.acme.example.extension.runtime;

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class ExampleRecorder {

public RuntimeValue<ExampleBuildOptions> buildOptions(String name) {
System.out.println("ExampleRecorder.buildOptions " + name);
return new RuntimeValue<>(new ExampleBuildOptions(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deployment-artifact=org.acme.extensions\:example-extension-deployment\:1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: Quarkus Example Extension
artifact: ${project.groupId}:${project.artifactId}:${project.version}
metadata:
config:
- "quarkus.example.extension."
keywords:
- "logzio"
- "logging"
categories:
- "logging"
description: "Quarkus example extension"
Loading

0 comments on commit 2227695

Please sign in to comment.