Skip to content

Commit

Permalink
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 6b711c9 commit e8bff27
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -226,6 +227,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 @@ -26,6 +26,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 @@ -102,6 +103,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;
}
}

0 comments on commit e8bff27

Please sign in to comment.