|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.gradle |
| 21 | + |
| 22 | +import org.gradle.api.DefaultTask |
| 23 | +import org.gradle.api.artifacts.Dependency |
| 24 | +import org.gradle.api.artifacts.DependencySet |
| 25 | +import org.gradle.api.tasks.Input |
| 26 | +import org.gradle.api.tasks.InputDirectory |
| 27 | +import org.gradle.api.tasks.OutputFile |
| 28 | +import org.gradle.api.tasks.TaskAction |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * A task to gather information about the dependencies and export them into a csv file. |
| 33 | + * |
| 34 | + * The following information is gathered: |
| 35 | + * <ul> |
| 36 | + * <li>name: name that identifies the library (groupId:artifactId)</li> |
| 37 | + * <li>version</li> |
| 38 | + * <li>URL: link to have more information about the dependency.</li> |
| 39 | + * <li>license: <a href="https://spdx.org/licenses/">SPDX license</a> identifier, custom license or UNKNOWN.</li> |
| 40 | + * </ul> |
| 41 | + * |
| 42 | + */ |
| 43 | +public class DependenciesInfoTask extends DefaultTask { |
| 44 | + |
| 45 | + /** Dependencies to gather information from. */ |
| 46 | + @Input |
| 47 | + public DependencySet dependencies |
| 48 | + |
| 49 | + /** Directory to read license files */ |
| 50 | + @InputDirectory |
| 51 | + public File licensesDir = new File(project.projectDir, 'licenses') |
| 52 | + |
| 53 | + @OutputFile |
| 54 | + File outputFile = new File(project.buildDir, "reports/dependencies/dependencies.csv") |
| 55 | + |
| 56 | + public DependenciesInfoTask() { |
| 57 | + description = 'Create a CSV file with dependencies information.' |
| 58 | + } |
| 59 | + |
| 60 | + @TaskAction |
| 61 | + public void generateDependenciesInfo() { |
| 62 | + final StringBuilder output = new StringBuilder() |
| 63 | + |
| 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") |
| 70 | + } |
| 71 | + } |
| 72 | + outputFile.setText(output.toString(), 'UTF-8') |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a> |
| 77 | + * based on dependency coordinates. |
| 78 | + */ |
| 79 | + protected String createURL(final String group, final String name, final String version){ |
| 80 | + final String baseURL = 'https://repo1.maven.org/maven2' |
| 81 | + return "${baseURL}/${group.replaceAll('\\.' , '/')}/${name}/${version}" |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Read the LICENSE file associated with the dependency and determine a license type. |
| 86 | + * |
| 87 | + * The license type is one of the following values: |
| 88 | + * <u> |
| 89 | + * <li><em>UNKNOWN</em> if LICENSE file is not present for this dependency.</li> |
| 90 | + * <li><em>one SPDX identifier</em> if the LICENSE content matches with an SPDX license.</li> |
| 91 | + * <li><em>Custom:URL</em> if it's not an SPDX license, |
| 92 | + * URL is the Github URL to the LICENSE file in elasticsearch repository.</li> |
| 93 | + * </ul> |
| 94 | + * |
| 95 | + * @param group dependency group |
| 96 | + * @param name dependency name |
| 97 | + * @return SPDX identifier, UNKNOWN or a Custom license |
| 98 | + */ |
| 99 | + protected String getLicenseType(final String group, final String name) { |
| 100 | + File license |
| 101 | + |
| 102 | + if (licensesDir.exists()) { |
| 103 | + licensesDir.eachFileMatch({ it ==~ /.*-LICENSE.*/ }) { File file -> |
| 104 | + String prefix = file.name.split('-LICENSE.*')[0] |
| 105 | + if (group.contains(prefix) || name.contains(prefix)) { |
| 106 | + license = file.getAbsoluteFile() |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (license) { |
| 112 | + final String content = license.readLines("UTF-8").toString() |
| 113 | + final String spdx = checkSPDXLicense(content) |
| 114 | + if (spdx == null) { |
| 115 | + // License has not be identified as SPDX. |
| 116 | + // As we have the license file, we create a Custom entry with the URL to this license file. |
| 117 | + final gitBranch = System.getProperty('build.branch', 'master') |
| 118 | + final String githubBaseURL = "https://raw.githubusercontent.com/elastic/elasticsearch/${gitBranch}/" |
| 119 | + return "Custom:${license.getCanonicalPath().replaceFirst('.*/elasticsearch/', githubBaseURL)}" |
| 120 | + } |
| 121 | + return spdx |
| 122 | + } else { |
| 123 | + return "UNKNOWN" |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check the license content to identify an SPDX license type. |
| 129 | + * |
| 130 | + * @param licenseText LICENSE file content. |
| 131 | + * @return SPDX identifier or null. |
| 132 | + */ |
| 133 | + private String checkSPDXLicense(final String licenseText) { |
| 134 | + String spdx = null |
| 135 | + |
| 136 | + final String APACHE_2_0 = "Apache.*License.*(v|V)ersion 2.0" |
| 137 | + final String BSD_2 = "BSD 2-clause.*License" |
| 138 | + final String CDDL_1_0 = "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.0" |
| 139 | + final String CDDL_1_1 = "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.1" |
| 140 | + final String ICU = "ICU License - ICU 1.8.1 and later" |
| 141 | + final String LGPL_3 = "GNU LESSER GENERAL PUBLIC LICENSE.*Version 3" |
| 142 | + final String MIT = "MIT License" |
| 143 | + final String MOZILLA_1_1 = "Mozilla Public License.*Version 1.1" |
| 144 | + |
| 145 | + switch (licenseText) { |
| 146 | + case ~/.*${APACHE_2_0}.*/: |
| 147 | + spdx = 'Apache-2.0' |
| 148 | + break |
| 149 | + case ~/.*${MIT}.*/: |
| 150 | + spdx = 'MIT' |
| 151 | + break |
| 152 | + case ~/.*${BSD_2}.*/: |
| 153 | + spdx = 'BSD-2-Clause' |
| 154 | + break |
| 155 | + case ~/.*${LGPL_3}.*/: |
| 156 | + spdx = 'LGPL-3.0' |
| 157 | + break |
| 158 | + case ~/.*${CDDL_1_0}.*/: |
| 159 | + spdx = 'CDDL_1_0' |
| 160 | + break |
| 161 | + case ~/.*${CDDL_1_1}.*/: |
| 162 | + spdx = 'CDDL_1_1' |
| 163 | + break |
| 164 | + case ~/.*${ICU}.*/: |
| 165 | + spdx = 'ICU' |
| 166 | + break |
| 167 | + case ~/.*${MOZILLA_1_1}.*/: |
| 168 | + spdx = 'MPL-1.1' |
| 169 | + break |
| 170 | + default: |
| 171 | + break |
| 172 | + } |
| 173 | + return spdx |
| 174 | + } |
| 175 | +} |
0 commit comments