Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requireAllDefinedDependencies - skip platform (non-library) dependencies #94

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
private static final Attribute<String> CATEGORY_ATTRIBUTE_UNTYPED = Attribute.of(CATEGORY_ATTRIBUTE.getName(), String.class);

@Override
public void apply(Project project) {
Expand Down Expand Up @@ -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 ->
Expand All @@ -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));
}
});

Expand Down Expand Up @@ -208,14 +219,23 @@ private Stream<ResolvedComponentResult> filteredResolutionResult(Configuration c
private Map<String, Set<String>> toStringMap(Stream<ResolvedComponentResult> 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));
}

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}