Skip to content

Commit

Permalink
Merge pull request #38988 from radcortez/fix-37984
Browse files Browse the repository at this point in the history
Do not expand configuration for Gradle cache
  • Loading branch information
radcortez authored Mar 1, 2024
2 parents 7481022 + 11bd532 commit efd8745
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void beforeTest(Test task) {
Map<String, Object> props = task.getSystemProperties();
ApplicationModel appModel = getApplicationModel(TEST);

SmallRyeConfig config = buildEffectiveConfiguration(appModel.getAppArtifact()).config();
SmallRyeConfig config = buildEffectiveConfiguration(appModel.getAppArtifact()).getConfig();
config.getOptionalValue(TEST.getProfileKey(), String.class)
.ifPresent(value -> props.put(TEST.getProfileKey(), value));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.quarkus.gradle.tasks;

import static io.quarkus.gradle.tasks.QuarkusGradleUtils.getSourceSet;
import static io.smallrye.common.expression.Expression.Flag.DOUBLE_COLON;
import static io.smallrye.common.expression.Expression.Flag.LENIENT_SYNTAX;
import static io.smallrye.common.expression.Expression.Flag.NO_SMART_BRACES;
import static io.smallrye.common.expression.Expression.Flag.NO_TRIM;
import static java.util.Collections.emptyList;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -24,6 +29,7 @@

import io.quarkus.gradle.dsl.Manifest;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.smallrye.common.expression.Expression;

/**
* This base class exists to hide internal properties, make those only available in the {@link io.quarkus.gradle.tasks}
Expand Down Expand Up @@ -138,7 +144,7 @@ private EffectiveConfig buildEffectiveConfiguration(Map<String, Object> properti
* @param appArtifact the application dependency to retrive the quarkus application name and version.
* @return a filtered view of the configuration only with <code>quarkus.</code> names.
*/
protected Map<String, String> buildSystemProperties(ResolvedDependency appArtifact) {
protected Map<String, String> buildSystemProperties(ResolvedDependency appArtifact, Map<String, String> quarkusProperties) {
Map<String, String> buildSystemProperties = new HashMap<>();
buildSystemProperties.putIfAbsent("quarkus.application.name", appArtifact.getArtifactId());
buildSystemProperties.putIfAbsent("quarkus.application.version", appArtifact.getVersion());
Expand All @@ -158,6 +164,33 @@ protected Map<String, String> buildSystemProperties(ResolvedDependency appArtifa
buildSystemProperties.put(entry.getKey(), entry.getValue().toString());
}
}

Set<String> quarkusValues = new HashSet<>();
quarkusValues.addAll(quarkusProperties.values());
quarkusValues.addAll(buildSystemProperties.values());

for (String value : quarkusValues) {
Expression expression = Expression.compile(value, LENIENT_SYNTAX, NO_TRIM, NO_SMART_BRACES, DOUBLE_COLON);
for (String reference : expression.getReferencedStrings()) {
String expanded = forcedPropertiesProperty.get().get(reference);
if (expanded != null) {
buildSystemProperties.put(reference, expanded);
continue;
}

expanded = quarkusBuildProperties.get().get(reference);
if (expanded != null) {
buildSystemProperties.put(reference, expanded);
continue;
}

expanded = (String) project.getProperties().get(reference);
if (expanded != null) {
buildSystemProperties.put(reference, expanded);
}
}
}

return buildSystemProperties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
final class BaseConfig {
private final Manifest manifest;
private final PackageConfig packageConfig;
private final Map<String, String> configMap;
private final Map<String, String> values;

// Note: EffectiveConfig has all the code to load the configurations from all the sources.
BaseConfig(EffectiveConfig config) {
manifest = new Manifest();
packageConfig = new PackageConfig();

ConfigInstantiator.handleObject(packageConfig, config.config());
ConfigInstantiator.handleObject(packageConfig, config.getConfig());

// populate the Gradle Manifest object
manifest.attributes(packageConfig.manifest.attributes);
packageConfig.manifest.manifestSections.forEach((section, attribs) -> manifest.attributes(attribs, section));

configMap = config.configMap();
values = config.getValues();
}

PackageConfig packageConfig() {
Expand All @@ -53,7 +53,7 @@ Map<String, String> cachingRelevantProperties(List<String> propertyPatterns) {
List<Pattern> patterns = propertyPatterns.stream().map(s -> "^(" + s + ")$").map(Pattern::compile)
.collect(Collectors.toList());
Predicate<Map.Entry<String, ?>> keyPredicate = e -> patterns.stream().anyMatch(p -> p.matcher(e.getKey()).matches());
return configMap.entrySet().stream()
return values.entrySet().stream()
.filter(keyPredicate)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ public Deploy() {
@TaskAction
public void checkRequiredExtensions() {
ApplicationModel appModel = resolveAppModelForBuild();
Map<String, String> configMap = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).configMap();
Properties sysProps = new Properties();
sysProps.putAll(configMap);
sysProps.putAll(extension().buildEffectiveConfiguration(appModel.getAppArtifact()).getValues());
try (CuratedApplication curatedApplication = QuarkusBootstrap.builder()
.setBaseClassLoader(getClass().getClassLoader())
.setExistingModel(appModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
Expand All @@ -26,6 +27,7 @@
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.AbstractLocationConfigSourceLoader;
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.Expressions;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.PropertiesConfigSourceProvider;
import io.smallrye.config.SmallRyeConfig;
Expand All @@ -42,9 +44,8 @@
* the Quarkus config objects like {@link PackageConfig}, {@link ClassLoadingConfig} and the underlying {@link SmallRyeConfig}.
*/
public final class EffectiveConfig {
private final Map<String, String> fullConfig;

private final SmallRyeConfig config;
private final Map<String, String> values;

private EffectiveConfig(Builder builder) {
List<ConfigSource> configSources = new ArrayList<>();
Expand Down Expand Up @@ -81,13 +82,17 @@ private EffectiveConfig(Builder builder) {
.addAll(PropertiesConfigSourceProvider.classPathSources(META_INF_MICROPROFILE_CONFIG_PROPERTIES, classLoader));

this.config = buildConfig(builder.profile, configSources);
this.fullConfig = generateFullConfigMap(config);
this.values = generateFullConfigMap(config);
}

public SmallRyeConfig config() {
public SmallRyeConfig getConfig() {
return config;
}

public Map<String, String> getValues() {
return values;
}

private Map<String, String> asStringMap(Map<String, ?> map) {
Map<String, String> target = new HashMap<>();
map.forEach((k, v) -> {
Expand All @@ -100,14 +105,19 @@ private Map<String, String> asStringMap(Map<String, ?> map) {

@VisibleForTesting
static Map<String, String> generateFullConfigMap(SmallRyeConfig config) {
Map<String, String> map = new HashMap<>();
config.getPropertyNames().forEach(property -> {
String v = config.getConfigValue(property).getValue();
if (v != null) {
map.put(property, v);
return Expressions.withoutExpansion(new Supplier<Map<String, String>>() {
@Override
public Map<String, String> get() {
Map<String, String> properties = new HashMap<>();
for (String propertyName : config.getPropertyNames()) {
String value = config.getRawValue(propertyName);
if (value != null) {
properties.put(propertyName, value);
}
}
return unmodifiableMap(properties);
}
});
return unmodifiableMap(map);
}

@VisibleForTesting
Expand All @@ -126,10 +136,6 @@ static Builder builder() {
return new Builder();
}

public Map<String, String> configMap() {
return fullConfig;
}

static final class Builder {
private Map<String, String> buildProperties = emptyMap();
private Map<String, ?> projectProperties = emptyMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.DependencyFlags;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.smallrye.config.SmallRyeConfig;

/**
* Collect the Quarkus app dependencies, the contents of the {@code quarkus-app/lib} folder, without making the task
Expand Down Expand Up @@ -139,22 +140,23 @@ private void jarDependencies(Path libBoot, Path libMain) {
}

ApplicationModel appModel = resolveAppModelForBuild();
Map<String, String> configMap = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).configMap();
SmallRyeConfig config = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).getConfig();

// see https://quarkus.io/guides/class-loading-reference#configuring-class-loading
Set<ArtifactKey> removedArtifacts = java.util.Optional.ofNullable(
configMap.getOrDefault(CLASS_LOADING_REMOVED_ARTIFACTS, null))
Set<ArtifactKey> removedArtifacts = config.getOptionalValue(CLASS_LOADING_REMOVED_ARTIFACTS, String.class)
.map(QuarkusBuildDependencies::dependenciesListToArtifactKeySet)
.orElse(Collections.emptySet());
getLogger().info("Removed artifacts: {}", configMap.getOrDefault(CLASS_LOADING_REMOVED_ARTIFACTS, "(none)"));
getLogger().info("Removed artifacts: {}",
config.getOptionalValue(CLASS_LOADING_REMOVED_ARTIFACTS, String.class).orElse("(none)"));

String parentFirstArtifactsProp = configMap.getOrDefault(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, "");
String parentFirstArtifactsProp = config.getOptionalValue(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, String.class)
.orElse("");
Set<ArtifactKey> parentFirstArtifacts = dependenciesListToArtifactKeySet(parentFirstArtifactsProp);
getLogger().info("parent first artifacts: {}", configMap.getOrDefault(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, "(none)"));
getLogger().info("parent first artifacts: {}",
config.getOptionalValue(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, String.class).orElse("(none)"));

String optionalDependenciesProp = configMap.getOrDefault(INCLUDED_OPTIONAL_DEPENDENCIES, "");
boolean filterOptionalDependencies = Boolean
.parseBoolean(configMap.getOrDefault(FILTER_OPTIONAL_DEPENDENCIES, "false"));
String optionalDependenciesProp = config.getOptionalValue(INCLUDED_OPTIONAL_DEPENDENCIES, String.class).orElse("");
boolean filterOptionalDependencies = config.getOptionalValue(FILTER_OPTIONAL_DEPENDENCIES, Boolean.class).orElse(false);
Set<ArtifactKey> optionalDependencies = filterOptionalDependencies
? dependenciesListToArtifactKeySet(optionalDependenciesProp)
: Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.quarkus.gradle.tasks.worker.BuildWorker;
import io.quarkus.maven.dependency.GACTV;
import io.smallrye.config.Expressions;
import io.smallrye.config.SmallRyeConfig;

/**
* Base class for the {@link QuarkusBuildDependencies}, {@link QuarkusBuildCacheableAppParts}, {@link QuarkusBuild} tasks
Expand Down Expand Up @@ -207,30 +208,29 @@ void generateBuild() {
});

ApplicationModel appModel = resolveAppModelForBuild();
Map<String, String> configMap = new HashMap<>();
EffectiveConfig effectiveConfig = extension().buildEffectiveConfiguration(appModel.getAppArtifact());
Expressions.withoutExpansion(() -> {
for (Map.Entry<String, String> entry : effectiveConfig.configMap().entrySet()) {
if (entry.getKey().startsWith("quarkus.")) {
configMap.put(entry.getKey(), effectiveConfig.config().getRawValue(entry.getKey()));
}
}
SmallRyeConfig config = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).getConfig();
Map<String, String> quarkusProperties = Expressions.withoutExpansion(() -> {
Map<String, String> values = new HashMap<>();
config.getValues("quarkus", String.class, String.class)
.forEach((key, value) -> values.put("quarkus." + key, value));
return values;
});

getLogger().info("Starting Quarkus application build for package type {}", packageType);

if (getLogger().isEnabled(LogLevel.INFO)) {
getLogger().info("Effective properties: {}",
configMap.entrySet().stream()
quarkusProperties.entrySet().stream()
.map(Object::toString)
.sorted()
.collect(Collectors.joining("\n ", "\n ", "")));
}

WorkQueue workQueue = workQueue(configMap, () -> extension().buildForkOptions);
WorkQueue workQueue = workQueue(quarkusProperties, () -> extension().buildForkOptions);

workQueue.submit(BuildWorker.class, params -> {
params.getBuildSystemProperties().putAll(extension().buildSystemProperties(appModel.getAppArtifact()));
params.getBuildSystemProperties()
.putAll(extension().buildSystemProperties(appModel.getAppArtifact(), quarkusProperties));
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 @@ -102,17 +102,17 @@ public Set<File> getInputDirectory() {
@TaskAction
public void generateCode() {
ApplicationModel appModel = extension().getApplicationModel(launchMode);
Map<String, String> configMap = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).configMap();
Map<String, String> values = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).getValues();

File outputPath = getGeneratedOutputDirectory().get().getAsFile();

getLogger().debug("Will trigger preparing sources for source directories: {} buildDir: {}",
sourcesDirectories, buildDir.getAbsolutePath());

WorkQueue workQueue = workQueue(configMap, () -> extension().codeGenForkOptions);
WorkQueue workQueue = workQueue(values, () -> extension().codeGenForkOptions);

workQueue.submit(CodeGenWorker.class, params -> {
params.getBuildSystemProperties().putAll(configMap);
params.getBuildSystemProperties().putAll(values);
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 @@ -100,9 +100,8 @@ public void setJvmArgs(List<String> jvmArgs) {
@TaskAction
public void runQuarkus() {
ApplicationModel appModel = resolveAppModelForBuild();
Map<String, String> configMap = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).configMap();
Properties sysProps = new Properties();
sysProps.putAll(configMap);
sysProps.putAll(extension().buildEffectiveConfiguration(appModel.getAppArtifact()).getValues());
try (CuratedApplication curatedApplication = QuarkusBootstrap.builder()
.setBaseClassLoader(getClass().getClassLoader())
.setExistingModel(appModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

Expand All @@ -22,6 +21,7 @@
import org.gradle.api.tasks.options.Option;

import io.quarkus.gradle.QuarkusPlugin;
import io.smallrye.config.SmallRyeConfig;

/**
* Just show the effective configuration and settings.
Expand All @@ -45,21 +45,21 @@ public Property<Boolean> getSaveConfigProperties() {
@TaskAction
public void dumpEffectiveConfiguration() {
try {
EffectiveConfig effective = extension()
EffectiveConfig effectiveConfig = extension()
.buildEffectiveConfiguration(extension().getApplicationModel().getAppArtifact());
Map<String, String> configMap = effective.configMap();
SmallRyeConfig config = effectiveConfig.getConfig();
List<String> sourceNames = new ArrayList<>();
effective.config().getConfigSources().forEach(configSource -> sourceNames.add(configSource.getName()));
config.getConfigSources().forEach(configSource -> sourceNames.add(configSource.getName()));

String config = configMap.entrySet().stream()
.filter(e -> e.getKey().startsWith("quarkus."))
.map(e -> format("%s=%s", e.getKey(), e.getValue())).sorted()
String quarkusConfig = config.getValues("quarkus", String.class, String.class)
.entrySet()
.stream()
.map(e -> format("quarkus.%s=%s", e.getKey(), e.getValue())).sorted()
.collect(Collectors.joining("\n ", "\n ", "\n"));

getLogger().lifecycle("Effective Quarkus configuration options: {}", config);
getLogger().lifecycle("Effective Quarkus configuration options: {}", quarkusConfig);

String finalName = extension().finalName();
String packageType = configMap.getOrDefault(QuarkusPlugin.QUARKUS_PACKAGE_TYPE, "fast-jar");
String packageType = config.getOptionalValue(QuarkusPlugin.QUARKUS_PACKAGE_TYPE, String.class).orElse("fast-jar");
File fastJar = fastJar();
getLogger().lifecycle(
"Quarkus package type: {}\n" +
Expand All @@ -79,7 +79,7 @@ public void dumpEffectiveConfiguration() {

if (getSaveConfigProperties().get()) {
Properties props = new Properties();
props.putAll(configMap);
props.putAll(effectiveConfig.getValues());
Path file = buildDir.toPath().resolve(finalName + ".quarkus-build.properties");
try (BufferedWriter writer = newBufferedWriter(file)) {
props.store(writer, format("Quarkus build properties with package type %s", packageType));
Expand Down
Loading

0 comments on commit efd8745

Please sign in to comment.