Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #4830] Generate LICENSE and NOTICE with Gradle tasks #4831

Merged
merged 19 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
116 changes: 108 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
*/

import java.util.concurrent.TimeUnit


import groovy.json.JsonSlurper

buildscript {
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://maven.aliyun.com/repository/public"
}

maven {
url "https://plugins.gradle.org/m2/"
}
Expand All @@ -35,11 +32,15 @@ buildscript {
dependencies {
classpath "com.github.spotbugs.snom:spotbugs-gradle-plugin:5.0.14"
classpath "io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE"
classpath "com.github.jk1:gradle-license-report:1.17"
classpath "com.diffplug.spotless:spotless-plugin-gradle:6.13.0"
}
}

plugins {
id 'org.cyclonedx.bom' version '1.8.2'
id 'com.github.jk1.dependency-license-report' version '2.6'
}
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved

// Remove doclint warnings that pollute javadoc logs when building
if (JavaVersion.current().isJava8()) {
allprojects {
Expand Down Expand Up @@ -96,7 +97,6 @@ allprojects {

dependencies {
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://maven.aliyun.com/repository/public"
Expand Down Expand Up @@ -212,6 +212,107 @@ task printProjects() {
})
}

cyclonedxBom {
includeConfigs = ["runtimeClasspath"]
}

// TODO depend 'dist' on 'generateDistLicense'
tasks.register('generateDistLicense') {
dependsOn('cyclonedxBom') // Task from 'org.cyclonedx.bom' plugin
doLast {
// Inputs
def bomFile = file('build/reports/bom.json')
def bom = new JsonSlurper().parseText(bomFile.text)
def projectLicenseText = file('LICENSE').text

// Outputs
def distLicenseFile = file('tools/dist-license/LICENSE')
def licensesDir = file('tools/dist-license/licenses/java/')
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved
if (licensesDir.exists()) {
licensesDir.eachFile { it.delete() }
} else {
licensesDir.mkdirs()
}

List<Map<String, String>> thirdPartyArtifacts = new ArrayList<Map<String, String>>()
// Parse BOM
bom.components.each { component ->
// Exclude project modules
if (!component.group.startsWith('org.apache.eventmesh')) {
component.licenses.each { artifactLicense ->
if (artifactLicense.license != null) {
Map<String, String> artifact = new HashMap<String, String>()
artifact.put("name", component.name)
artifact.put("version", component.version)
if (artifactLicense.license.id != null) {
artifact.put("license", artifactLicense.license.id)
if (artifactLicense.license.text != null) {
artifact.put("licenseContent", new String(artifactLicense.license.text.content.decodeBase64()))
}
} else {
artifact.put("license", artifactLicense.license.name)
artifact.put("licenseContent", artifactLicense.license.url)
}
thirdPartyArtifacts.add(artifact)
}
}
}
}
thirdPartyArtifacts.sort { a, b ->
def nameComparison = a.name <=> b.name
if (nameComparison == 0) {
return a.version <=> b.version
} else {
return nameComparison
}
}

def distLicenseText = projectLicenseText + "\n=======================================================================\n" +
"This distribution contains the following third-party:\n\n"
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved
thirdPartyArtifacts.each { artifact ->
// Write licenses
def artifactLicenseFilename = artifact.license.replaceAll("/", "-") + ".txt"
def artifactLicenseFile = new File(licensesDir, artifactLicenseFilename)
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved
if (artifact.licenseContent != null) {
artifactLicenseFile.text = artifact.licenseContent
} else {
artifactLicenseFile.text = "No license content provided by the artifact."
Pil0tXia marked this conversation as resolved.
Show resolved Hide resolved
}

// Assemble LICENSE
distLicenseText += "${artifact.name} ${artifact.version} licensed under '${artifact.license}'. For details see: licenses/${artifactLicenseFilename}\n"
}
distLicenseFile.text = distLicenseText
}
}

// TODO depend 'dist' on 'generateDistNotice'
tasks.register('generateDistNotice') {
dependsOn('generateLicenseReport') // Task from 'com.github.jk1.dependency-license-report' plugin
doLast {
// Inputs
def reportsDir = file('build/reports/dependency-license/')
def projectNoticeText = file('NOTICE').text

// Outputs
def distNoticeFile = file('tools/dist-license/NOTICE')

def distNoticeText = projectNoticeText
reportsDir.eachDir { dir ->
dir.eachFileRecurse (groovy.io.FileType.FILES) { file ->
// Find NOTICE files
if (file.name.length() >= 6 && file.name.substring(0, 6).equalsIgnoreCase("NOTICE")) {
def artifactName = dir.name.replace(".jar", "")
distNoticeText += "\n=======================================================================\n\n" +
"${artifactName} NOTICE\n" + "\n=======================================================================\n\n"
distNoticeText += file.text
}
}
}
distNoticeFile.text = distNoticeText
}
}

subprojects {

apply plugin: "io.spring.dependency-management"
Expand Down Expand Up @@ -369,7 +470,7 @@ subprojects {
}
copy {
into "${rootDir}/dist"
from "${rootDir}/tools/third-party-licenses"
from "${rootDir}/tools/dist-license"
}
}
}
Expand Down Expand Up @@ -405,7 +506,6 @@ subprojects {
}

repositories {
mavenLocal()
mavenCentral()
maven { url "https://maven.aliyun.com/repository/public" }
}
Expand Down