Skip to content

Commit

Permalink
Gradle plugin: optionally respect non-quarkus.* cache-relevant prop…
Browse files Browse the repository at this point in the history
…erties

Currently the Gradle plugin considers only system properties starting with `quarkus.` (and env vars starting with `QUARKUS_`) as relevant for Gradle's cache key. This is on purpose to not cause unnecessary rebuilds, because especially system properties can contain entries that are rather "random-ish". But other environment variables or system properties cannot be specified as "relevant".

This change introduces a new list-property `cachingRelevantProperties` on the Quarkus Gradle extension object to tell the Quarkus Gradle plugin to consider the specified properties/env-vars as input(s) for the tasks. This list-property accepts regex patterns, and by default contains `quarkus[.].*`, matching the current `.startsWith("quarkus.")`.

Fixes #34869
  • Loading branch information
snazy committed Jul 26, 2023
1 parent 0e89470 commit 44b76fa
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
Expand Down Expand Up @@ -254,6 +255,10 @@ public MapProperty<String, String> getQuarkusBuildProperties() {
return quarkusBuildProperties;
}

public ListProperty<String> getCachingRelevantProperties() {
return cachingRelevantProperties;
}

public void set(String name, @Nullable String value) {
quarkusBuildProperties.put(String.format("quarkus.%s", name), value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class AbstractQuarkusExtension {
protected final Property<String> finalName;
private final MapProperty<String, String> forcedPropertiesProperty;
protected final MapProperty<String, String> quarkusBuildProperties;
protected final ListProperty<String> cachingRelevantProperties;
private final ListProperty<String> ignoredEntries;
private final FileCollection classpath;
private final Property<BaseConfig> baseConfig;
Expand All @@ -52,6 +53,7 @@ protected AbstractQuarkusExtension(Project project) {
this.finalName.convention(project.provider(() -> String.format("%s-%s", project.getName(), project.getVersion())));
this.forcedPropertiesProperty = project.getObjects().mapProperty(String.class, String.class);
this.quarkusBuildProperties = project.getObjects().mapProperty(String.class, String.class);
this.cachingRelevantProperties = project.getObjects().listProperty(String.class).value(List.of("quarkus[.].*"));
this.ignoredEntries = project.getObjects().listProperty(String.class);
this.ignoredEntries.convention(
project.provider(() -> baseConfig().packageConfig().userConfiguredIgnoredEntries.orElse(emptyList())));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.quarkus.gradle.tasks;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.quarkus.deployment.pkg.PackageConfig;
Expand All @@ -18,7 +21,7 @@
final class BaseConfig {
private final Manifest manifest;
private final PackageConfig packageConfig;
private final Map<String, String> quarkusProperties;
private final Map<String, String> configMap;

// Note: EffectiveConfig has all the code to load the configurations from all the sources.
BaseConfig(EffectiveConfig config) {
Expand All @@ -31,8 +34,7 @@ final class BaseConfig {
manifest.attributes(packageConfig.manifest.attributes);
packageConfig.manifest.manifestSections.forEach((section, attribs) -> manifest.attributes(attribs, section));

this.quarkusProperties = config.configMap().entrySet().stream().filter(e -> e.getKey().startsWith("quarkus."))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
configMap = config.configMap();
}

PackageConfig packageConfig() {
Expand All @@ -47,7 +49,12 @@ Manifest manifest() {
return manifest;
}

Map<String, String> quarkusProperties() {
return quarkusProperties;
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()
.filter(keyPredicate)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ static Map<String, String> generateFullConfigMap(SmallRyeConfig config) {
static SmallRyeConfig buildConfig(String profile, List<ConfigSource> configSources) {
return ConfigUtils.emptyConfigBuilder()
.setAddDiscoveredSecretKeysHandlers(false)
// We add our own sources for environment, system-properties and microprofile-config.properties,
// no need to include those twice.
.setAddDefaultSources(false)
.withSources(configSources)
.withProfile(profile)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.StopExecutionException;
Expand Down Expand Up @@ -57,7 +58,8 @@ public FileCollection getClasspath() {

@Input
public Map<String, String> getCachingRelevantInput() {
return extension().baseConfig().quarkusProperties();
ListProperty<String> vars = extension().getCachingRelevantProperties();
return extension().baseConfig().cachingRelevantProperties(vars.get());
}

PackageConfig.BuiltInType packageType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.CompileClasspath;
import org.gradle.api.tasks.Input;
Expand Down Expand Up @@ -65,7 +66,17 @@ public void setCompileClasspath(Configuration compileClasspath) {

@Input
public Map<String, String> getCachingRelevantInput() {
return extension().baseConfig().quarkusProperties();
ListProperty<String> vars = extension().getCachingRelevantProperties();
return extension().baseConfig().cachingRelevantProperties(vars.get());
}

@Input
Map<String, String> getInternalTaskConfig() {
// Necessary to distinguish the different `quarkusGenerateCode*` tasks, because the task path is _not_
// an input to the cache key. We need to declare these properties as inputs, because those influence the
// execution.
// Documented here: https://docs.gradle.org/current/userguide/build_cache.html#sec:task_output_caching_inputs
return Map.of("launchMode", launchMode.name(), "inputSourceSetName", inputSourceSetName);
}

@InputFiles
Expand Down
Loading

0 comments on commit 44b76fa

Please sign in to comment.