Skip to content

Commit 0aa6b50

Browse files
committed
Add support for dependency level exclusions
1 parent 5902456 commit 0aa6b50

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/dependency/ApplicationDeploymentClasspathBuilder.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.gradle.api.artifacts.Configuration;
1717
import org.gradle.api.artifacts.ConfigurationContainer;
1818
import org.gradle.api.artifacts.Dependency;
19+
import org.gradle.api.artifacts.ExcludeRule;
1920
import org.gradle.api.artifacts.ModuleDependency;
2021
import org.gradle.api.artifacts.ModuleIdentifier;
2122
import org.gradle.api.artifacts.ModuleVersionIdentifier;
@@ -180,7 +181,7 @@ private static Configuration[] getOriginalRuntimeClasspaths(Project project, Lau
180181
private final String platformImportName;
181182

182183
private final List<Dependency> platformDataDeps = new ArrayList<>();
183-
private final Set<ManualPlatformSpec.Constraint> platformConstraints = new HashSet<>();
184+
private final Set<PlatformSpec.Constraint> platformConstraints = new HashSet<>();
184185

185186
public ApplicationDeploymentClasspathBuilder(Project project, LaunchMode mode,
186187
TaskDependencyFactory taskDependencyFactory) {
@@ -243,8 +244,12 @@ private void setUpPlatformConfiguration() {
243244
}
244245
}
245246
} else {
246-
platformConstraints.add(new ManualPlatformSpec.Constraint(d.getTarget().getGroup(), name,
247-
d.getTarget().getVersion()));
247+
Set<ExcludeRule> depExcludeRules = new HashSet<>();
248+
if (d instanceof ModuleDependency md) {
249+
depExcludeRules.addAll(md.getExcludeRules());
250+
}
251+
platformConstraints.add(new PlatformSpec.Constraint(d.getTarget().getGroup(), name,
252+
d.getTarget().getVersion(), depExcludeRules));
248253
}
249254
});
250255
});
@@ -282,9 +287,9 @@ private List<Dependency> resolvePlatformDependencies() {
282287
return platformDataDeps;
283288
}
284289

285-
private ManualPlatformSpec resolvePlatformSpec() {
290+
private PlatformSpec resolvePlatformSpec() {
286291
getPlatformConfiguration().resolve();
287-
return new ManualPlatformSpec(platformConstraints, getPlatformConfiguration().getExcludeRules());
292+
return new PlatformSpec(platformConstraints, getPlatformConfiguration().getExcludeRules());
288293
}
289294

290295
private void setUpRuntimeConfiguration() {
@@ -294,8 +299,8 @@ private void setUpRuntimeConfiguration() {
294299
if (disableComponentVariants) {
295300
baseConfig = ApplicationDeploymentClasspathBuilder.getBaseRuntimeConfigName(mode);
296301
} else {
297-
Property<ManualPlatformSpec> platformSpecProperty = project.getObjects()
298-
.property(ManualPlatformSpec.class);
302+
Property<PlatformSpec> platformSpecProperty = project.getObjects()
303+
.property(PlatformSpec.class);
299304
QuarkusComponentVariants.addVariants(project, mode,
300305
platformSpecProperty.value(project.provider(this::resolvePlatformSpec)));
301306
baseConfig = QuarkusComponentVariants.getConditionalConfigurationName(mode);
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import org.gradle.api.artifacts.Dependency;
66
import org.gradle.api.artifacts.ExcludeRule;
77

8-
class ManualPlatformSpec {
8+
class PlatformSpec {
99
private final Set<Constraint> constraints;
1010
private final Set<ExcludeRule> exclusions;
1111

12-
public ManualPlatformSpec(Set<Constraint> constraints, Set<ExcludeRule> exclusions) {
12+
public PlatformSpec(Set<Constraint> constraints, Set<ExcludeRule> exclusions) {
1313
this.constraints = constraints;
1414
this.exclusions = exclusions;
1515
}
@@ -26,11 +26,13 @@ static class Constraint {
2626
private final String groupId;
2727
private final String artifactId;
2828
private final String version;
29+
private final Set<ExcludeRule> exclusions;
2930

30-
public Constraint(String groupId, String artifactId, String version) {
31+
public Constraint(String groupId, String artifactId, String version, Set<ExcludeRule> exclusions) {
3132
this.groupId = groupId;
3233
this.artifactId = artifactId;
3334
this.version = version;
35+
this.exclusions = exclusions;
3436
}
3537

3638
public String getGroupId() {
@@ -45,6 +47,10 @@ public String getVersion() {
4547
return version;
4648
}
4749

50+
public Set<ExcludeRule> getExclusions() {
51+
return exclusions;
52+
}
53+
4854
public boolean matches(Dependency dependency) {
4955
return groupId.equals(dependency.getGroup())
5056
&& artifactId.equals(dependency.getName());

devtools/gradle/gradle-model/src/main/java/io/quarkus/gradle/dependency/QuarkusComponentVariants.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.HashSet;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.Optional;
1112
import java.util.Set;
1213
import java.util.concurrent.atomic.AtomicInteger;
1314

@@ -171,13 +172,13 @@ public static void setCommonAttributes(AttributeContainer attrs, ObjectFactory o
171172
* @param mode launch mode
172173
*/
173174
public static void addVariants(Project project, LaunchMode mode,
174-
Property<ManualPlatformSpec> manualPlatformConfig) {
175-
new QuarkusComponentVariants(project, mode, manualPlatformConfig).configureAndAddVariants();
175+
Property<PlatformSpec> platformSpecProperty) {
176+
new QuarkusComponentVariants(project, mode, platformSpecProperty).configureAndAddVariants();
176177
}
177178

178179
private final Attribute<String> quarkusDepAttr;
179180
private final Project project;
180-
private final Property<ManualPlatformSpec> manualPlatformConfig;
181+
private final Property<PlatformSpec> platformSpecProperty;
181182
private final Map<ArtifactKey, ProcessedDependency> processedDeps = new HashMap<>();
182183
private final Map<ArtifactKey, ConditionalDependency> allConditionalDeps = new HashMap<>();
183184
private final List<ConditionalDependencyVariant> dependencyVariantQueue = new ArrayList<>();
@@ -186,10 +187,10 @@ public static void addVariants(Project project, LaunchMode mode,
186187
private final AtomicInteger configCopyCounter = new AtomicInteger();
187188

188189
private QuarkusComponentVariants(Project project, LaunchMode mode,
189-
Property<ManualPlatformSpec> manualPlatformConfig) {
190+
Property<PlatformSpec> platformSpecProperty) {
190191
this.project = project;
191192
this.mode = mode;
192-
this.manualPlatformConfig = manualPlatformConfig;
193+
this.platformSpecProperty = platformSpecProperty;
193194
this.quarkusDepAttr = getConditionalDependencyAttribute(project.getName(), mode);
194195
project.getDependencies().getAttributesSchema().attribute(quarkusDepAttr);
195196
project.getDependencies().getAttributesSchema().attribute(getDeploymentDependencyAttribute(project.getName(), mode));
@@ -456,8 +457,8 @@ private ResolvedArtifact tryResolvingRelocationArtifact(Dependency dep) {
456457
}
457458

458459
private ConditionalDependency newConditionalDep(Dependency originalDep) {
459-
var manualConfig = manualPlatformConfig.get();
460-
var dep = getConstrainedDep(originalDep, manualConfig.getConstraints());
460+
// var platformSpec = platformSpecProperty.get();
461+
var dep = getConstrainedDep(originalDep);
461462
final Configuration config = getDetachedWithExclusions(dep).setTransitive(false);
462463

463464
setConditionalAttributes(config, project, mode);
@@ -491,7 +492,7 @@ private ConditionalDependency newConditionalDep(Dependency originalDep) {
491492
}
492493

493494
private boolean isExplicitlyExcluded(Dependency dep) {
494-
return manualPlatformConfig.get().getExclusions().stream().anyMatch(rule -> {
495+
return platformSpecProperty.get().getExclusions().stream().anyMatch(rule -> {
495496
rule.getGroup();
496497
if (!rule.getGroup().equals(dep.getGroup())) {
497498
return false;
@@ -503,33 +504,46 @@ private boolean isExplicitlyExcluded(Dependency dep) {
503504

504505
private Configuration getDetachedWithExclusions(Dependency dep) {
505506
var c = project.getConfigurations().detachedConfiguration(dep);
506-
manualPlatformConfig.get().getExclusions().forEach(rule -> {
507+
PlatformSpec platformSpec = platformSpecProperty.get();
508+
platformSpec.getExclusions().forEach(rule -> {
507509
Map<String, String> excludeProperties = new HashMap<>();
508510
excludeProperties.put("group", rule.getGroup());
509511
excludeProperties.put("module", rule.getModule());
510512
c.exclude(excludeProperties);
511513
});
514+
// Also apply exclusions from matching constraints
515+
findMatchingConstraint(dep).ifPresent(constraint -> constraint.getExclusions().forEach(rule -> {
516+
Map<String, String> excludeProperties = new HashMap<>();
517+
excludeProperties.put("group", rule.getGroup());
518+
excludeProperties.put("module", rule.getModule());
519+
c.exclude(excludeProperties);
520+
}));
512521
return c;
513522
}
514523

515-
private Dependency getConstrainedDep(Dependency dep, Set<ManualPlatformSpec.Constraint> constraints) {
524+
private Dependency getConstrainedDep(Dependency dep) {
525+
return findMatchingConstraint(dep).map(c -> project.getDependencies().create(
526+
dep.getGroup() + ":" + dep.getName() + ":" + c.getVersion())).orElse(dep);
527+
}
528+
529+
private Optional<PlatformSpec.Constraint> findMatchingConstraint(Dependency dep) {
530+
PlatformSpec platformSpec = platformSpecProperty.get();
531+
Set<PlatformSpec.Constraint> constraints = platformSpec.getConstraints();
516532
if (constraints == null || constraints.isEmpty()) {
517-
return dep;
533+
return Optional.empty();
518534
}
519535

520536
var matchingConstraints = constraints.stream().filter(c -> c.matches(dep)).toList();
521537

522538
if (matchingConstraints.isEmpty()) {
523-
return dep;
539+
return Optional.empty();
524540
}
525541

526542
if (matchingConstraints.size() > 1) {
527543
throw new RuntimeException("Multiple matching constraints for " + dep + ": " + matchingConstraints);
528544
}
529545

530-
var c = matchingConstraints.get(0);
531-
return project.getDependencies().create(
532-
dep.getGroup() + ":" + dep.getName() + ":" + c.getVersion());
546+
return Optional.of(matchingConstraints.get(0));
533547
}
534548

535549
private class ProcessedDependency {

0 commit comments

Comments
 (0)