Skip to content

Commit 369336b

Browse files
committed
Configure ignoring transitive dependencies via ComponentMetadataRules
1 parent ecf7eae commit 369336b

File tree

14 files changed

+630
-36
lines changed

14 files changed

+630
-36
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal;
11+
12+
import org.gradle.api.Named;
13+
import org.gradle.api.attributes.Attribute;
14+
15+
public interface DependencyContext extends Named {
16+
Attribute<DependencyContext> CONTEXT_ATTRIBUTE = Attribute.of("org.elasticsearch.context", DependencyContext.class);
17+
String CODE_QUALITY = "code-quality";
18+
19+
default String getName() {
20+
return "org.elasticsearch.context";
21+
}
22+
}

build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/precommit/FormattingPrecommitPlugin.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.diffplug.gradle.spotless.SpotlessExtension;
1313
import com.diffplug.gradle.spotless.SpotlessPlugin;
1414

15+
import org.elasticsearch.gradle.internal.DependencyContext;
1516
import org.elasticsearch.gradle.internal.conventions.util.Util;
1617
import org.gradle.api.Plugin;
1718
import org.gradle.api.Project;
@@ -43,6 +44,7 @@
4344
*/
4445
public class FormattingPrecommitPlugin implements Plugin<Project> {
4546

47+
@SuppressWarnings("checkstyle:RegexpMultiline")
4648
@Override
4749
public void apply(Project project) {
4850
project.getPluginManager().withPlugin("java-base", javaBasePlugin -> {
@@ -52,6 +54,22 @@ public void apply(Project project) {
5254
// Spotless resolves required dependencies from project repositories, so we need maven central
5355
project.getRepositories().mavenCentral();
5456

57+
// project.getDependencies()
58+
// .components(
59+
// metadataHandler -> metadataHandler.withModule(
60+
// "com.google.googlejavaformat:google-java-format",
61+
// componentMetadataDetails -> componentMetadataDetails.allVariants(
62+
// variant -> variant.withDependencies(depMetadata -> depMetadata.add("com.google.guava:guava:32.1.3-jre"))
63+
// )
64+
// )
65+
// );
66+
project.getConfigurations().matching(spec -> spec.getName().startsWith("spotless")).all(conf -> {
67+
conf.getAttributes()
68+
.attribute(
69+
DependencyContext.CONTEXT_ATTRIBUTE,
70+
project.getObjects().named(DependencyContext.class, DependencyContext.CODE_QUALITY)
71+
);
72+
});
5573
project.getExtensions().getByType(SpotlessExtension.class).java(java -> {
5674
File elasticsearchWorkspace = Util.locateElasticsearchWorkspace(project.getGradle());
5775
String importOrderPath = "build-conventions/elastic.importorder";
@@ -74,7 +92,7 @@ public void apply(Project project) {
7492
// When running build benchmarks we alter the source in some scenarios.
7593
// The gradle-profiler unfortunately does not generate compliant formatted
7694
// sources so we ignore that altered file when running build benchmarks
77-
if(Boolean.getBoolean("BUILD_PERFORMANCE_TEST") && project.getPath().equals(":server")) {
95+
if (Boolean.getBoolean("BUILD_PERFORMANCE_TEST") && project.getPath().equals(":server")) {
7896
java.targetExclude("src/main/java/org/elasticsearch/bootstrap/BootstrapInfo.java");
7997
}
8098
});

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchJavaBasePlugin.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,6 @@ public static void configureConfigurations(Project project) {
106106
}
107107
configuration.resolutionStrategy(ResolutionStrategy::failOnVersionConflict);
108108
});
109-
110-
// disable transitive dependency management
111-
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
112-
sourceSets.all(sourceSet -> disableTransitiveDependenciesForSourceSet(project, sourceSet));
113-
}
114-
115-
private static void disableTransitiveDependenciesForSourceSet(Project project, SourceSet sourceSet) {
116-
List<String> sourceSetConfigurationNames = List.of(
117-
sourceSet.getApiConfigurationName(),
118-
sourceSet.getImplementationConfigurationName(),
119-
sourceSet.getCompileOnlyConfigurationName(),
120-
sourceSet.getRuntimeOnlyConfigurationName()
121-
);
122-
123-
project.getConfigurations()
124-
.matching(c -> sourceSetConfigurationNames.contains(c.getName()))
125-
.configureEach(GradleUtils::disableTransitiveDependencies);
126109
}
127110

128111
/**

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchJavaModulePathPlugin.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.gradle.api.artifacts.result.ResolvedComponentResult;
2222
import org.gradle.api.artifacts.result.ResolvedDependencyResult;
2323
import org.gradle.api.attributes.LibraryElements;
24+
import org.gradle.api.attributes.Usage;
2425
import org.gradle.api.file.FileCollection;
2526
import org.gradle.api.logging.Logger;
2627
import org.gradle.api.plugins.JavaPlugin;
@@ -76,10 +77,16 @@ void configureCompileModulePath(Project project) {
7677
it.setCanBeResolved(true);
7778
it.setCanBeConsumed(false); // we don't want this configuration used by dependent projects
7879
it.attributes(
79-
attrs -> attrs.attribute(
80-
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
81-
project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES)
82-
)
80+
attrs -> {
81+
attrs.attribute(
82+
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
83+
project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES)
84+
);
85+
attrs.attribute(
86+
Usage.USAGE_ATTRIBUTE,
87+
project.getObjects().named(Usage.class, Usage.JAVA_API)
88+
);
89+
}
8390
);
8491
}).getIncoming().artifactView(it -> {
8592
it.componentFilter(cf -> {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.dependencies.rules;
11+
12+
import org.elasticsearch.gradle.internal.DependencyContext;
13+
import org.gradle.api.artifacts.ComponentMetadataContext;
14+
import org.gradle.api.artifacts.ComponentMetadataRule;
15+
import org.gradle.api.model.ObjectFactory;
16+
17+
import javax.inject.Inject;
18+
19+
public abstract class CodeQualityRule implements ComponentMetadataRule {
20+
21+
@Inject
22+
public ObjectFactory getObjects() {
23+
throw new UnsupportedOperationException();
24+
}
25+
26+
@Override
27+
public void execute(ComponentMetadataContext context) {
28+
System.out.println("CodeQualityRule#context = " + context.getDetails().getId());
29+
context.getDetails().addVariant("codeQuality", "runtime", variant -> {
30+
System.out.println("CodeQualityRule#context#variant = " + variant);
31+
// variant.attributes(attrs -> {
32+
// ystem.out.println("Adding codeQuality variant to " + context.getDetails().getId());
33+
variant.getAttributes()
34+
.attribute(
35+
DependencyContext.CONTEXT_ATTRIBUTE,
36+
getObjects().named(DependencyContext.class, DependencyContext.CODE_QUALITY)
37+
);
38+
});
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.dependencies.rules;
11+
12+
import org.gradle.api.artifacts.ComponentMetadataContext;
13+
import org.gradle.api.artifacts.ComponentMetadataRule;
14+
15+
import java.util.List;
16+
17+
public class DependencyRemovalByNameRule implements ComponentMetadataRule {
18+
19+
private final List<String> modulesToRemove;
20+
21+
public DependencyRemovalByNameRule(String... modulesToRemove) {
22+
this.modulesToRemove = List.of(modulesToRemove);
23+
}
24+
25+
@Override
26+
public void execute(ComponentMetadataContext context) {
27+
context.getDetails().allVariants(variants -> {
28+
variants.withDependencies(dependencies -> {
29+
dependencies.removeIf(metadata -> modulesToRemove.contains(metadata.getName()));
30+
});
31+
});
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.dependencies.rules;
11+
12+
import org.elasticsearch.gradle.internal.DependencyContext;
13+
import org.gradle.api.artifacts.ComponentMetadataContext;
14+
import org.gradle.api.artifacts.ComponentMetadataRule;
15+
import org.gradle.api.artifacts.VariantMetadata;
16+
import org.gradle.api.attributes.Attribute;
17+
import org.gradle.api.internal.artifacts.repositories.resolver.VariantMetadataAdapter;
18+
19+
//@CacheableRule
20+
public abstract class ExcludeTransitivesRule implements ComponentMetadataRule {
21+
22+
23+
@Override
24+
public void execute(ComponentMetadataContext context) {
25+
context.getDetails().allVariants(variant -> {
26+
variant.withDependencies(dependencies -> {
27+
// Exclude all transitive dependencies
28+
if(dependencies.isEmpty()) {
29+
System.out.println(context.getDetails().getId().getGroup() + ":" + context.getDetails().getId().getName());
30+
}
31+
dependencies.clear();
32+
// dependencies.removeIf(p -> {
33+
// System.out.println("p.getName() = " + p.getName());
34+
// return true;
35+
// });
36+
});
37+
});
38+
}
39+
//
40+
// private boolean isCodeQuality(VariantMetadata variant) {
41+
// // System.out.println("CodeQualityRule#context#variantName = " + ((VariantMetadataAdapter)variant).variantName);
42+
// System.out.println("ExcludeTransitivesRule#context#isCodeQuality = " + ((VariantMetadataAdapter) variant).toString());
43+
// try {
44+
// java.lang.reflect.Field field = VariantMetadataAdapter.class.getDeclaredField("variantName");
45+
// field.setAccessible(true);
46+
// Object variantName = field.get(variant);
47+
// System.out.println("ExcludeTransitivesRule#context#variantName = " + variantName);
48+
// } catch (Exception e) {
49+
// System.out.println("Failed to access variantName: " + e.getMessage());
50+
// }
51+
// DependencyContext attribute = variant.getAttributes().getAttribute(DependencyContext.CONTEXT_ATTRIBUTE);
52+
// return attribute != null && DependencyContext.CODE_QUALITY.equals(attribute.getName());
53+
// }
54+
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.dependencies.rules;
11+
12+
import org.gradle.api.artifacts.CacheableRule;
13+
import org.gradle.api.artifacts.ComponentMetadataContext;
14+
import org.gradle.api.artifacts.ComponentMetadataRule;
15+
16+
@CacheableRule
17+
public abstract class Log4jRule implements ComponentMetadataRule {
18+
@Override
19+
public void execute(ComponentMetadataContext context) {
20+
context.getDetails().withVariant("apiElements", variantMetadata -> {
21+
variantMetadata.withDependencies(
22+
deps -> {
23+
deps.add("org.jspecify:jspecify:1.0.0");
24+
deps.add("com.github.spotbugs:spotbugs-annotations:4.9.3");
25+
deps.add("com.google.errorprone:error_prone_annotations:2.38.0");
26+
}
27+
);
28+
});
29+
}
30+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.dependencies.rules;
11+
12+
import org.elasticsearch.gradle.internal.DependencyContext;
13+
import org.gradle.api.artifacts.ComponentMetadataContext;
14+
import org.gradle.api.artifacts.ComponentMetadataRule;
15+
import org.gradle.api.model.ObjectFactory;
16+
17+
import javax.inject.Inject;
18+
19+
public abstract class SpotlessRule implements ComponentMetadataRule {
20+
21+
final String baseVariant;
22+
23+
@Inject
24+
public SpotlessRule(String baseVariant) {
25+
this.baseVariant = baseVariant;
26+
}
27+
28+
@Inject
29+
public ObjectFactory getObjects() {
30+
throw new UnsupportedOperationException();
31+
}
32+
33+
@Override
34+
public void execute(ComponentMetadataContext context) {
35+
System.out.println("SpotlessRule#context = " + context.getDetails().getId());
36+
// context.getDetails().allVariants(variant -> {
37+
// variant.getAttributes().attribute(
38+
// Attribute.of("custom.attribute", String.class),
39+
// "some-value"
40+
// );
41+
// });
42+
43+
context.getDetails().addVariant("codeQuality", baseVariant, variant -> {
44+
variant.getAttributes()
45+
.attribute(
46+
DependencyContext.CONTEXT_ATTRIBUTE,
47+
getObjects().named(DependencyContext.class, DependencyContext.CODE_QUALITY)
48+
);
49+
});
50+
51+
}
52+
}

0 commit comments

Comments
 (0)