Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ licenseReport {
// This is for the allowed-licenses-file in checkLicense Task
// Accepts File, URL or String path to local or remote file
allowedLicensesFile = project.layout.projectDirectory.file("config/allowed-licenses.json").asFile

// If set to true, the plugin will not fail the build if no licenses are found
// simply to support quite old libraries that do not provide any license information.
allowEmptyLicense = false
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'com.github.jk1'
version = '2.9'
version = '2.11'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class LicenseReportExtension {
public String[] excludeGroups
public String[] excludes
public Object allowedLicensesFile
public boolean allowEmptyLicense

LicenseReportExtension(Project project) {
unionParentPomLicenses = true
Expand All @@ -54,6 +55,7 @@ class LicenseReportExtension {
excludes = []
importers = []
filters = []
allowEmptyLicense = false
}

@Nested
Expand All @@ -71,6 +73,11 @@ class LicenseReportExtension {
return filters
}

@Input
boolean getAllowEmptyLicense() {
return allowEmptyLicense
}

@Internal
String getAbsoluteOutputDir(){
if (new File(outputDir).isAbsolute()) {
Expand Down Expand Up @@ -101,6 +108,8 @@ class LicenseReportExtension {
snapshot += excludeGroups
snapshot << 'excludes'
snapshot += excludes
snapshot << 'allowEmptyLicenses'
snapshot += allowEmptyLicense
snapshot << 'unionParentPomLicenses'
snapshot += unionParentPomLicenses
snapshot.join("!")
Expand Down
19 changes: 16 additions & 3 deletions src/main/groovy/com/github/jk1/license/check/LicenseChecker.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import groovy.json.JsonOutput
import org.gradle.api.GradleException

class LicenseChecker {
boolean allowEmptyLicense

LicenseChecker(boolean allowEmpty) {
this.allowEmptyLicense = allowEmpty
}

void checkAllDependencyLicensesAreAllowed(
Object allowedLicensesFile, File projectLicensesDataFile, File notPassedDependenciesOutputFile) {
Expand Down Expand Up @@ -53,9 +58,13 @@ class LicenseChecker {
}

private boolean isDependencyMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) {
return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
return isEmptyLicense(dependency) || (isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) &&
isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense)
isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense))
}

private boolean isEmptyLicense(Dependency dependency) {
return dependency.moduleLicenses.empty && allowEmptyLicense
}

private boolean isDependencyNameMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) {
Expand All @@ -73,7 +82,11 @@ class LicenseChecker {

for (moduleLicenses in dependency.moduleLicenses)
if (moduleLicenses.moduleLicense ==~ allowedLicense.moduleLicense ||
moduleLicenses.moduleLicense == allowedLicense.moduleLicense) return true
moduleLicenses.moduleLicense == allowedLicense.moduleLicense ||
moduleLicenses.moduleLicense == null && allowedLicense )
{
return true
}
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ class ManifestReader {
def additionalParameter = it.split('=')

if (additionalParameter[0] == 'description')
data.license = additionalParameter[1]
data.license = additionalParameter[1]?.trim()
}
}
else {
data.license = bundleLicense
data.license = bundleLicense?.trim()
}
LOGGER.info("Returning manifest data: " + data.dump())
return data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ class PomReader {
}
licensePoms.each { pom ->
pom.licenses?.license?.each { GPathResult license ->
LOGGER.debug("Processing license: ${license.name.text()}")
LOGGER.debug("Processing license: ${license.name.text()} will be trimmed if needed")
pomData.licenses << new License(
name: license.name?.text(),
name: license.name?.text()?.trim(),
url: license.url?.text()
)
}
Expand All @@ -238,9 +238,9 @@ class PomReader {
if ( !pomData.licenses ) {
childPoms.each { pom ->
pom.licenses?.license?.each { GPathResult license ->
LOGGER.debug("Processing license: ${license.name.text()}")
LOGGER.debug("Processing license: ${license.name.text()} will be trimmed if needed")
pomData.licenses << new License(
name: license.name?.text(),
name: license.name?.text()?.trim(),
url: license.url?.text()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CheckLicenseTask extends DefaultTask {
@TaskAction
void checkLicense() {
LOGGER.info("Startup CheckLicense for ${config.projects.first()}")
LicenseChecker licenseChecker = new LicenseChecker()
LicenseChecker licenseChecker = new LicenseChecker(config.allowEmptyLicense)
LOGGER.info("Check licenses if they are allowed to use.")
licenseChecker.checkAllDependencyLicensesAreAllowed(
getAllowedLicenseFile(), getProjectDependenciesData(), notPassedDependenciesFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class CheckLicenseTaskSpec extends Specification {
{
"moduleLicense": "MIT License"
},
{
"moduleLicense": "CDDL License"
},
{
"moduleLicense": ".*", "moduleName": "org.jetbrains"
}
Expand Down Expand Up @@ -680,6 +683,128 @@ class CheckLicenseTaskSpec extends Specification {
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
}

def "it should pass when empty license and allowEmptyLicense set to true"() {
given:
buildFile << """
import com.github.jk1.license.filter.*

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
id 'com.github.jk1.dependency-license-report'
}

apply plugin: 'java'

group 'greeting'
version '0.0.1'

repositories {
mavenCentral()
}

dependencies {
implementation group: "javax.xml", name: "jaxrpc-api", version: "1.1"
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
licenseReport {
filters = new LicenseBundleNormalizer()
allowEmptyLicense = true
allowedLicensesFile = new File("${StringEscapeUtils.escapeJava(allowed.path)}")
}
"""

when:
BuildResult buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.SUCCESS

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE

when:
buildResult = result("--build-cache", "clean", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.FROM_CACHE

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
}

def "it should trim and record license without whitespaces around"() {
given:
buildFile << """
import com.github.jk1.license.filter.*

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
id 'com.github.jk1.dependency-license-report'
}

apply plugin: 'java'

group 'greeting'
version '0.0.1'

repositories {
mavenCentral()
}

dependencies {
implementation group: "javax.ws.rs", name: "jsr311-api", version: "1.1.1"
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
licenseReport {
filters = new LicenseBundleNormalizer()
allowEmptyLicense = true
allowedLicensesFile = new File("${StringEscapeUtils.escapeJava(allowed.path)}")
}
"""

when:
BuildResult buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.SUCCESS

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE

when:
buildResult = result("--build-cache", "clean", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.FROM_CACHE

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
}

@Ignore // https://github.com/jk1/Gradle-License-Report/issues/255
def "using it with configuration cache should not cause the build to fail"() {
given:
Expand Down