Skip to content

Commit 09f93c8

Browse files
committed
WIP
1 parent 11e0453 commit 09f93c8

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

src/main/java/org/gradlex/jvm/dependency/conflict/resolution/rules/ReduceToCompileOnlyApiDependencyMetadataRule.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,16 @@
1616

1717
package org.gradlex.jvm.dependency.conflict.resolution.rules;
1818

19-
import org.gradle.api.Action;
2019
import org.gradle.api.artifacts.CacheableRule;
2120
import org.gradle.api.artifacts.ComponentMetadataContext;
22-
import org.gradle.api.artifacts.ComponentMetadataDetails;
2321
import org.gradle.api.artifacts.ComponentMetadataRule;
24-
import org.gradle.api.artifacts.VariantMetadata;
25-
import org.gradle.api.attributes.Attribute;
26-
import org.gradle.api.attributes.Category;
2722
import org.gradle.api.attributes.Usage;
2823

2924
import javax.inject.Inject;
30-
import java.util.Objects;
3125
import java.util.stream.Collectors;
3226

27+
import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantUtils.withVariants;
28+
3329
/**
3430
* See:
3531
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
@@ -49,16 +45,4 @@ public ReduceToCompileOnlyApiDependencyMetadataRule(String dependency) {
4945
public void execute(ComponentMetadataContext context) {
5046
withVariants(context.getDetails(), Usage.JAVA_RUNTIME, v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList()))));
5147
}
52-
53-
private void withVariants(ComponentMetadataDetails details, String expectedUsage, Action<VariantMetadata> action) {
54-
details.allVariants(v -> {
55-
v.attributes(attributeContainer -> {
56-
String usage = attributeContainer.getAttributes().getAttribute(Attribute.of(Usage.USAGE_ATTRIBUTE.getName(), String.class));
57-
String category = attributeContainer.getAttributes().getAttribute(Attribute.of(Category.CATEGORY_ATTRIBUTE.getName(), String.class));
58-
if (Objects.equals(usage, expectedUsage) && Objects.equals(category, Category.LIBRARY)) {
59-
action.execute(v);
60-
}
61-
});
62-
});
63-
}
6448
}

src/main/java/org/gradlex/jvm/dependency/conflict/resolution/rules/ReduceToRuntimeOnlyDependencyMetadataRule.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
import org.gradle.api.artifacts.CacheableRule;
2020
import org.gradle.api.artifacts.ComponentMetadataContext;
2121
import org.gradle.api.artifacts.ComponentMetadataRule;
22+
import org.gradle.api.attributes.Usage;
2223

2324
import javax.inject.Inject;
2425
import java.util.stream.Collectors;
2526

27+
import static org.gradlex.jvm.dependency.conflict.resolution.rules.VariantUtils.withVariants;
28+
2629
/**
2730
* See:
2831
* <a href="https://docs.gradle.org/current/userguide/component_metadata_rules.html#fixing_wrong_dependency_details">
2932
* component_metadata_rules.html#fixing_wrong_dependency_details</a>
3033
*/
31-
@CacheableRule
34+
//@CacheableRule
3235
public abstract class ReduceToRuntimeOnlyDependencyMetadataRule implements ComponentMetadataRule {
3336

3437
private final String dependency;
@@ -40,7 +43,11 @@ public ReduceToRuntimeOnlyDependencyMetadataRule(String dependency) {
4043

4144
@Override
4245
public void execute(ComponentMetadataContext context) {
43-
context.getDetails().withVariant("compile", v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .pom
44-
context.getDetails().withVariant("apiElements", v -> v.withDependencies(d -> d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList())))); // .module
46+
withVariants(context.getDetails(), Usage.JAVA_API,
47+
v -> {
48+
v.withDependencies(d -> {
49+
d.removeAll(d.stream().filter(it -> dependency.equals(it.getModule().toString())).collect(Collectors.toList()));
50+
});
51+
});
4552
}
4653
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.gradlex.jvm.dependency.conflict.resolution.rules;
2+
3+
import org.gradle.api.Action;
4+
import org.gradle.api.artifacts.ComponentMetadataDetails;
5+
import org.gradle.api.artifacts.VariantMetadata;
6+
import org.gradle.api.attributes.Attribute;
7+
import org.gradle.api.attributes.Category;
8+
import org.gradle.api.attributes.Usage;
9+
10+
import java.util.Objects;
11+
12+
final class VariantUtils {
13+
private VariantUtils() {}
14+
15+
static void withVariants(ComponentMetadataDetails details, String expectedUsage, Action<VariantMetadata> action) {
16+
details.allVariants(v -> {
17+
v.attributes(attributeContainer -> {
18+
String usage = attributeContainer.getAttributes().getAttribute(Attribute.of(Usage.USAGE_ATTRIBUTE.getName(), String.class));
19+
String category = attributeContainer.getAttributes().getAttribute(Attribute.of(Category.CATEGORY_ATTRIBUTE.getName(), String.class));
20+
if (Objects.equals(usage, expectedUsage) && Objects.equals(category, Category.LIBRARY)) {
21+
action.execute(v);
22+
}
23+
v.withDependencies(d -> System.out.println(d));
24+
});
25+
});
26+
}
27+
}

src/test/groovy/org/gradlex/jvm/dependency/conflict/test/fixture/GradleBuild.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ class GradleBuild {
5151
}
5252

5353
BuildResult dependenciesCompile() {
54-
runner('dependencies', '--configuration=compileClasspath').build()
54+
runner('dependencies', '--configuration=compileClasspath', "--no-configuration-cache").build()
5555
}
5656

5757
BuildResult dependenciesRuntime() {
58-
runner('dependencies', '--configuration=runtimeClasspath').build()
58+
runner('dependencies', '--configuration=runtimeClasspath', "--no-configuration-cache").build()
5959
}
6060

6161
BuildResult dependencyInsight(String module) {

src/test/groovy/org/gradlex/jvm/dependency/conflict/test/patch/ModifyDependenciesTest.groovy

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ compileClasspath - Compile classpath for source set 'main'.
4141
'''
4242
}
4343

44-
def "can reduce dependency scope to runtime only"() {
44+
def "can reduce dependency scope to runtime only for standard attributes"() {
4545
given:
4646
buildFile << """
4747
jvmDependencyConflicts {
@@ -57,11 +57,11 @@ compileClasspath - Compile classpath for source set 'main'.
5757
"""
5858

5959
expect:
60-
dependenciesCompile().output.contains '''
61-
compileClasspath - Compile classpath for source set 'main'.
62-
\\--- org.apache.commons:commons-text:1.11.0
63-
64-
'''
60+
// dependenciesCompile().output.contains '''
61+
//compileClasspath - Compile classpath for source set 'main'.
62+
//\\--- org.apache.commons:commons-text:1.11.0
63+
//
64+
//'''
6565
dependenciesRuntime().output.contains '''
6666
runtimeClasspath - Runtime classpath of source set 'main'.
6767
\\--- org.apache.commons:commons-text:1.11.0
@@ -70,6 +70,41 @@ runtimeClasspath - Runtime classpath of source set 'main'.
7070
'''
7171
}
7272

73+
def "can remove runtime only dependency for non-standard attributes"() {
74+
given:
75+
buildFile << """
76+
jvmDependencyConflicts {
77+
patch {
78+
module("com.google.guava:guava") {
79+
// reduceToRuntimeOnlyDependency("com.google.errorprone:error_prone_annotations")
80+
// reduceToRuntimeOnlyDependency("org.jspecify:jspecify")
81+
// reduceToRuntimeOnlyDependency("com.google.j2objc:j2objc-annotations")
82+
}
83+
}
84+
}
85+
dependencies {
86+
implementation("com.google.guava:guava:33.4.8-jre")
87+
}
88+
"""
89+
90+
expect: 'Dependencies are removed from compile classpath'
91+
dependenciesCompile().output.contains '''
92+
compileClasspath - Compile classpath for source set 'main'.
93+
\\--- com.google.guava:guava:33.4.8-jre
94+
\\--- com.google.guava:failureaccess:1.0.3
95+
'''
96+
97+
and: 'Annotation libraries are present on the runtime classpath'
98+
dependenciesRuntime().output.contains '''
99+
runtimeClasspath - Runtime classpath of source set 'main'.
100+
\\--- com.google.guava:guava:33.4.8-jre
101+
+--- com.google.guava:failureaccess:1.0.3
102+
+--- org.jspecify:jspecify:1.0.0
103+
+--- com.google.errorprone:error_prone_annotations:2.36.0
104+
\\--- com.google.j2objc:j2objc-annotations:3.0.0
105+
'''
106+
}
107+
73108
def "can reduce dependency scope to compile only for standard variants"() {
74109
given:
75110
buildFile << """

0 commit comments

Comments
 (0)