44import static java .util .Collections .emptyList ;
55import static java .util .Objects .requireNonNull ;
66import static org .gradle .api .attributes .Category .CATEGORY_ATTRIBUTE ;
7+ import static org .gradle .api .attributes .Category .ENFORCED_PLATFORM ;
78import static org .gradle .api .attributes .Category .LIBRARY ;
9+ import static org .gradle .api .attributes .Category .REGULAR_PLATFORM ;
810import static org .gradle .api .attributes .Usage .USAGE_ATTRIBUTE ;
911
1012import java .io .Serializable ;
1517import org .gradle .api .Project ;
1618import org .gradle .api .artifacts .Configuration ;
1719import org .gradle .api .artifacts .ConfigurationContainer ;
20+ import org .gradle .api .artifacts .Dependency ;
21+ import org .gradle .api .artifacts .ModuleDependency ;
1822import org .gradle .api .artifacts .result .DependencyResult ;
1923import org .gradle .api .artifacts .result .ResolvedDependencyResult ;
2024import org .gradle .api .artifacts .result .UnresolvedDependencyResult ;
3438public class PublishedMetadata implements Serializable {
3539 private static final Attribute <String > CATEGORY_ATTRIBUTE_UNTYPED =
3640 Attribute .of (CATEGORY_ATTRIBUTE .getName (), String .class );
41+ private static final Attribute <Category > CATEGORY_ATTRIBUTE_TYPED =
42+ Attribute .of (CATEGORY_ATTRIBUTE .getName (), Category .class );
3743 private static final String DEFAULT_VERSION_SOURCE_CONFIGURATION = "definedDependenciesVersions" ;
3844
3945 private final String gav ;
@@ -47,10 +53,16 @@ public class PublishedMetadata implements Serializable {
4753 PublishedMetadata (String gav , Project project , ExtraJavaModuleInfoPluginExtension extension ) {
4854 this .gav = gav ;
4955
50- List <String > compileDependencies =
51- componentVariant (extension .getVersionsProvidingConfiguration (), project , Usage .JAVA_API );
52- List <String > runtimeDependencies =
53- componentVariant (extension .getVersionsProvidingConfiguration (), project , Usage .JAVA_RUNTIME );
56+ List <String > compileDependencies = componentVariant (
57+ extension .getVersionsProvidingConfiguration (),
58+ extension .getPlatformDependency (),
59+ project ,
60+ Usage .JAVA_API );
61+ List <String > runtimeDependencies = componentVariant (
62+ extension .getVersionsProvidingConfiguration (),
63+ extension .getPlatformDependency (),
64+ project ,
65+ Usage .JAVA_RUNTIME );
5466
5567 Stream .concat (compileDependencies .stream (), runtimeDependencies .stream ())
5668 .distinct ()
@@ -67,7 +79,10 @@ public class PublishedMetadata implements Serializable {
6779
6880 @ SuppressWarnings ({"UnstableApiUsage" , "unchecked" })
6981 private List <String > componentVariant (
70- Provider <String > versionsProvidingConfiguration , Project project , String usage ) {
82+ Provider <String > versionsProvidingConfiguration ,
83+ Provider <Dependency > platformDependencyProvider ,
84+ Project project ,
85+ String usage ) {
7186 Configuration versionsSource ;
7287 if (versionsProvidingConfiguration .isPresent ()) {
7388 versionsSource = project .getConfigurations ()
@@ -81,8 +96,29 @@ private List<String> componentVariant(
8196 project .getExtensions ().findByType (SourceSetContainer .class ));
8297 }
8398
84- Configuration singleComponentVariantResolver = project .getConfigurations ()
85- .detachedConfiguration (project .getDependencies ().create (gav ));
99+ List <Dependency > dependencies = new ArrayList <>();
100+ dependencies .add (project .getDependencies ().create (gav ));
101+ if (platformDependencyProvider .isPresent ()) {
102+ if (!ModuleDependency .class .isAssignableFrom (
103+ platformDependencyProvider .get ().getClass ())) {
104+ throw new IllegalArgumentException ("Unable to determine dependency '"
105+ + platformDependencyProvider .get ().getName () + "' type" );
106+ }
107+
108+ ModuleDependency platformDependency = (ModuleDependency ) platformDependencyProvider .get ();
109+ // A platform dependency must have the platform attribute specified.
110+ Category category = platformDependency .getAttributes ().getAttribute (CATEGORY_ATTRIBUTE_TYPED );
111+ if (category == null
112+ || (!category .getName ().equals (REGULAR_PLATFORM )
113+ && !category .getName ().equals (ENFORCED_PLATFORM ))) {
114+ throw new IllegalArgumentException (
115+ "Dependency '" + platformDependency .getName () + "' is not a platform" );
116+ }
117+ dependencies .add (platformDependency );
118+ }
119+
120+ Configuration singleComponentVariantResolver =
121+ project .getConfigurations ().detachedConfiguration (dependencies .toArray (new Dependency [0 ]));
86122 singleComponentVariantResolver .setCanBeConsumed (false );
87123 singleComponentVariantResolver .shouldResolveConsistentlyWith (versionsSource );
88124 versionsSource .getAttributes ().keySet ().forEach (a -> {
0 commit comments