Skip to content

Commit 1e09240

Browse files
author
Olgun Kaya
committed
Allow empty licenses for pretty old libraries
proper release number
1 parent 8da16c1 commit 1e09240

File tree

8 files changed

+162
-11
lines changed

8 files changed

+162
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ licenseReport {
8585
// This is for the allowed-licenses-file in checkLicense Task
8686
// Accepts File, URL or String path to local or remote file
8787
allowedLicensesFile = project.layout.projectDirectory.file("config/allowed-licenses.json").asFile
88+
89+
// If set to true, the plugin will not fail the build if no licenses are found
90+
// simply to support quite old libraries that do not provide any license information.
91+
allowEmptyLicense = false
8892
}
8993
```
9094

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = 'com.github.jk1'
9-
version = '2.9'
9+
version = '2.11'
1010

1111
sourceCompatibility = JavaVersion.VERSION_1_8
1212
targetCompatibility = JavaVersion.VERSION_1_8

src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LicenseReportExtension {
4141
public String[] excludeGroups
4242
public String[] excludes
4343
public Object allowedLicensesFile
44+
public boolean allowEmptyLicense
4445

4546
LicenseReportExtension(Project project) {
4647
unionParentPomLicenses = true
@@ -54,6 +55,7 @@ class LicenseReportExtension {
5455
excludes = []
5556
importers = []
5657
filters = []
58+
allowEmptyLicense = false
5759
}
5860

5961
@Nested
@@ -71,6 +73,11 @@ class LicenseReportExtension {
7173
return filters
7274
}
7375

76+
@Input
77+
boolean getAllowEmptyLicense() {
78+
return allowEmptyLicense
79+
}
80+
7481
@Internal
7582
String getAbsoluteOutputDir(){
7683
if (new File(outputDir).isAbsolute()) {
@@ -101,6 +108,8 @@ class LicenseReportExtension {
101108
snapshot += excludeGroups
102109
snapshot << 'excludes'
103110
snapshot += excludes
111+
snapshot << 'allowEmptyLicenses'
112+
snapshot += allowEmptyLicense
104113
snapshot << 'unionParentPomLicenses'
105114
snapshot += unionParentPomLicenses
106115
snapshot.join("!")

src/main/groovy/com/github/jk1/license/check/LicenseChecker.groovy

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import groovy.json.JsonOutput
1919
import org.gradle.api.GradleException
2020

2121
class LicenseChecker {
22+
boolean allowEmptyLicense
23+
24+
LicenseChecker(boolean allowEmpty) {
25+
this.allowEmptyLicense = allowEmpty
26+
}
2227

2328
void checkAllDependencyLicensesAreAllowed(
2429
Object allowedLicensesFile, File projectLicensesDataFile, File notPassedDependenciesOutputFile) {
@@ -53,9 +58,13 @@ class LicenseChecker {
5358
}
5459

5560
private boolean isDependencyMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) {
56-
return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
61+
return isEmptyLicense(dependency) || (isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
5762
isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) &&
58-
isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense)
63+
isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense))
64+
}
65+
66+
private boolean isEmptyLicense(Dependency dependency) {
67+
return dependency.moduleLicenses.empty && allowEmptyLicense
5968
}
6069

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

7483
for (moduleLicenses in dependency.moduleLicenses)
7584
if (moduleLicenses.moduleLicense ==~ allowedLicense.moduleLicense ||
76-
moduleLicenses.moduleLicense == allowedLicense.moduleLicense) return true
85+
moduleLicenses.moduleLicense == allowedLicense.moduleLicense ||
86+
moduleLicenses.moduleLicense == null && allowedLicense )
87+
{
88+
return true
89+
}
7790
return false
7891
}
7992

src/main/groovy/com/github/jk1/license/reader/ManifestReader.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ class ManifestReader {
9999
def additionalParameter = it.split('=')
100100

101101
if (additionalParameter[0] == 'description')
102-
data.license = additionalParameter[1]
102+
data.license = additionalParameter[1]?.trim()
103103
}
104104
}
105105
else {
106-
data.license = bundleLicense
106+
data.license = bundleLicense?.trim()
107107
}
108108
LOGGER.info("Returning manifest data: " + data.dump())
109109
return data

src/main/groovy/com/github/jk1/license/reader/PomReader.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ class PomReader {
227227
}
228228
licensePoms.each { pom ->
229229
pom.licenses?.license?.each { GPathResult license ->
230-
LOGGER.debug("Processing license: ${license.name.text()}")
230+
LOGGER.debug("Processing license: ${license.name.text()} will be trimmed if needed")
231231
pomData.licenses << new License(
232-
name: license.name?.text(),
232+
name: license.name?.text()?.trim(),
233233
url: license.url?.text()
234234
)
235235
}
@@ -238,9 +238,9 @@ class PomReader {
238238
if ( !pomData.licenses ) {
239239
childPoms.each { pom ->
240240
pom.licenses?.license?.each { GPathResult license ->
241-
LOGGER.debug("Processing license: ${license.name.text()}")
241+
LOGGER.debug("Processing license: ${license.name.text()} will be trimmed if needed")
242242
pomData.licenses << new License(
243-
name: license.name?.text(),
243+
name: license.name?.text()?.trim(),
244244
url: license.url?.text()
245245
)
246246
}

src/main/groovy/com/github/jk1/license/task/CheckLicenseTask.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CheckLicenseTask extends DefaultTask {
6464
@TaskAction
6565
void checkLicense() {
6666
LOGGER.info("Startup CheckLicense for ${config.projects.first()}")
67-
LicenseChecker licenseChecker = new LicenseChecker()
67+
LicenseChecker licenseChecker = new LicenseChecker(config.allowEmptyLicense)
6868
LOGGER.info("Check licenses if they are allowed to use.")
6969
licenseChecker.checkAllDependencyLicensesAreAllowed(
7070
getAllowedLicenseFile(), getProjectDependenciesData(), notPassedDependenciesFile)

src/test/groovy/com/github/jk1/license/check/CheckLicenseTaskSpec.groovy

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class CheckLicenseTaskSpec extends Specification {
9696
{
9797
"moduleLicense": "MIT License"
9898
},
99+
{
100+
"moduleLicense": "CDDL License"
101+
},
99102
{
100103
"moduleLicense": ".*", "moduleName": "org.jetbrains"
101104
}
@@ -680,6 +683,128 @@ class CheckLicenseTaskSpec extends Specification {
680683
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
681684
}
682685

686+
def "it should pass when empty license and allowEmptyLicense set to true"() {
687+
given:
688+
buildFile << """
689+
import com.github.jk1.license.filter.*
690+
691+
plugins {
692+
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
693+
id 'com.github.jk1.dependency-license-report'
694+
}
695+
696+
apply plugin: 'java'
697+
698+
group 'greeting'
699+
version '0.0.1'
700+
701+
repositories {
702+
mavenCentral()
703+
}
704+
705+
dependencies {
706+
implementation group: "javax.xml", name: "jaxrpc-api", version: "1.1"
707+
}
708+
709+
compileKotlin {
710+
kotlinOptions.jvmTarget = "1.8"
711+
}
712+
compileTestKotlin {
713+
kotlinOptions.jvmTarget = "1.8"
714+
}
715+
licenseReport {
716+
filters = new LicenseBundleNormalizer()
717+
allowEmptyLicense = true
718+
allowedLicensesFile = new File("${StringEscapeUtils.escapeJava(allowed.path)}")
719+
}
720+
"""
721+
722+
when:
723+
BuildResult buildResult = result("--build-cache", "checkLicense")
724+
725+
then:
726+
buildResult.task(":checkLicense").outcome == TaskOutcome.SUCCESS
727+
728+
when:
729+
buildResult = result("--build-cache", "checkLicense")
730+
731+
then:
732+
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
733+
734+
when:
735+
buildResult = result("--build-cache", "clean", "checkLicense")
736+
737+
then:
738+
buildResult.task(":checkLicense").outcome == TaskOutcome.FROM_CACHE
739+
740+
when:
741+
buildResult = result("--build-cache", "checkLicense")
742+
743+
then:
744+
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
745+
}
746+
747+
def "it should trim and record license without whitespaces around"() {
748+
given:
749+
buildFile << """
750+
import com.github.jk1.license.filter.*
751+
752+
plugins {
753+
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
754+
id 'com.github.jk1.dependency-license-report'
755+
}
756+
757+
apply plugin: 'java'
758+
759+
group 'greeting'
760+
version '0.0.1'
761+
762+
repositories {
763+
mavenCentral()
764+
}
765+
766+
dependencies {
767+
implementation group: "javax.ws.rs", name: "jsr311-api", version: "1.1.1"
768+
}
769+
770+
compileKotlin {
771+
kotlinOptions.jvmTarget = "1.8"
772+
}
773+
compileTestKotlin {
774+
kotlinOptions.jvmTarget = "1.8"
775+
}
776+
licenseReport {
777+
filters = new LicenseBundleNormalizer()
778+
allowEmptyLicense = true
779+
allowedLicensesFile = new File("${StringEscapeUtils.escapeJava(allowed.path)}")
780+
}
781+
"""
782+
783+
when:
784+
BuildResult buildResult = result("--build-cache", "checkLicense")
785+
786+
then:
787+
buildResult.task(":checkLicense").outcome == TaskOutcome.SUCCESS
788+
789+
when:
790+
buildResult = result("--build-cache", "checkLicense")
791+
792+
then:
793+
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
794+
795+
when:
796+
buildResult = result("--build-cache", "clean", "checkLicense")
797+
798+
then:
799+
buildResult.task(":checkLicense").outcome == TaskOutcome.FROM_CACHE
800+
801+
when:
802+
buildResult = result("--build-cache", "checkLicense")
803+
804+
then:
805+
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
806+
}
807+
683808
@Ignore // https://github.com/jk1/Gradle-License-Report/issues/255
684809
def "using it with configuration cache should not cause the build to fail"() {
685810
given:

0 commit comments

Comments
 (0)