19
19
20
20
package org.elasticsearch.gradle
21
21
22
+ import org.elasticsearch.gradle.precommit.DependencyLicensesTask
22
23
import org.gradle.api.DefaultTask
24
+ import org.gradle.api.artifacts.Configuration
23
25
import org.gradle.api.artifacts.Dependency
26
+ import org.gradle.api.artifacts.DependencyResolutionListener
24
27
import org.gradle.api.artifacts.DependencySet
25
28
import org.gradle.api.tasks.Input
26
29
import org.gradle.api.tasks.InputDirectory
27
30
import org.gradle.api.tasks.OutputFile
28
31
import org.gradle.api.tasks.TaskAction
29
32
33
+ import java.util.regex.Matcher
34
+ import java.util.regex.Pattern
30
35
31
36
/**
32
37
* A task to gather information about the dependencies and export them into a csv file.
@@ -44,7 +49,14 @@ public class DependenciesInfoTask extends DefaultTask {
44
49
45
50
/* * Dependencies to gather information from. */
46
51
@Input
47
- public DependencySet dependencies
52
+ public Configuration runtimeConfiguration
53
+
54
+ /* * We subtract compile-only dependencies. */
55
+ @Input
56
+ public Configuration compileOnlyConfiguration
57
+
58
+ @Input
59
+ public LinkedHashMap<String , String > mappings
48
60
49
61
/* * Directory to read license files */
50
62
@InputDirectory
@@ -59,15 +71,34 @@ public class DependenciesInfoTask extends DefaultTask {
59
71
60
72
@TaskAction
61
73
public void generateDependenciesInfo () {
74
+
75
+ final DependencySet runtimeDependencies = runtimeConfiguration. getAllDependencies()
76
+ // we have to resolve the transitive dependencies and create a group:artifactId:version map
77
+ final Set<String > compileOnlyArtifacts =
78
+ compileOnlyConfiguration
79
+ .getResolvedConfiguration()
80
+ .resolvedArtifacts
81
+ .collect { it -> " ${ it.moduleVersion.id.group} :${ it.moduleVersion.id.name} :${ it.moduleVersion.id.version} " }
82
+
62
83
final StringBuilder output = new StringBuilder ()
63
84
64
- for (Dependency dependency : dependencies) {
65
- // Only external dependencies are checked
66
- if (dependency. group != null && dependency. group. contains(" elasticsearch" ) == false ) {
67
- final String url = createURL(dependency. group, dependency. name, dependency. version)
68
- final String licenseType = getLicenseType(dependency. group, dependency. name)
69
- output. append(" ${ dependency.group} :${ dependency.name} ,${ dependency.version} ,${ url} ,${ licenseType} \n " )
85
+ for (final Dependency dependency : runtimeDependencies) {
86
+ // we do not need compile-only dependencies here
87
+ if (compileOnlyArtifacts. contains(" ${ dependency.group} :${ dependency.name} :${ dependency.version} " )) {
88
+ continue
70
89
}
90
+ // only external dependencies are checked
91
+ if (dependency. group != null && dependency. group. contains(" org.elasticsearch" )) {
92
+ continue
93
+ }
94
+
95
+ final String url = createURL(dependency. group, dependency. name, dependency. version)
96
+ final String dependencyName = DependencyLicensesTask . getDependencyName(mappings, dependency. name)
97
+ logger. info(" mapped dependency ${ dependency.group} :${ dependency.name} to ${ dependencyName} for license info" )
98
+
99
+ final String licenseType = getLicenseType(dependency. group, dependencyName)
100
+ output. append(" ${ dependency.group} :${ dependency.name} ,${ dependency.version} ,${ url} ,${ licenseType} \n " )
101
+
71
102
}
72
103
outputFile. setText(output. toString(), ' UTF-8' )
73
104
}
@@ -109,7 +140,8 @@ public class DependenciesInfoTask extends DefaultTask {
109
140
}
110
141
111
142
if (license) {
112
- final String content = license. readLines(" UTF-8" ). toString()
143
+ // replace * because they are sometimes used at the beginning lines as if the license was a multi-line comment
144
+ final String content = new String (license. readBytes(), " UTF-8" ). replaceAll(" \\ s+" , " " ). replaceAll(" \\ *" , " " )
113
145
final String spdx = checkSPDXLicense(content)
114
146
if (spdx == null ) {
115
147
// License has not be identified as SPDX.
@@ -133,15 +165,88 @@ public class DependenciesInfoTask extends DefaultTask {
133
165
private String checkSPDXLicense (final String licenseText ) {
134
166
String spdx = null
135
167
136
- final String APACHE_2_0 = " Apache.*License.*(v|V)ersion 2.0"
137
- final String BSD_2 = " BSD 2-clause.*License"
168
+ final String APACHE_2_0 = " Apache.*License.*(v|V)ersion.*2\\ .0"
169
+
170
+ final String BSD_2 = """
171
+ Redistribution and use in source and binary forms, with or without
172
+ modification, are permitted provided that the following conditions
173
+ are met:
174
+
175
+ 1\\ . Redistributions of source code must retain the above copyright
176
+ notice, this list of conditions and the following disclaimer\\ .
177
+ 2\\ . Redistributions in binary form must reproduce the above copyright
178
+ notice, this list of conditions and the following disclaimer in the
179
+ documentation and/or other materials provided with the distribution\\ .
180
+
181
+ THIS SOFTWARE IS PROVIDED BY .+ (``|''|")AS IS(''|") AND ANY EXPRESS OR
182
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\ .
184
+ IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT,
185
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\ (INCLUDING, BUT
186
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
187
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\ ) HOWEVER CAUSED AND ON ANY
188
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
189
+ \\ (INCLUDING NEGLIGENCE OR OTHERWISE\\ ) ARISING IN ANY WAY OUT OF THE USE OF
190
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\ .
191
+ """ . replaceAll(" \\ s+" , " \\\\ s*" )
192
+
193
+ final String BSD_3 = """
194
+ Redistribution and use in source and binary forms, with or without
195
+ modification, are permitted provided that the following conditions
196
+ are met:
197
+
198
+ (1\\ .)? Redistributions of source code must retain the above copyright
199
+ notice, this list of conditions and the following disclaimer\\ .
200
+ (2\\ .)? Redistributions in binary form must reproduce the above copyright
201
+ notice, this list of conditions and the following disclaimer in the
202
+ documentation and/or other materials provided with the distribution\\ .
203
+ ((3\\ .)? The name of .+ may not be used to endorse or promote products
204
+ derived from this software without specific prior written permission\\ .|
205
+ (3\\ .)? Neither the name of .+ nor the names of its
206
+ contributors may be used to endorse or promote products derived from
207
+ this software without specific prior written permission\\ .)
208
+
209
+ THIS SOFTWARE IS PROVIDED BY .+ (``|''|")AS IS(''|") AND ANY EXPRESS OR
210
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
211
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\ .
212
+ IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT,
213
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\ (INCLUDING, BUT
214
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
215
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\ ) HOWEVER CAUSED AND ON ANY
216
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217
+ \\ (INCLUDING NEGLIGENCE OR OTHERWISE\\ ) ARISING IN ANY WAY OUT OF THE USE OF
218
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\ .
219
+ """ . replaceAll(" \\ s+" , " \\\\ s*" )
220
+
138
221
final String CDDL_1_0 = " COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.0"
139
222
final String CDDL_1_1 = " COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.1"
140
223
final String ICU = " ICU License - ICU 1.8.1 and later"
141
224
final String LGPL_3 = " GNU LESSER GENERAL PUBLIC LICENSE.*Version 3"
142
- final String MIT = " MIT License"
225
+
226
+ final String MIT = """
227
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
228
+ this software and associated documentation files \\ (the "Software"\\ ), to deal in
229
+ the Software without restriction, including without limitation the rights to
230
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
231
+ of the Software, and to permit persons to whom the Software is furnished to do
232
+ so, subject to the following conditions:
233
+
234
+ The above copyright notice and this permission notice shall be included in all
235
+ copies or substantial portions of the Software\\ .
236
+
237
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
239
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\\ . IN NO EVENT SHALL THE
240
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
241
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
242
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
243
+ SOFTWARE\\ .
244
+ """ . replaceAll(" \\ s+" , " \\\\ s*" )
245
+
143
246
final String MOZILLA_1_1 = " Mozilla Public License.*Version 1.1"
144
247
248
+ final String MOZILLA_2_0 = " Mozilla\\ s*Public\\ s*License\\ s*Version\\ s*2\\ .0"
249
+
145
250
switch (licenseText) {
146
251
case ~/ .*${APACHE_2_0}.*/ :
147
252
spdx = ' Apache-2.0'
@@ -152,6 +257,9 @@ public class DependenciesInfoTask extends DefaultTask {
152
257
case ~/ .*${BSD_2}.*/ :
153
258
spdx = ' BSD-2-Clause'
154
259
break
260
+ case ~/ .*${BSD_3}.*/ :
261
+ spdx = ' BSD-3-Clause'
262
+ break
155
263
case ~/ .*${LGPL_3}.*/ :
156
264
spdx = ' LGPL-3.0'
157
265
break
@@ -167,6 +275,9 @@ public class DependenciesInfoTask extends DefaultTask {
167
275
case ~/ .*${MOZILLA_1_1}.*/ :
168
276
spdx = ' MPL-1.1'
169
277
break
278
+ case ~/ .*${MOZILLA_2_0}.*/ :
279
+ spdx = ' MPL-2.0'
280
+ break
170
281
default :
171
282
break
172
283
}
0 commit comments