1919
2020package org.elasticsearch.gradle
2121
22+ import org.elasticsearch.gradle.precommit.DependencyLicensesTask
2223import org.gradle.api.DefaultTask
24+ import org.gradle.api.artifacts.Configuration
2325import org.gradle.api.artifacts.Dependency
26+ import org.gradle.api.artifacts.DependencyResolutionListener
2427import org.gradle.api.artifacts.DependencySet
2528import org.gradle.api.tasks.Input
2629import org.gradle.api.tasks.InputDirectory
2730import org.gradle.api.tasks.OutputFile
2831import org.gradle.api.tasks.TaskAction
2932
33+ import java.util.regex.Matcher
34+ import java.util.regex.Pattern
3035
3136/**
3237 * A task to gather information about the dependencies and export them into a csv file.
@@ -44,7 +49,14 @@ public class DependenciesInfoTask extends DefaultTask {
4449
4550 /* * Dependencies to gather information from. */
4651 @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
4860
4961 /* * Directory to read license files */
5062 @InputDirectory
@@ -59,15 +71,34 @@ public class DependenciesInfoTask extends DefaultTask {
5971
6072 @TaskAction
6173 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+
6283 final StringBuilder output = new StringBuilder ()
6384
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
7089 }
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+
71102 }
72103 outputFile. setText(output. toString(), ' UTF-8' )
73104 }
@@ -109,7 +140,8 @@ public class DependenciesInfoTask extends DefaultTask {
109140 }
110141
111142 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(" \\ *" , " " )
113145 final String spdx = checkSPDXLicense(content)
114146 if (spdx == null ) {
115147 // License has not be identified as SPDX.
@@ -133,15 +165,88 @@ public class DependenciesInfoTask extends DefaultTask {
133165 private String checkSPDXLicense (final String licenseText ) {
134166 String spdx = null
135167
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+
138221 final String CDDL_1_0 = " COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.0"
139222 final String CDDL_1_1 = " COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.1"
140223 final String ICU = " ICU License - ICU 1.8.1 and later"
141224 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+
143246 final String MOZILLA_1_1 = " Mozilla Public License.*Version 1.1"
144247
248+ final String MOZILLA_2_0 = " Mozilla\\ s*Public\\ s*License\\ s*Version\\ s*2\\ .0"
249+
145250 switch (licenseText) {
146251 case ~/ .*${APACHE_2_0}.*/ :
147252 spdx = ' Apache-2.0'
@@ -152,6 +257,9 @@ public class DependenciesInfoTask extends DefaultTask {
152257 case ~/ .*${BSD_2}.*/ :
153258 spdx = ' BSD-2-Clause'
154259 break
260+ case ~/ .*${BSD_3}.*/ :
261+ spdx = ' BSD-3-Clause'
262+ break
155263 case ~/ .*${LGPL_3}.*/ :
156264 spdx = ' LGPL-3.0'
157265 break
@@ -167,6 +275,9 @@ public class DependenciesInfoTask extends DefaultTask {
167275 case ~/ .*${MOZILLA_1_1}.*/ :
168276 spdx = ' MPL-1.1'
169277 break
278+ case ~/ .*${MOZILLA_2_0}.*/ :
279+ spdx = ' MPL-2.0'
280+ break
170281 default :
171282 break
172283 }
0 commit comments