4343import org .gradlex .javamodule .moduleinfo .tasks .ModuleDescriptorRecommendation ;
4444
4545import java .io .File ;
46- import java .util .*;
46+ import java .util .Collection ;
47+ import java .util .Comparator ;
48+ import java .util .List ;
49+ import java .util .Map ;
50+ import java .util .Set ;
4751import java .util .stream .Collectors ;
4852import java .util .stream .Stream ;
4953
54+ import static org .gradle .api .attributes .Category .CATEGORY_ATTRIBUTE ;
55+ import static org .gradle .api .attributes .Category .LIBRARY ;
56+ import static org .gradle .api .attributes .Usage .JAVA_RUNTIME ;
57+ import static org .gradle .api .attributes .Usage .USAGE_ATTRIBUTE ;
58+ import static org .gradle .api .plugins .JavaPlugin .RUNTIME_CLASSPATH_CONFIGURATION_NAME ;
59+
5060/**
5161 * Entry point of the plugin.
5262 */
5363@ SuppressWarnings ("unused" )
5464@ NonNullApi
5565public abstract class ExtraJavaModuleInfoPlugin implements Plugin <Project > {
66+ private static final Attribute <String > CATEGORY_ATTRIBUTE_UNTYPED = Attribute .of (CATEGORY_ATTRIBUTE .getName (), String .class );
5667
5768 @ Override
5869 public void apply (Project project ) {
@@ -125,8 +136,8 @@ private void configureTransform(Project project, ExtraJavaModuleInfoPluginExtens
125136 c .setVisible (false );
126137 c .setCanBeConsumed (false );
127138 c .setCanBeResolved (true );
128- c .getAttributes ().attribute (Usage . USAGE_ATTRIBUTE , project .getObjects ().named (Usage .class , Usage . JAVA_RUNTIME ));
129- c .getAttributes ().attribute (Category . CATEGORY_ATTRIBUTE , project .getObjects ().named (Category .class , Category . LIBRARY ));
139+ c .getAttributes ().attribute (USAGE_ATTRIBUTE , project .getObjects ().named (Usage .class , JAVA_RUNTIME ));
140+ c .getAttributes ().attribute (CATEGORY_ATTRIBUTE , project .getObjects ().named (Category .class , LIBRARY ));
130141
131142 // Automatically add dependencies for Jars where we know the coordinates
132143 c .withDependencies (d -> extension .getModuleSpecs ().get ().values ().stream ().flatMap (m ->
@@ -136,7 +147,7 @@ private void configureTransform(Project project, ExtraJavaModuleInfoPluginExtens
136147 // Automatically get versions from the runtime classpath
137148 if (GradleVersion .current ().compareTo (GradleVersion .version ("6.8" )) >= 0 ) {
138149 //noinspection UnstableApiUsage
139- c .shouldResolveConsistentlyWith (project .getConfigurations ().getByName (JavaPlugin . RUNTIME_CLASSPATH_CONFIGURATION_NAME ));
150+ c .shouldResolveConsistentlyWith (project .getConfigurations ().getByName (RUNTIME_CLASSPATH_CONFIGURATION_NAME ));
140151 }
141152 });
142153
@@ -208,14 +219,23 @@ private Stream<ResolvedComponentResult> filteredResolutionResult(Configuration c
208219 private Map <String , Set <String >> toStringMap (Stream <ResolvedComponentResult > result ) {
209220 return result .collect (Collectors .toMap (
210221 c -> ga (c .getId ()),
211- c -> c .getDependencies ().stream ().map (ExtraJavaModuleInfoPlugin ::ga ).collect (Collectors .toSet ()),
222+ c -> c .getDependencies ().stream ().filter ( ExtraJavaModuleInfoPlugin :: filterComponentDependencies ). map (ExtraJavaModuleInfoPlugin ::ga ).collect (Collectors .toSet ()),
212223 (dependencies1 , dependencies2 ) -> dependencies1 ));
213224 }
214225
215226 private static boolean needsDependencies (ModuleSpec moduleSpec ) {
216227 return moduleSpec instanceof ModuleInfo && ((ModuleInfo ) moduleSpec ).requireAllDefinedDependencies ;
217228 }
218229
230+ private static boolean filterComponentDependencies (DependencyResult d ) {
231+ if (d instanceof ResolvedDependencyResult ) {
232+ Category category = ((ResolvedDependencyResult ) d ).getResolvedVariant ().getAttributes ().getAttribute (CATEGORY_ATTRIBUTE );
233+ String categoryUntyped = ((ResolvedDependencyResult ) d ).getResolvedVariant ().getAttributes ().getAttribute (CATEGORY_ATTRIBUTE_UNTYPED );
234+ return LIBRARY .equals (categoryUntyped ) || (category != null && LIBRARY .equals (category .getName ()));
235+ }
236+ return false ;
237+ }
238+
219239 private static String ga (DependencyResult d ) {
220240 if (d instanceof ResolvedDependencyResult ) {
221241 return ga (((ResolvedDependencyResult ) d ).getSelected ().getId ());
0 commit comments