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 runtime properties to Quarkus builder #43160

Merged
merged 1 commit into from
Sep 10, 2024
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 @@ -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<String, String> allProps = new HashMap<>();
for (String name : config.getPropertyNames()) {
allProps.put(name, ConfigTrackingValueTransformer.asString(config.getConfigValue(name)));
Expand Down Expand Up @@ -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> T readConfig(ApplicationModel appModel, LaunchMode launchMode, Properties buildSystemProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuildChainBuilder> loadStepsFrom(ClassLoader classLoader, Properties buildSystemProps,
public static Consumer<BuildChainBuilder> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class QuarkusAugmentor {
private final Collection<Path> excludedFromIndexing;
private final LiveReloadBuildItem liveReloadBuildItem;
private final Properties buildSystemProperties;
private final Properties runtimeProperties;
private final Path targetDir;
private final ApplicationModel effectiveModel;
private final Supplier<DependencyInfoProvider> depInfoProvider;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,25 +380,25 @@ public List<ConfigClassWithPrefix> 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<String, String> platformProperties) {
// now prepare & load the build configuration
SmallRyeConfigBuilder builder = ConfigUtils.configBuilder(false, launchMode);
if (classLoader != null) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void accept(CuratedApplication curatedApplication, Map<String, Object> re
.setTargetDir(quarkusBootstrap.getTargetDirectory())
.setDeploymentClassLoader(curatedApplication.createDeploymentClassLoader())
.setBuildSystemProperties(quarkusBootstrap.getBuildSystemProperties())
.setRuntimeProperties(quarkusBootstrap.getRuntimeProperties())
.setEffectiveModel(curatedApplication.getApplicationModel());
if (quarkusBootstrap.getBaseName() != null) {
builder.setBaseName(quarkusBootstrap.getBaseName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,6 +47,7 @@ public static Map<String, Object> 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)) {
Expand All @@ -54,7 +56,7 @@ public static Map<String, Object> 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));
}
}

Expand Down Expand Up @@ -117,12 +119,15 @@ public Enumeration<URL> getResources(String name) throws IOException {
Thread.currentThread().setContextClassLoader(loader);
Class<?> launcher = loader.loadClass("io.quarkus.bootstrap.jbang.JBangBuilderImpl");
return (Map<String, Object>) 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class QuarkusBootstrap implements Serializable {
private final List<Path> excludeFromClassPath;

private final Properties buildSystemProperties;
private final Properties runtimeProperties;
private final String baseName;
private final String originalBaseName;
private final Path targetDirectory;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -202,6 +204,10 @@ public Properties getBuildSystemProperties() {
return buildSystemProperties;
}

public Properties getRuntimeProperties() {
return runtimeProperties;
}

public Path getProjectRoot() {
return projectRoot;
}
Expand Down Expand Up @@ -266,6 +272,7 @@ public Builder clonedBuilder() {
.setProjectRoot(projectRoot)
.setBaseClassLoader(baseClassLoader)
.setBuildSystemProperties(buildSystemProperties)
.setRuntimeProperties(runtimeProperties)
.setMode(mode)
.setTest(test)
.setLocalProjectDiscovery(localProjectDiscovery)
Expand Down Expand Up @@ -316,6 +323,7 @@ public static class Builder {
final List<Path> additionalDeploymentArchives = new ArrayList<>();
final List<Path> excludeFromClassPath = new ArrayList<>();
Properties buildSystemProperties;
Properties runtimeProperties;
Mode mode = Mode.PROD;
Boolean offline;
boolean test;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +27,12 @@
import io.quarkus.maven.dependency.ResolvedArtifactDependency;

public class JBangBuilderImpl {
public static Map<String, Object> postBuild(Path appClasses, Path pomFile, List<Map.Entry<String, String>> repositories,
public static Map<String, Object> postBuild(
Path appClasses,
Path pomFile,
List<Map.Entry<String, String>> repositories,
List<Map.Entry<String, Path>> dependencies,
Properties configurationProperties,
boolean nativeImage) {
final MavenArtifactResolver quarkusResolver;
try {
Expand Down Expand Up @@ -80,6 +85,8 @@ public static Map<String, Object> postBuild(Path appClasses, Path pomFile, List<
}).collect(Collectors.toList()))
.setAppArtifact(appArtifact)
.setIsolateDeployment(true)
.setBuildSystemProperties(configurationProperties)
.setRuntimeProperties(configurationProperties)
.setMode(QuarkusBootstrap.Mode.PROD);

CuratedApplication app = builder
Expand Down
Loading