18
18
19
19
import org .gradle .api .Project ;
20
20
import org .gradle .api .artifacts .Configuration ;
21
+ import org .gradle .api .artifacts .ConfigurationContainer ;
21
22
import org .gradle .api .artifacts .result .DependencyResult ;
22
- import org .gradle .api .artifacts .result .ResolvedComponentResult ;
23
23
import org .gradle .api .artifacts .result .ResolvedDependencyResult ;
24
+ import org .gradle .api .artifacts .result .UnresolvedDependencyResult ;
24
25
import org .gradle .api .attributes .Attribute ;
26
+ import org .gradle .api .attributes .Bundling ;
25
27
import org .gradle .api .attributes .Category ;
28
+ import org .gradle .api .attributes .LibraryElements ;
26
29
import 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 ;
27
36
28
37
import java .io .Serializable ;
29
38
import java .util .ArrayList ;
30
39
import java .util .List ;
31
40
import java .util .stream .Collectors ;
32
41
import java .util .stream .Stream ;
33
42
43
+ import static java .util .Collections .emptyList ;
34
44
import static java .util .Objects .requireNonNull ;
35
45
import static org .gradle .api .attributes .Category .CATEGORY_ATTRIBUTE ;
36
46
import static org .gradle .api .attributes .Category .LIBRARY ;
37
47
import static org .gradle .api .attributes .Usage .USAGE_ATTRIBUTE ;
38
48
39
49
public class PublishedMetadata implements Serializable {
40
50
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" ;
41
52
42
53
private final String gav ;
43
54
private final List <String > requires = new ArrayList <>();
44
55
private final List <String > requiresTransitive = new ArrayList <>();
45
56
private final List <String > requiresStaticTransitive = new ArrayList <>();
57
+ private String errorMessage = null ;
46
58
47
- PublishedMetadata (String gav , Configuration origin , Project project ) {
59
+ PublishedMetadata (String gav , Project project , ExtraJavaModuleInfoPluginExtension extension ) {
48
60
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 );
51
64
52
65
Stream .concat (compileDependencies .stream (), runtimeDependencies .stream ()).distinct ().forEach (ga -> {
53
66
if (compileDependencies .contains (ga ) && runtimeDependencies .contains (ga )) {
@@ -60,26 +73,73 @@ public class PublishedMetadata implements Serializable {
60
73
});
61
74
}
62
75
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
+
64
86
Configuration singleComponentVariantResolver = project .getConfigurations ().detachedConfiguration (project .getDependencies ().create (gav ));
65
87
singleComponentVariantResolver .setCanBeConsumed (false );
66
- singleComponentVariantResolver .shouldResolveConsistentlyWith (origin );
67
- origin .getAttributes ().keySet ().forEach (a -> {
88
+ singleComponentVariantResolver .shouldResolveConsistentlyWith (versionsSource );
89
+ versionsSource .getAttributes ().keySet ().forEach (a -> {
68
90
@ SuppressWarnings ("rawtypes" ) Attribute untypedAttributeKey = a ;
69
91
//noinspection unchecked
70
- singleComponentVariantResolver .getAttributes ().attribute (untypedAttributeKey , requireNonNull (origin .getAttributes ().getAttribute (a )));
92
+ singleComponentVariantResolver .getAttributes ().attribute (untypedAttributeKey , requireNonNull (versionsSource .getAttributes ().getAttribute (a )));
71
93
});
72
94
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
+ });
77
127
}
78
128
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
+ }
83
143
}
84
144
85
145
private static boolean filterComponentDependencies (DependencyResult d ) {
@@ -113,4 +173,8 @@ public List<String> getRequiresTransitive() {
113
173
public List <String > getRequiresStaticTransitive () {
114
174
return requiresStaticTransitive ;
115
175
}
176
+
177
+ public String getErrorMessage () {
178
+ return errorMessage ;
179
+ }
116
180
}
0 commit comments