From be4817d541882225767e9e3655ec5bf325045360 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Mon, 9 Sep 2024 19:25:59 +0100 Subject: [PATCH] Add runtime properties to Quarkus builder --- .../io/quarkus/deployment/CodeGenerator.java | 6 ++++-- .../quarkus/deployment/ExtensionLoader.java | 6 ++++-- .../quarkus/deployment/QuarkusAugmentor.java | 18 ++++++++++++----- .../BuildTimeConfigurationReader.java | 20 +++++++++---------- .../deployment/jbang/JBangAugmentorImpl.java | 1 + .../io/quarkus/launcher/JBangIntegration.java | 9 +++++++-- .../bootstrap/app/QuarkusBootstrap.java | 13 ++++++++++++ .../bootstrap/jbang/JBangBuilderImpl.java | 9 ++++++++- 8 files changed, 60 insertions(+), 22 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/CodeGenerator.java b/core/deployment/src/main/java/io/quarkus/deployment/CodeGenerator.java index 8e5d12873a19b..c98dad86571c4 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/CodeGenerator.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/CodeGenerator.java @@ -227,7 +227,8 @@ public static void dumpCurrentConfigValues(ApplicationModel appModel, String lau if (previouslyRecordedProperties.isEmpty()) { try { readConfig(appModel, mode, buildSystemProps, deploymentClassLoader, configReader -> { - var config = configReader.initConfiguration(mode, buildSystemProps, appModel.getPlatformProperties()); + var config = configReader.initConfiguration(mode, buildSystemProps, new Properties(), + appModel.getPlatformProperties()); final Map allProps = new HashMap<>(); for (String name : config.getPropertyNames()) { allProps.put(name, ConfigTrackingValueTransformer.asString(config.getConfigValue(name))); @@ -287,7 +288,8 @@ public static void dumpCurrentConfigValues(ApplicationModel appModel, String lau public static Config getConfig(ApplicationModel appModel, LaunchMode launchMode, Properties buildSystemProps, QuarkusClassLoader deploymentClassLoader) throws CodeGenException { return readConfig(appModel, launchMode, buildSystemProps, deploymentClassLoader, - configReader -> configReader.initConfiguration(launchMode, buildSystemProps, appModel.getPlatformProperties())); + configReader -> configReader.initConfiguration(launchMode, buildSystemProps, new Properties(), + appModel.getPlatformProperties())); } public static T readConfig(ApplicationModel appModel, LaunchMode launchMode, Properties buildSystemProps, diff --git a/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java b/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java index 6549aed18394b..6d7bc5bd526c9 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java @@ -132,12 +132,14 @@ private static boolean isRecorder(AnnotatedElement element) { * @throws IOException if the class loader could not load a resource * @throws ClassNotFoundException if a build step class is not found */ - public static Consumer loadStepsFrom(ClassLoader classLoader, Properties buildSystemProps, + public static Consumer loadStepsFrom(ClassLoader classLoader, + Properties buildSystemProps, Properties runtimeProperties, ApplicationModel appModel, LaunchMode launchMode, DevModeType devModeType) throws IOException, ClassNotFoundException { final BuildTimeConfigurationReader reader = new BuildTimeConfigurationReader(classLoader); - final SmallRyeConfig src = reader.initConfiguration(launchMode, buildSystemProps, appModel.getPlatformProperties()); + final SmallRyeConfig src = reader.initConfiguration(launchMode, buildSystemProps, runtimeProperties, + appModel.getPlatformProperties()); // install globally QuarkusConfigFactory.setConfig(src); final BuildTimeConfigurationReader.ReadResult readResult = reader.readConfiguration(src); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/QuarkusAugmentor.java b/core/deployment/src/main/java/io/quarkus/deployment/QuarkusAugmentor.java index ef13a5eeb0ea8..b423d83378756 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/QuarkusAugmentor.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/QuarkusAugmentor.java @@ -55,6 +55,7 @@ public class QuarkusAugmentor { private final Collection excludedFromIndexing; private final LiveReloadBuildItem liveReloadBuildItem; private final Properties buildSystemProperties; + private final Properties runtimeProperties; private final Path targetDir; private final ApplicationModel effectiveModel; private final Supplier depInfoProvider; @@ -75,6 +76,7 @@ public class QuarkusAugmentor { this.excludedFromIndexing = builder.excludedFromIndexing; this.liveReloadBuildItem = builder.liveReloadState; this.buildSystemProperties = builder.buildSystemProperties; + this.runtimeProperties = builder.runtimeProperties; this.targetDir = builder.targetDir; this.effectiveModel = builder.effectiveModel; this.baseName = builder.baseName; @@ -102,13 +104,9 @@ public BuildResult run() throws Exception { final BuildChainBuilder chainBuilder = BuildChain.builder(); chainBuilder.setClassLoader(deploymentClassLoader); - //provideCapabilities(chainBuilder); - - //TODO: we load everything from the deployment class loader - //this allows the deployment config (application.properties) to be loaded, but in theory could result - //in additional stuff from the deployment leaking in, this is unlikely but has a bit of a smell. ExtensionLoader.loadStepsFrom(deploymentClassLoader, buildSystemProperties == null ? new Properties() : buildSystemProperties, + runtimeProperties == null ? new Properties() : runtimeProperties, effectiveModel, launchMode, devModeType) .accept(chainBuilder); @@ -210,6 +208,7 @@ public static final class Builder { LaunchMode launchMode = LaunchMode.NORMAL; LiveReloadBuildItem liveReloadState = new LiveReloadBuildItem(); Properties buildSystemProperties; + Properties runtimeProperties; ApplicationModel effectiveModel; String baseName = QUARKUS_APPLICATION; @@ -322,6 +321,15 @@ public Builder setBuildSystemProperties(final Properties buildSystemProperties) return this; } + public Properties getRuntimeProperties() { + return runtimeProperties; + } + + public Builder setRuntimeProperties(final Properties runtimeProperties) { + this.runtimeProperties = runtimeProperties; + return this; + } + public Builder setRebuild(boolean rebuild) { this.rebuild = rebuild; return this; diff --git a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java index e42d94cbb07b6..e493211f6336e 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java @@ -380,7 +380,7 @@ public List getBuildTimeVisibleMappings() { * @param platformProperties Quarkus platform properties to add as a configuration source * @return configuration instance */ - public SmallRyeConfig initConfiguration(LaunchMode launchMode, Properties buildSystemProps, + public SmallRyeConfig initConfiguration(LaunchMode launchMode, Properties buildSystemProps, Properties runtimeProperties, Map platformProperties) { // now prepare & load the build configuration SmallRyeConfigBuilder builder = ConfigUtils.configBuilder(false, launchMode); @@ -388,17 +388,17 @@ public SmallRyeConfig initConfiguration(LaunchMode launchMode, Properties buildS builder.forClassLoader(classLoader); } - DefaultValuesConfigurationSource ds1 = new DefaultValuesConfigurationSource(getBuildTimePatternMap()); - DefaultValuesConfigurationSource ds2 = new DefaultValuesConfigurationSource(getBuildTimeRunTimePatternMap()); - PropertiesConfigSource pcs = new PropertiesConfigSource(buildSystemProps, "Build system"); - if (platformProperties.isEmpty()) { - builder.withSources(ds1, ds2, pcs); - } else { + builder + .withSources(new DefaultValuesConfigurationSource(getBuildTimePatternMap())) + .withSources(new DefaultValuesConfigurationSource(getBuildTimeRunTimePatternMap())) + .withSources(new PropertiesConfigSource(buildSystemProps, "Build system")) + .withSources(new PropertiesConfigSource(runtimeProperties, "Runtime Properties")); + + if (!platformProperties.isEmpty()) { // Our default value configuration source is using an ordinal of Integer.MIN_VALUE // (see io.quarkus.deployment.configuration.DefaultValuesConfigurationSource) - DefaultValuesConfigSource platformConfigSource = new DefaultValuesConfigSource(platformProperties, - "Quarkus platform", Integer.MIN_VALUE + 1000); - builder.withSources(ds1, ds2, platformConfigSource, pcs); + builder.withSources( + new DefaultValuesConfigSource(platformProperties, "Quarkus platform", Integer.MIN_VALUE + 1000)); } for (ConfigClassWithPrefix mapping : getBuildTimeVisibleMappings()) { diff --git a/core/deployment/src/main/java/io/quarkus/deployment/jbang/JBangAugmentorImpl.java b/core/deployment/src/main/java/io/quarkus/deployment/jbang/JBangAugmentorImpl.java index 4fe1d0469bd19..9d94bcac05481 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/jbang/JBangAugmentorImpl.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/jbang/JBangAugmentorImpl.java @@ -51,6 +51,7 @@ public void accept(CuratedApplication curatedApplication, Map re .setTargetDir(quarkusBootstrap.getTargetDirectory()) .setDeploymentClassLoader(curatedApplication.createDeploymentClassLoader()) .setBuildSystemProperties(quarkusBootstrap.getBuildSystemProperties()) + .setRuntimeProperties(quarkusBootstrap.getRuntimeProperties()) .setEffectiveModel(curatedApplication.getApplicationModel()); if (quarkusBootstrap.getBaseName() != null) { builder.setBaseName(quarkusBootstrap.getBaseName()); diff --git a/core/launcher/src/main/java/io/quarkus/launcher/JBangIntegration.java b/core/launcher/src/main/java/io/quarkus/launcher/JBangIntegration.java index 7e7cf1fb12cb2..e0adc8e68e543 100644 --- a/core/launcher/src/main/java/io/quarkus/launcher/JBangIntegration.java +++ b/core/launcher/src/main/java/io/quarkus/launcher/JBangIntegration.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import io.quarkus.bootstrap.BootstrapConstants; @@ -46,6 +47,7 @@ public static Map postBuild(Path appClasses, Path pomFile, List< return ret; } + Properties configurationProperties = new Properties(); for (String comment : comments) { //we allow config to be provided via //Q:CONFIG name=value if (comment.startsWith(CONFIG)) { @@ -54,7 +56,7 @@ public static Map postBuild(Path appClasses, Path pomFile, List< if (equals == -1) { throw new RuntimeException("invalid config " + comment); } - System.setProperty(conf.substring(0, equals), conf.substring(equals + 1)); + configurationProperties.setProperty(conf.substring(0, equals), conf.substring(equals + 1)); } } @@ -117,12 +119,15 @@ public Enumeration getResources(String name) throws IOException { Thread.currentThread().setContextClassLoader(loader); Class launcher = loader.loadClass("io.quarkus.bootstrap.jbang.JBangBuilderImpl"); return (Map) launcher - .getDeclaredMethod("postBuild", Path.class, Path.class, List.class, List.class, boolean.class).invoke( + .getDeclaredMethod("postBuild", Path.class, Path.class, List.class, List.class, Properties.class, + boolean.class) + .invoke( null, appClasses, pomFile, repositories, dependencies, + configurationProperties, nativeImage); } catch (Exception e) { throw new RuntimeException(e); diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java index 966629bcc767e..35f44526d7eab 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/QuarkusBootstrap.java @@ -65,6 +65,7 @@ public class QuarkusBootstrap implements Serializable { private final List excludeFromClassPath; private final Properties buildSystemProperties; + private final Properties runtimeProperties; private final String baseName; private final String originalBaseName; private final Path targetDirectory; @@ -100,6 +101,7 @@ private QuarkusBootstrap(Builder builder) { this.excludeFromClassPath = new ArrayList<>(builder.excludeFromClassPath); this.projectRoot = builder.projectRoot != null ? builder.projectRoot.normalize() : null; this.buildSystemProperties = builder.buildSystemProperties != null ? builder.buildSystemProperties : new Properties(); + this.runtimeProperties = builder.runtimeProperties != null ? builder.runtimeProperties : new Properties(); this.mode = builder.mode; this.offline = builder.offline; this.test = builder.test; @@ -202,6 +204,10 @@ public Properties getBuildSystemProperties() { return buildSystemProperties; } + public Properties getRuntimeProperties() { + return runtimeProperties; + } + public Path getProjectRoot() { return projectRoot; } @@ -266,6 +272,7 @@ public Builder clonedBuilder() { .setProjectRoot(projectRoot) .setBaseClassLoader(baseClassLoader) .setBuildSystemProperties(buildSystemProperties) + .setRuntimeProperties(runtimeProperties) .setMode(mode) .setTest(test) .setLocalProjectDiscovery(localProjectDiscovery) @@ -316,6 +323,7 @@ public static class Builder { final List additionalDeploymentArchives = new ArrayList<>(); final List excludeFromClassPath = new ArrayList<>(); Properties buildSystemProperties; + Properties runtimeProperties; Mode mode = Mode.PROD; Boolean offline; boolean test; @@ -389,6 +397,11 @@ public Builder setBuildSystemProperties(Properties buildSystemProperties) { return this; } + public Builder setRuntimeProperties(Properties runtimeProperties) { + this.runtimeProperties = runtimeProperties; + return this; + } + public Builder setOffline(boolean offline) { this.offline = offline; return this; diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/jbang/JBangBuilderImpl.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/jbang/JBangBuilderImpl.java index 4b5c60c0bdd93..26fb7f706b243 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/jbang/JBangBuilderImpl.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/jbang/JBangBuilderImpl.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.stream.Collectors; import org.eclipse.aether.repository.RemoteRepository; @@ -26,8 +27,12 @@ import io.quarkus.maven.dependency.ResolvedArtifactDependency; public class JBangBuilderImpl { - public static Map postBuild(Path appClasses, Path pomFile, List> repositories, + public static Map postBuild( + Path appClasses, + Path pomFile, + List> repositories, List> dependencies, + Properties configurationProperties, boolean nativeImage) { final MavenArtifactResolver quarkusResolver; try { @@ -80,6 +85,8 @@ public static Map postBuild(Path appClasses, Path pomFile, List< }).collect(Collectors.toList())) .setAppArtifact(appArtifact) .setIsolateDeployment(true) + .setBuildSystemProperties(configurationProperties) + .setRuntimeProperties(configurationProperties) .setMode(QuarkusBootstrap.Mode.PROD); CuratedApplication app = builder