From e196af3b289637ec3c206cd52cfa450130f7c3f8 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Sat, 13 Jan 2024 22:33:03 +0100 Subject: [PATCH] requireAllDefinedDependencies: skip platform (non-library) dependencies --- .../moduleinfo/ExtraJavaModuleInfoPlugin.java | 30 +++++++++++++++---- ...llDefinedDependenciesFunctionalTest.groovy | 30 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoPlugin.java b/src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoPlugin.java index eb2ca9f..daddec8 100644 --- a/src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoPlugin.java +++ b/src/main/java/org/gradlex/javamodule/moduleinfo/ExtraJavaModuleInfoPlugin.java @@ -43,16 +43,27 @@ import org.gradlex.javamodule.moduleinfo.tasks.ModuleDescriptorRecommendation; import java.io.File; -import java.util.*; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.gradle.api.attributes.Category.CATEGORY_ATTRIBUTE; +import static org.gradle.api.attributes.Category.LIBRARY; +import static org.gradle.api.attributes.Usage.JAVA_RUNTIME; +import static org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE; +import static org.gradle.api.plugins.JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME; + /** * Entry point of the plugin. */ @SuppressWarnings("unused") @NonNullApi public abstract class ExtraJavaModuleInfoPlugin implements Plugin { + private static final Attribute CATEGORY_ATTRIBUTE_UNTYPED = Attribute.of(CATEGORY_ATTRIBUTE.getName(), String.class); @Override public void apply(Project project) { @@ -125,8 +136,8 @@ private void configureTransform(Project project, ExtraJavaModuleInfoPluginExtens c.setVisible(false); c.setCanBeConsumed(false); c.setCanBeResolved(true); - c.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)); - c.getAttributes().attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY)); + c.getAttributes().attribute(USAGE_ATTRIBUTE, project.getObjects().named(Usage.class, JAVA_RUNTIME)); + c.getAttributes().attribute(CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, LIBRARY)); // Automatically add dependencies for Jars where we know the coordinates c.withDependencies(d -> extension.getModuleSpecs().get().values().stream().flatMap(m -> @@ -136,7 +147,7 @@ private void configureTransform(Project project, ExtraJavaModuleInfoPluginExtens // Automatically get versions from the runtime classpath if (GradleVersion.current().compareTo(GradleVersion.version("6.8")) >= 0) { //noinspection UnstableApiUsage - c.shouldResolveConsistentlyWith(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)); + c.shouldResolveConsistentlyWith(project.getConfigurations().getByName(RUNTIME_CLASSPATH_CONFIGURATION_NAME)); } }); @@ -208,7 +219,7 @@ private Stream filteredResolutionResult(Configuration c private Map> toStringMap(Stream result) { return result.collect(Collectors.toMap( c -> ga(c.getId()), - c -> c.getDependencies().stream().map(ExtraJavaModuleInfoPlugin::ga).collect(Collectors.toSet()), + c -> c.getDependencies().stream().filter(ExtraJavaModuleInfoPlugin::filterComponentDependencies).map(ExtraJavaModuleInfoPlugin::ga).collect(Collectors.toSet()), (dependencies1, dependencies2) -> dependencies1)); } @@ -216,6 +227,15 @@ private static boolean needsDependencies(ModuleSpec moduleSpec) { return moduleSpec instanceof ModuleInfo && ((ModuleInfo) moduleSpec).requireAllDefinedDependencies; } + private static boolean filterComponentDependencies(DependencyResult d) { + if (d instanceof ResolvedDependencyResult) { + Category category = ((ResolvedDependencyResult) d).getResolvedVariant().getAttributes().getAttribute(CATEGORY_ATTRIBUTE); + String categoryUntyped = ((ResolvedDependencyResult) d).getResolvedVariant().getAttributes().getAttribute(CATEGORY_ATTRIBUTE_UNTYPED); + return LIBRARY.equals(categoryUntyped) || (category != null && LIBRARY.equals(category.getName())); + } + return false; + } + private static String ga(DependencyResult d) { if (d instanceof ResolvedDependencyResult) { return ga(((ResolvedDependencyResult) d).getSelected().getId()); diff --git a/src/test/groovy/org/gradlex/javamodule/moduleinfo/test/RequireAllDefinedDependenciesFunctionalTest.groovy b/src/test/groovy/org/gradlex/javamodule/moduleinfo/test/RequireAllDefinedDependenciesFunctionalTest.groovy index 46e1f44..0ea752b 100644 --- a/src/test/groovy/org/gradlex/javamodule/moduleinfo/test/RequireAllDefinedDependenciesFunctionalTest.groovy +++ b/src/test/groovy/org/gradlex/javamodule/moduleinfo/test/RequireAllDefinedDependenciesFunctionalTest.groovy @@ -338,4 +338,34 @@ class RequireAllDefinedDependenciesFunctionalTest extends Specification { expect: run().task(':run').outcome == TaskOutcome.SUCCESS } + + def "can handle components with platform dependencies"() { + given: + file("src/main/java/org/gradle/sample/app/Main.java") << """ + package org.gradle.sample.app; + public class Main { public static void main(String[] args) { } } + """ + file("src/main/java/module-info.java") << """ + module org.gradle.sample.app { requires com.fasterxml.jackson.databind; } + """ + buildFile << """ + dependencies { + implementation("com.fasterxml.jackson.core:jackson-databind:2.16.1") + } + + extraJavaModuleInfo { + module("com.fasterxml.jackson.core:jackson-databind", "com.fasterxml.jackson.databind") { + patchRealModule() + exportAllPackages() + requireAllDefinedDependencies() + } + // 'com.fasterxml.jackson:jackson-bom' is ignored + knownModule("com.fasterxml.jackson.core:jackson-core", "com.fasterxml.jackson.core") + knownModule("com.fasterxml.jackson.core:jackson-annotations", "com.fasterxml.jackson.annotation") + } + """ + + expect: + run().task(':run').outcome == TaskOutcome.SUCCESS + } }