diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildDependencies.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildDependencies.java index 10d841997acc6..b8e15b43a2e37 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildDependencies.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildDependencies.java @@ -5,6 +5,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -33,6 +34,8 @@ public abstract class QuarkusBuildDependencies extends QuarkusBuildTask { static final String CLASS_LOADING_REMOVED_ARTIFACTS = "quarkus.class-loading.removed-artifacts"; static final String CLASS_LOADING_PARENT_FIRST_ARTIFACTS = "quarkus.class-loading.parent-first-artifacts"; + static final String FILTER_OPTIONAL_DEPENDENCIES = "quarkus.package.filter-optional-dependencies"; + static final String INCLUDED_OPTIONAL_DEPENDENCIES = "quarkus.package.included-optional-dependencies"; @Inject public QuarkusBuildDependencies() { @@ -138,19 +141,22 @@ private void jarDependencies(Path libBoot, Path libMain) { Map configMap = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).configMap(); // see https://quarkus.io/guides/class-loading-reference#configuring-class-loading - String removedArtifactsProp = configMap.getOrDefault(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, ""); - java.util.Optional> optionalDependencies = java.util.Optional.ofNullable( + Set removedArtifacts = java.util.Optional.ofNullable( configMap.getOrDefault(CLASS_LOADING_REMOVED_ARTIFACTS, null)) - .map(s -> Arrays.stream(s.split(",")) - .map(String::trim) - .filter(gact -> !gact.isEmpty()) - .map(ArtifactKey::fromString) - .collect(Collectors.toSet())); - Set removedArtifacts = Arrays.stream(removedArtifactsProp.split(",")) - .map(String::trim) - .filter(gact -> !gact.isEmpty()) - .map(ArtifactKey::fromString) - .collect(Collectors.toSet()); + .map(QuarkusBuildDependencies::dependenciesListToArtifactKeySet) + .orElse(Collections.emptySet()); + getLogger().info("Removed artifacts: {}", configMap.getOrDefault(CLASS_LOADING_REMOVED_ARTIFACTS, "(none)")); + + String parentFirstArtifactsProp = configMap.getOrDefault(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, ""); + Set parentFirstArtifacts = dependenciesListToArtifactKeySet(parentFirstArtifactsProp); + getLogger().info("parent first artifacts: {}", configMap.getOrDefault(CLASS_LOADING_PARENT_FIRST_ARTIFACTS, "(none)")); + + String optionalDependenciesProp = configMap.getOrDefault(INCLUDED_OPTIONAL_DEPENDENCIES, ""); + boolean filterOptionalDependencies = Boolean + .parseBoolean(configMap.getOrDefault(FILTER_OPTIONAL_DEPENDENCIES, "false")); + Set optionalDependencies = filterOptionalDependencies + ? dependenciesListToArtifactKeySet(optionalDependenciesProp) + : Collections.emptySet(); appModel.getRuntimeDependencies().stream() .filter(appDep -> { @@ -158,13 +164,13 @@ private void jarDependencies(Path libBoot, Path libMain) { if (!appDep.isJar()) { return false; } - if (appDep.isOptional()) { - return optionalDependencies.map(appArtifactKeys -> appArtifactKeys.contains(appDep.getKey())) - .orElse(true); + if (filterOptionalDependencies && appDep.isOptional()) { + return optionalDependencies.contains(appDep.getKey()); } return !removedArtifacts.contains(appDep.getKey()); }) - .map(dep -> Map.entry(dep.isFlagSet(DependencyFlags.CLASSLOADER_RUNNER_PARENT_FIRST) ? libBoot : libMain, dep)) + .map(dep -> Map.entry(dep.isFlagSet(DependencyFlags.CLASSLOADER_RUNNER_PARENT_FIRST) + || parentFirstArtifacts.contains(dep.getKey()) ? libBoot : libMain, dep)) .peek(depAndTarget -> { ResolvedDependency dep = depAndTarget.getValue(); Path targetDir = depAndTarget.getKey(); @@ -186,4 +192,12 @@ private void jarDependencies(Path libBoot, Path libMain) { .collect(Collectors.toMap(Map.Entry::getKey, depAndTarget -> 1, Integer::sum)) .forEach((path, count) -> getLogger().info("Copied {} files into {}", count, path)); } + + private static Set dependenciesListToArtifactKeySet(String optionalDependenciesProp) { + return Arrays.stream(optionalDependenciesProp.split(",")) + .map(String::trim) + .filter(gact -> !gact.isEmpty()) + .map(ArtifactKey::fromString) + .collect(Collectors.toSet()); + } }