11
11
12
12
import org .elasticsearch .gradle .internal .precommit .DependencyLicensesTask ;
13
13
import org .elasticsearch .gradle .internal .precommit .LicenseAnalyzer ;
14
- import org .gradle .api .artifacts .Configuration ;
15
- import org .gradle .api .artifacts .Dependency ;
16
- import org .gradle .api .artifacts .DependencySet ;
17
- import org .gradle .api .artifacts .ModuleVersionIdentifier ;
14
+ import org .gradle .api .artifacts .ArtifactCollection ;
18
15
import org .gradle .api .artifacts .ProjectDependency ;
16
+ import org .gradle .api .artifacts .component .ModuleComponentIdentifier ;
17
+ import org .gradle .api .file .ConfigurableFileCollection ;
19
18
import org .gradle .api .file .DirectoryProperty ;
20
19
import org .gradle .api .file .ProjectLayout ;
21
20
import org .gradle .api .internal .ConventionTask ;
22
21
import org .gradle .api .model .ObjectFactory ;
22
+ import org .gradle .api .provider .MapProperty ;
23
+ import org .gradle .api .provider .Property ;
24
+ import org .gradle .api .provider .Provider ;
23
25
import org .gradle .api .provider .ProviderFactory ;
24
26
import org .gradle .api .tasks .Input ;
25
27
import org .gradle .api .tasks .InputDirectory ;
26
28
import org .gradle .api .tasks .InputFiles ;
29
+ import org .gradle .api .tasks .Internal ;
27
30
import org .gradle .api .tasks .Optional ;
28
31
import org .gradle .api .tasks .OutputFile ;
29
32
import org .gradle .api .tasks .TaskAction ;
34
37
import java .nio .file .StandardOpenOption ;
35
38
import java .util .Arrays ;
36
39
import java .util .LinkedHashMap ;
40
+ import java .util .Map ;
37
41
import java .util .Set ;
38
42
import java .util .regex .Pattern ;
39
43
import java .util .stream .Collectors ;
51
55
* <li>license: <a href="https://spdx.org/licenses/">SPDX license</a> identifier, custom license or UNKNOWN.</li>
52
56
* </ul>
53
57
*/
54
- public class DependenciesInfoTask extends ConventionTask {
58
+ public abstract class DependenciesInfoTask extends ConventionTask {
55
59
56
- private final DirectoryProperty licensesDir ;
60
+ @ Inject
61
+ public abstract ProviderFactory getProviderFactory ();
57
62
58
- @ OutputFile
59
- private File outputFile ;
63
+ /**
64
+ * We have to use ArtifactCollection instead of ResolvedArtifactResult here as we're running
65
+ * into a an issue in Gradle: https://github.com/gradle/gradle/issues/27582
66
+ */
60
67
61
- private LinkedHashMap <String , String > mappings ;
68
+ @ Internal
69
+ abstract Property <ArtifactCollection > getRuntimeArtifacts ();
62
70
63
- public Configuration getRuntimeConfiguration () {
64
- return runtimeConfiguration ;
71
+ @ Input
72
+ public Provider <Set <ModuleComponentIdentifier >> getRuntimeModules () {
73
+ return mapToModuleComponentIdentifiers (getRuntimeArtifacts ().get ());
65
74
}
66
75
67
- public void setRuntimeConfiguration (Configuration runtimeConfiguration ) {
68
- this .runtimeConfiguration = runtimeConfiguration ;
69
- }
76
+ @ Internal
77
+ abstract Property <ArtifactCollection > getCompileOnlyArtifacts ();
70
78
71
- public Configuration getCompileOnlyConfiguration () {
72
- return compileOnlyConfiguration ;
79
+ @ Input
80
+ public Provider <Set <ModuleComponentIdentifier >> getCompileOnlyModules () {
81
+ return mapToModuleComponentIdentifiers (getCompileOnlyArtifacts ().get ());
73
82
}
74
83
75
- public void setCompileOnlyConfiguration (Configuration compileOnlyConfiguration ) {
76
- this .compileOnlyConfiguration = compileOnlyConfiguration ;
84
+ /**
85
+ * We need to track file inputs here from the configurations we inspect to ensure we dont miss any
86
+ * artifact transforms that might be applied and fail due to missing task dependency to jar
87
+ * generating tasks.
88
+ * */
89
+ @ InputFiles
90
+ abstract ConfigurableFileCollection getClasspath ();
91
+
92
+ private Provider <Set <ModuleComponentIdentifier >> mapToModuleComponentIdentifiers (ArtifactCollection artifacts ) {
93
+ return getProviderFactory ().provider (
94
+ () -> artifacts .getArtifacts ()
95
+ .stream ()
96
+ .map (r -> r .getId ())
97
+ .filter (id -> id instanceof ModuleComponentIdentifier )
98
+ .map (id -> (ModuleComponentIdentifier ) id )
99
+ .collect (Collectors .toSet ())
100
+ );
77
101
}
78
102
103
+ private final DirectoryProperty licensesDir ;
104
+
105
+ @ OutputFile
106
+ private File outputFile ;
107
+
108
+ private LinkedHashMap <String , String > mappings ;
109
+
79
110
/**
80
111
* Directory to read license files
81
112
*/
@@ -102,17 +133,6 @@ public void setOutputFile(File outputFile) {
102
133
this .outputFile = outputFile ;
103
134
}
104
135
105
- /**
106
- * Dependencies to gather information from.
107
- */
108
- @ InputFiles
109
- private Configuration runtimeConfiguration ;
110
- /**
111
- * We subtract compile-only dependencies.
112
- */
113
- @ InputFiles
114
- private Configuration compileOnlyConfiguration ;
115
-
116
136
@ Inject
117
137
public DependenciesInfoTask (ProjectLayout projectLayout , ObjectFactory objectFactory , ProviderFactory providerFactory ) {
118
138
this .licensesDir = objectFactory .directoryProperty ();
@@ -123,22 +143,18 @@ public DependenciesInfoTask(ProjectLayout projectLayout, ObjectFactory objectFac
123
143
124
144
@ TaskAction
125
145
public void generateDependenciesInfo () throws IOException {
126
- final DependencySet runtimeDependencies = runtimeConfiguration .getAllDependencies ();
127
- // we have to resolve the transitive dependencies and create a group:artifactId:version map
128
-
129
- final Set <String > compileOnlyArtifacts = compileOnlyConfiguration .getResolvedConfiguration ()
130
- .getResolvedArtifacts ()
131
- .stream ()
132
- .map (r -> {
133
- ModuleVersionIdentifier id = r .getModuleVersion ().getId ();
134
- return id .getGroup () + ":" + id .getName () + ":" + id .getVersion ();
135
- })
136
- .collect (Collectors .toSet ());
137
146
147
+ final Set <String > compileOnlyIds = getCompileOnlyModules ().map (
148
+ set -> set .stream ()
149
+ .map (id -> id .getModuleIdentifier ().getGroup () + ":" + id .getModuleIdentifier ().getName () + ":" + id .getVersion ())
150
+ .collect (Collectors .toSet ())
151
+ ).get ();
138
152
final StringBuilder output = new StringBuilder ();
139
- for (final Dependency dep : runtimeDependencies ) {
153
+ Map <String , String > mappings = getMappings ().get ();
154
+ for (final ModuleComponentIdentifier dep : getRuntimeModules ().get ()) {
140
155
// we do not need compile-only dependencies here
141
- if (compileOnlyArtifacts .contains (dep .getGroup () + ":" + dep .getName () + ":" + dep .getVersion ())) {
156
+ String moduleName = dep .getModuleIdentifier ().getName ();
157
+ if (compileOnlyIds .contains (dep .getGroup () + ":" + moduleName + ":" + dep .getVersion ())) {
142
158
continue ;
143
159
}
144
160
@@ -147,25 +163,20 @@ public void generateDependenciesInfo() throws IOException {
147
163
continue ;
148
164
}
149
165
150
- final String url = createURL (dep .getGroup (), dep . getName () , dep .getVersion ());
151
- final String dependencyName = DependencyLicensesTask .getDependencyName (getMappings (), dep . getName () );
152
- getLogger ().info ("mapped dependency " + dep .getGroup () + ":" + dep . getName () + " to " + dependencyName + " for license info" );
166
+ final String url = createURL (dep .getGroup (), moduleName , dep .getVersion ());
167
+ final String dependencyName = DependencyLicensesTask .getDependencyName (mappings , moduleName );
168
+ getLogger ().info ("mapped dependency " + dep .getGroup () + ":" + moduleName + " to " + dependencyName + " for license info" );
153
169
154
170
final String licenseType = getLicenseType (dep .getGroup (), dependencyName );
155
- output .append (dep .getGroup () + ":" + dep . getName () + "," + dep .getVersion () + "," + url + "," + licenseType + "\n " );
171
+ output .append (dep .getGroup () + ":" + moduleName + "," + dep .getVersion () + "," + url + "," + licenseType + "\n " );
156
172
}
157
173
158
174
Files .write (outputFile .toPath (), output .toString ().getBytes ("UTF-8" ), StandardOpenOption .CREATE );
159
175
}
160
176
161
177
@ Input
162
- public LinkedHashMap <String , String > getMappings () {
163
- return mappings ;
164
- }
165
-
166
- public void setMappings (LinkedHashMap <String , String > mappings ) {
167
- this .mappings = mappings ;
168
- }
178
+ @ Optional
179
+ public abstract MapProperty <String , String > getMappings ();
169
180
170
181
/**
171
182
* Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a>
0 commit comments