1818
1919import org .gradle .api .Project ;
2020import org .gradle .api .artifacts .Configuration ;
21+ import org .gradle .api .artifacts .ConfigurationContainer ;
2122import org .gradle .api .artifacts .result .DependencyResult ;
22- import org .gradle .api .artifacts .result .ResolvedComponentResult ;
2323import org .gradle .api .artifacts .result .ResolvedDependencyResult ;
24+ import org .gradle .api .artifacts .result .UnresolvedDependencyResult ;
2425import org .gradle .api .attributes .Attribute ;
26+ import org .gradle .api .attributes .Bundling ;
2527import org .gradle .api .attributes .Category ;
28+ import org .gradle .api .attributes .LibraryElements ;
2629import org .gradle .api .attributes .Usage ;
30+ import org .gradle .api .attributes .java .TargetJvmEnvironment ;
31+ import org .gradle .api .model .ObjectFactory ;
32+ import org .gradle .api .provider .Provider ;
33+ import org .gradle .api .tasks .SourceSet ;
34+ import org .gradle .api .tasks .SourceSetContainer ;
35+ import org .gradle .util .GradleVersion ;
2736
2837import java .io .Serializable ;
2938import java .util .ArrayList ;
3039import java .util .List ;
3140import java .util .stream .Collectors ;
3241import java .util .stream .Stream ;
3342
43+ import static java .util .Collections .emptyList ;
3444import static java .util .Objects .requireNonNull ;
3545import static org .gradle .api .attributes .Category .CATEGORY_ATTRIBUTE ;
3646import static org .gradle .api .attributes .Category .LIBRARY ;
3747import static org .gradle .api .attributes .Usage .USAGE_ATTRIBUTE ;
3848
3949public class PublishedMetadata implements Serializable {
4050 private static final Attribute <String > CATEGORY_ATTRIBUTE_UNTYPED = Attribute .of (CATEGORY_ATTRIBUTE .getName (), String .class );
51+ private static final String DEFAULT_VERSION_SOURCE_CONFIGURATION = "definedDependenciesVersions" ;
4152
4253 private final String gav ;
4354 private final List <String > requires = new ArrayList <>();
4455 private final List <String > requiresTransitive = new ArrayList <>();
4556 private final List <String > requiresStaticTransitive = new ArrayList <>();
57+ private String errorMessage = null ;
4658
47- PublishedMetadata (String gav , Configuration origin , Project project ) {
59+ PublishedMetadata (String gav , Project project , ExtraJavaModuleInfoPluginExtension extension ) {
4860 this .gav = gav ;
49- List <String > compileDependencies = componentVariant (origin , project , Usage .JAVA_API );
50- List <String > runtimeDependencies = componentVariant (origin , project , Usage .JAVA_RUNTIME );
61+
62+ List <String > compileDependencies = componentVariant (extension .getVersionsProvidingConfiguration (), project , Usage .JAVA_API );
63+ List <String > runtimeDependencies = componentVariant (extension .getVersionsProvidingConfiguration (), project , Usage .JAVA_RUNTIME );
5164
5265 Stream .concat (compileDependencies .stream (), runtimeDependencies .stream ()).distinct ().forEach (ga -> {
5366 if (compileDependencies .contains (ga ) && runtimeDependencies .contains (ga )) {
@@ -60,26 +73,73 @@ public class PublishedMetadata implements Serializable {
6073 });
6174 }
6275
63- private List <String > componentVariant (Configuration origin , Project project , String usage ) {
76+ private List <String > componentVariant (Provider <String > versionsProvidingConfiguration , Project project , String usage ) {
77+ Configuration versionsSource ;
78+ if (versionsProvidingConfiguration .isPresent ()) {
79+ versionsSource = project .getConfigurations ().getByName (versionsProvidingConfiguration .get ());
80+ } else {
81+ // version provider is not configured, create on adhoc based on ALL classpaths of the project
82+ versionsSource = maybeCreateDefaultVersionSourcConfiguration (project .getConfigurations (), project .getObjects (),
83+ project .getExtensions ().findByType (SourceSetContainer .class ));
84+ }
85+
6486 Configuration singleComponentVariantResolver = project .getConfigurations ().detachedConfiguration (project .getDependencies ().create (gav ));
6587 singleComponentVariantResolver .setCanBeConsumed (false );
66- singleComponentVariantResolver .shouldResolveConsistentlyWith (origin );
67- origin .getAttributes ().keySet ().forEach (a -> {
88+ singleComponentVariantResolver .shouldResolveConsistentlyWith (versionsSource );
89+ versionsSource .getAttributes ().keySet ().forEach (a -> {
6890 @ SuppressWarnings ("rawtypes" ) Attribute untypedAttributeKey = a ;
6991 //noinspection unchecked
70- singleComponentVariantResolver .getAttributes ().attribute (untypedAttributeKey , requireNonNull (origin .getAttributes ().getAttribute (a )));
92+ singleComponentVariantResolver .getAttributes ().attribute (untypedAttributeKey , requireNonNull (versionsSource .getAttributes ().getAttribute (a )));
7193 });
7294 singleComponentVariantResolver .getAttributes ().attribute (USAGE_ATTRIBUTE , project .getObjects ().named (Usage .class , usage ));
73- return firstAndOnlyComponent (singleComponentVariantResolver ).getDependencies ().stream ()
74- .filter (PublishedMetadata ::filterComponentDependencies )
75- .map (PublishedMetadata ::ga )
76- .collect (Collectors .toList ());
95+ return firstAndOnlyComponentDependencies (singleComponentVariantResolver );
96+ }
97+
98+ private Configuration maybeCreateDefaultVersionSourcConfiguration (ConfigurationContainer configurations , ObjectFactory objects , SourceSetContainer sourceSets ) {
99+ String name = DEFAULT_VERSION_SOURCE_CONFIGURATION ;
100+ Configuration existing = configurations .findByName (name );
101+ if (existing != null ) {
102+ return existing ;
103+ }
104+
105+ return configurations .create (name , c -> {
106+ c .setCanBeResolved (true );
107+ c .setCanBeConsumed (false );
108+ c .getAttributes ().attribute (Usage .USAGE_ATTRIBUTE , objects .named (Usage .class , Usage .JAVA_RUNTIME ));
109+ c .getAttributes ().attribute (Category .CATEGORY_ATTRIBUTE , objects .named (Category .class , Category .LIBRARY ));
110+ c .getAttributes ().attribute (LibraryElements .LIBRARY_ELEMENTS_ATTRIBUTE , objects .named (LibraryElements .class , LibraryElements .JAR ));
111+ c .getAttributes ().attribute (Bundling .BUNDLING_ATTRIBUTE , objects .named (Bundling .class , Bundling .EXTERNAL ));
112+ if (GradleVersion .current ().compareTo (GradleVersion .version ("7.0" )) >= 0 ) {
113+ c .getAttributes ().attribute (TargetJvmEnvironment .TARGET_JVM_ENVIRONMENT_ATTRIBUTE ,
114+ objects .named (TargetJvmEnvironment .class , TargetJvmEnvironment .STANDARD_JVM ));
115+ }
116+
117+ if (sourceSets != null ) {
118+ for (SourceSet sourceSet : sourceSets ) {
119+ Configuration implementation = configurations .getByName (sourceSet .getImplementationConfigurationName ());
120+ Configuration compileOnly = configurations .getByName (sourceSet .getCompileOnlyConfigurationName ());
121+ Configuration runtimeOnly = configurations .getByName (sourceSet .getRuntimeOnlyConfigurationName ());
122+ Configuration annotationProcessor = configurations .getByName (sourceSet .getAnnotationProcessorConfigurationName ());
123+ c .extendsFrom (implementation , compileOnly , runtimeOnly , annotationProcessor );
124+ }
125+ }
126+ });
77127 }
78128
79- private ResolvedComponentResult firstAndOnlyComponent (Configuration singleComponentVariantResolver ) {
80- ResolvedDependencyResult onlyResult = (ResolvedDependencyResult ) singleComponentVariantResolver .getIncoming ().getResolutionResult ()
81- .getRoot ().getDependencies ().iterator ().next ();
82- return onlyResult .getSelected ();
129+ private List <String > firstAndOnlyComponentDependencies (Configuration singleComponentVariantResolver ) {
130+ DependencyResult result = singleComponentVariantResolver
131+ .getIncoming ().getResolutionResult ().getRoot ()
132+ .getDependencies ().iterator ().next ();
133+
134+ if (result instanceof UnresolvedDependencyResult ) {
135+ errorMessage = ((UnresolvedDependencyResult ) result ).getFailure ().getMessage ();
136+ return emptyList ();
137+ } else {
138+ return ((ResolvedDependencyResult ) result ).getSelected ().getDependencies ().stream ()
139+ .filter (PublishedMetadata ::filterComponentDependencies )
140+ .map (PublishedMetadata ::ga )
141+ .collect (Collectors .toList ());
142+ }
83143 }
84144
85145 private static boolean filterComponentDependencies (DependencyResult d ) {
@@ -113,4 +173,8 @@ public List<String> getRequiresTransitive() {
113173 public List <String > getRequiresStaticTransitive () {
114174 return requiresStaticTransitive ;
115175 }
176+
177+ public String getErrorMessage () {
178+ return errorMessage ;
179+ }
116180}
0 commit comments