All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xbib.gradle.task.elasticsearch.build.DependenciesInfoTask.groovy Maven / Gradle / Ivy

Go to download

Gradle plugins for the developer kit for building and testing Elasticsearch and Elasticsearch plugins

The newest version!
package org.xbib.gradle.task.elasticsearch.build

import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.DependencySet
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction


/**
 * A task to gather information about the dependencies and export them into a csv file.
 *
 * The following information is gathered:
 * 
    *
  • name: name that identifies the library (groupId:artifactId)
  • *
  • version
  • *
  • URL: link to have more information about the dependency.
  • *
  • license: SPDX license identifier, custom license or UNKNOWN.
  • *
* */ class DependenciesInfoTask extends DefaultTask { /** Dependencies to gather information from. */ @Input DependencySet dependencies /** Directory to read license files */ @InputDirectory File licensesDir = new File(project.projectDir, 'licenses') @OutputFile File outputFile = new File(project.buildDir, "reports/dependencies/dependencies.csv") DependenciesInfoTask() { description = 'Create a CSV file with dependencies information.' } @TaskAction void generateDependenciesInfo() { final StringBuilder output = new StringBuilder() for (Dependency dependency : dependencies) { // Only external dependencies are checked if (dependency.group != null && !dependency.group.contains("elasticsearch")) { final String url = createURL(dependency.group, dependency.name, dependency.version) final String licenseType = getLicenseType(dependency.group, dependency.name) output.append("${dependency.group}:${dependency.name},${dependency.version},${url},${licenseType}\n") } } outputFile.setText(output.toString(), 'UTF-8') } /** * Create an URL on Maven Central * based on dependency coordinates. */ protected String createURL(final String group, final String name, final String version){ final String baseURL = 'https://repo1.maven.org/maven2' return "${baseURL}/${group.replaceAll('\\.' , '/')}/${name}/${version}" } /** * Read the LICENSE file associated with the dependency and determine a license type. * * The license type is one of the following values: * *
  • UNKNOWN if LICENSE file is not present for this dependency.
  • *
  • one SPDX identifier if the LICENSE content matches with an SPDX license.
  • *
  • Custom;URL if it's not an SPDX license, * URL is the Github URL to the LICENSE file in elasticsearch repository.
  • * * * @param group dependency group * @param name dependency name * @return SPDX identifier, UNKNOWN or a Custom license */ protected String getLicenseType(final String group, final String name) { File license if (licensesDir.exists()) { licensesDir.eachFileMatch({ it ==~ /.*-LICENSE.*/ }) { File file -> String prefix = file.name.split('-LICENSE.*')[0] if (group.contains(prefix) || name.contains(prefix)) { license = file.getAbsoluteFile() } } } if (license) { final String content = license.readLines("UTF-8").toString() final String spdx = checkSPDXLicense(content) if (spdx == null) { // License has not be identified as SPDX. // As we have the license file, we create a Custom entry with the URL to this license file. final gitBranch = System.getProperty('build.branch', 'master') final String githubBaseURL = "https://raw.githubusercontent.com/elastic/elasticsearch/${gitBranch}/" return "Custom;${license.getCanonicalPath().replaceFirst('.*/elasticsearch/', githubBaseURL)}" } return spdx } else { return "UNKNOWN" } } /** * Check the license content to identify an SPDX license type. * * @param licenseText LICENSE file content. * @return SPDX identifier or null. */ private String checkSPDXLicense(final String licenseText) { String spdx = null final String APACHE_2_0 = "Apache.*License.*(v|V)ersion 2.0" final String BSD_2 = "BSD 2-clause.*License" final String CDDL_1_0 = "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.0" final String CDDL_1_1 = "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.1" final String ICU = "ICU License - ICU 1.8.1 and later" final String LGPL_3 = "GNU LESSER GENERAL PUBLIC LICENSE.*Version 3" final String MIT = "MIT License" final String MOZILLA_1_1 = "Mozilla Public License.*Version 1.1" switch (licenseText) { case ~/.*${APACHE_2_0}.*/: spdx = 'Apache-2.0' break case ~/.*${MIT}.*/: spdx = 'MIT' break case ~/.*${BSD_2}.*/: spdx = 'BSD-2-Clause' break case ~/.*${LGPL_3}.*/: spdx = 'LGPL-3.0' break case ~/.*${CDDL_1_0}.*/: spdx = 'CDDL-1.0' break case ~/.*${CDDL_1_1}.*/: spdx = 'CDDL-1.1' break case ~/.*${ICU}.*/: spdx = 'ICU' break case ~/.*${MOZILLA_1_1}.*/: spdx = 'MPL-1.1' break default: break } return spdx } }




    © 2015 - 2025 Weber Informatics LLC | Privacy Policy