@@ -18,70 +18,92 @@ package com.github.jk1.license.check
18
18
import groovy.json.JsonOutput
19
19
import org.gradle.api.GradleException
20
20
21
+ /**
22
+ * This class compares the found licences with the allowed licenses and creates a report for any missing license
23
+ */
21
24
class LicenseChecker {
22
-
23
- void checkAllDependencyLicensesAreAllowed (
24
- Object allowedLicensesFile , File projectLicensesDataFile , File notPassedDependenciesOutputFile ) {
25
+ static void checkAllDependencyLicensesAreAllowed (
26
+ Object allowedLicensesFile ,
27
+ File projectLicensesDataFile ,
28
+ boolean requireAllLicensesAllowed ,
29
+ File notPassedDependenciesOutputFile ) {
25
30
List<Dependency > allDependencies = LicenseCheckerFileReader . importDependencies(projectLicensesDataFile)
31
+ removeNullLicenses(allDependencies)
26
32
List<AllowedLicense > allowedLicenses = LicenseCheckerFileReader . importAllowedLicenses(allowedLicensesFile)
27
- List<Dependency > notPassedDependencies = searchForNotAllowedDependencies(allDependencies, allowedLicenses)
33
+ List<Tuple2<Dependency , List<ModuleLicense > > > notPassedDependencies = getNotAllowedLicenses(allDependencies, allowedLicenses)
34
+ if (! requireAllLicensesAllowed) {
35
+ // when we do not check for all Licenses allowed, we can filter out all dependencies here which had a partial match:
36
+ // this means, when the size of notPassedLicenses differs, at least one license matched with our allowed-list
37
+ notPassedDependencies = notPassedDependencies. findAll { it. get(0 ). moduleLicenses == null || it. get(1 ). size() == it. get(0 ). moduleLicenses. size() }
38
+ }
28
39
generateNotPassedDependenciesFile(notPassedDependencies, notPassedDependenciesOutputFile)
29
40
30
41
if (! notPassedDependencies. isEmpty()) {
31
- throw new GradleException (" Some library licenses are not allowed.\n " +
32
- " Read [$notPassedDependenciesOutputFile . path ] for more information." )
42
+ throw new GradleException (" Some library licenses are not allowed:\n " +
43
+ " $notPassedDependenciesOutputFile . text \n\n " +
44
+ " Read [$notPassedDependenciesOutputFile . path ] for more information." )
33
45
}
34
46
}
35
47
36
- private List<Dependency > searchForNotAllowedDependencies (
37
- List<Dependency > dependencies , List<AllowedLicense > allowedLicenses ) {
38
- return dependencies. findAll { ! isDependencyHasAllowedLicense(it, allowedLicenses) }
39
- }
40
-
41
- private void generateNotPassedDependenciesFile (
42
- List<Dependency > notPassedDependencies , File notPassedDependenciesOutputFile ) {
43
- notPassedDependenciesOutputFile. text =
44
- JsonOutput . prettyPrint(JsonOutput . toJson(
45
- [" dependenciesWithoutAllowedLicenses" : notPassedDependencies. collect { toAllowedLicenseList(it) }. flatten()]))
48
+ /**
49
+ * removes 'null'-licenses from dependencies which have at least one more license
50
+ */
51
+ private static void removeNullLicenses (List<Dependency > dependencies ) {
52
+ for (Dependency dependency : dependencies) {
53
+ if (dependency. moduleLicenses. any { it. moduleLicense == null } && ! dependency. moduleLicenses. every { it. moduleLicense == null }) {
54
+ dependency. moduleLicenses = dependency. moduleLicenses. findAll { it. moduleLicense != null }
55
+ }
56
+ }
46
57
}
47
58
48
- private boolean isDependencyHasAllowedLicense (Dependency dependency , List<AllowedLicense > allowedLicenses ) {
49
- for (allowedLicense in allowedLicenses) {
50
- if (isDependencyMatchesAllowedLicense(dependency, allowedLicense)) return true
59
+ private static List<Tuple2<Dependency , List<ModuleLicense > > > getNotAllowedLicenses (List<Dependency > dependencies , List<AllowedLicense > allowedLicenses ) {
60
+ List<Tuple2<Dependency , List<ModuleLicense > > > result = new ArrayList<> ()
61
+ for (Dependency dependency : dependencies) {
62
+ List<AllowedLicense > perDependencyAllowedLicenses = allowedLicenses. findAll { isDependencyNameMatchesAllowedLicense(dependency, it) && isDependencyVersionMatchesAllowedLicense(dependency, it) }
63
+ // allowedLicense matches anything, so we don't need to further check
64
+ if (perDependencyAllowedLicenses. any { it. moduleLicense == null || it. moduleLicense == " .*" }) {
65
+ continue
66
+ }
67
+ def notAllowedLicenses = dependency. moduleLicenses. findAll { ! isDependencyLicenseMatchesAllowedLicense(it, perDependencyAllowedLicenses) }
68
+ if (! notAllowedLicenses. isEmpty()) {
69
+ result. add(Tuple2 . of(dependency, notAllowedLicenses))
70
+ }
51
71
}
52
- return false
72
+ return result
53
73
}
54
74
55
- private boolean isDependencyMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
56
- return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) &&
57
- isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) &&
58
- isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense)
75
+ private static void generateNotPassedDependenciesFile (
76
+ List<Tuple2<Dependency , List<ModuleLicense > > > notPassedDependencies , File notPassedDependenciesOutputFile ) {
77
+ notPassedDependenciesOutputFile. text =
78
+ JsonOutput . prettyPrint(JsonOutput . toJson(
79
+ [" dependenciesWithoutAllowedLicenses" : notPassedDependencies. collect { toAllowedLicenseList(it. get(0 ), it. get(1 )) }. flatten()]))
59
80
}
60
81
61
- private boolean isDependencyNameMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
82
+ private static boolean isDependencyNameMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
62
83
return dependency. moduleName ==~ allowedLicense. moduleName || allowedLicense. moduleName == null ||
63
- dependency. moduleName == allowedLicense. moduleName
84
+ dependency. moduleName == allowedLicense. moduleName
64
85
}
65
86
66
- private boolean isDependencyVersionMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
87
+ private static boolean isDependencyVersionMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
67
88
return dependency. moduleVersion ==~ allowedLicense. moduleVersion || allowedLicense. moduleVersion == null ||
68
- dependency. moduleVersion == allowedLicense. moduleVersion
89
+ dependency. moduleVersion == allowedLicense. moduleVersion
69
90
}
70
91
71
- private boolean isDependencyLicenseMatchesAllowedLicense (Dependency dependency , AllowedLicense allowedLicense ) {
72
- if (allowedLicense. moduleLicense == null || allowedLicense. moduleLicense == " .*" ) return true
92
+ private static boolean isDependencyLicenseMatchesAllowedLicense (ModuleLicense moduleLicense , List<AllowedLicense > allowedLicenses ) {
93
+ for (AllowedLicense allowedLicense : allowedLicenses) {
94
+ if (allowedLicense. moduleLicense == null || allowedLicense. moduleLicense == " .*" ) return true
73
95
74
- for (moduleLicenses in dependency . moduleLicenses)
75
- if (moduleLicenses . moduleLicense ==~ allowedLicense. moduleLicense ||
76
- moduleLicenses . moduleLicense == allowedLicense . moduleLicense) return true
96
+ if (moduleLicense . moduleLicense ==~ allowedLicense . moduleLicense ||
97
+ moduleLicense . moduleLicense == allowedLicense. moduleLicense) return true
98
+ }
77
99
return false
78
100
}
79
101
80
- private List<AllowedLicense > toAllowedLicenseList (Dependency dependency ) {
81
- if (dependency . moduleLicenses. isEmpty()) {
82
- return [ new AllowedLicense (dependency. moduleName, dependency. moduleVersion, null ) ]
102
+ private static List<AllowedLicense > toAllowedLicenseList (Dependency dependency , List< ModuleLicense > moduleLicenses ) {
103
+ if (moduleLicenses. isEmpty()) {
104
+ return [new AllowedLicense (dependency. moduleName, dependency. moduleVersion, null )]
83
105
} else {
84
- return dependency . moduleLicenses. collect { new AllowedLicense (dependency. moduleName, dependency. moduleVersion, it. moduleLicense) }
106
+ return moduleLicenses. findAll { it } . collect { new AllowedLicense (dependency. moduleName, dependency. moduleVersion, it. moduleLicense) }
85
107
}
86
108
}
87
109
}
0 commit comments