Skip to content

Commit b692aa8

Browse files
committed
Merge pull request #84 from jonathannaguin/master
Unified license reports for multi module builds
2 parents 01d5526 + 06b13e1 commit b692aa8

File tree

2 files changed

+83
-59
lines changed

2 files changed

+83
-59
lines changed

src/main/groovy/nl/javadude/gradle/plugins/license/LicenseResolver.groovy

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -42,67 +42,75 @@ class LicenseResolver {
4242
Set<DependencyMetadata> licenseSet = newHashSet()
4343
def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString()}
4444

45-
// Resolve each dependency
46-
resolveProjectDependencies(project).each {
47-
rd ->
48-
String dependencyDesc = "$rd.moduleVersion.id.group:$rd.moduleVersion.id.name:$rd.moduleVersion.id.version".toString()
49-
Map.Entry licenseEntry = licenses.find {
50-
dep ->
51-
if(dep.key instanceof String) {
52-
dep.key == dependencyDesc
53-
} else if (dep.key instanceof DependencyGroup) {
54-
rd.moduleVersion.id.group == dep.key.group
55-
}
56-
}
57-
if (licenseEntry != null) {
58-
def license = licenseEntry.value
59-
def licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license
60-
licenseSet << new DependencyMetadata(
61-
dependency: dependencyDesc, dependencyFileName: rd.file.name, licenseMetadataList: [ licenseMetadata ]
62-
)
63-
} else {
64-
Closure<DependencyMetadata> dependencyMetadata = {
65-
if(!subprojects[dependencyDesc]) {
66-
def depMetadata = retrieveLicensesForDependency(dependencyDesc)
67-
depMetadata.dependencyFileName = rd.file.name
68-
depMetadata
69-
} else {
70-
noLicenseMetaData(dependencyDesc, rd.file.name)
45+
Set<Project> projects = newHashSet()
46+
projects.add(project)
47+
projects.addAll(project.subprojects)
48+
49+
projects.each {
50+
p ->
51+
52+
// Resolve each dependency
53+
resolveProjectDependencies(p).each {
54+
rd ->
55+
String dependencyDesc = "$rd.moduleVersion.id.group:$rd.moduleVersion.id.name:$rd.moduleVersion.id.version".toString()
56+
Map.Entry licenseEntry = licenses.find {
57+
dep ->
58+
if(dep.key instanceof String) {
59+
dep.key == dependencyDesc
60+
} else if (dep.key instanceof DependencyGroup) {
61+
rd.moduleVersion.id.group == dep.key.group
7162
}
7263
}
64+
if (licenseEntry != null) {
65+
def license = licenseEntry.value
66+
def licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license
67+
licenseSet << new DependencyMetadata(
68+
dependency: dependencyDesc, dependencyFileName: rd.file.name, licenseMetadataList: [ licenseMetadata ]
69+
)
70+
} else {
71+
Closure<DependencyMetadata> dependencyMetadata = {
72+
if(!subprojects[dependencyDesc]) {
73+
def depMetadata = retrieveLicensesForDependency(p, dependencyDesc)
74+
depMetadata.dependencyFileName = rd.file.name
75+
depMetadata
76+
} else {
77+
noLicenseMetaData(dependencyDesc, rd.file.name)
78+
}
79+
}
7380

74-
licenseSet << dependencyMetadata()
81+
licenseSet << dependencyMetadata()
82+
}
7583
}
76-
}
7784

78-
provideFileDependencies(dependenciesToIgnore).each {
79-
fileDependency ->
80-
Closure<DependencyMetadata> licenseMetadata = {
81-
if (licenses.containsKey(fileDependency)) {
82-
def license = licenses[fileDependency]
83-
LicenseMetadata licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license
84-
def alias = aliases.find {
85-
aliasEntry ->
86-
aliasEntry.value.any {
87-
aliasElem ->
88-
if (aliasElem instanceof String) {
89-
return aliasElem == licenseMetadata.licenseName
90-
} else if(aliasElem instanceof LicenseMetadata) {
91-
return aliasElem == licenseMetadata
92-
}
93-
94-
}
95-
}
96-
if (alias) {
97-
licenseMetadata = alias.key
85+
provideFileDependencies(p, dependenciesToIgnore).each {
86+
fileDependency ->
87+
Closure<DependencyMetadata> licenseMetadata = {
88+
if (licenses.containsKey(fileDependency)) {
89+
def license = licenses[fileDependency]
90+
LicenseMetadata licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license
91+
def alias = aliases.find {
92+
aliasEntry ->
93+
aliasEntry.value.any {
94+
aliasElem ->
95+
if (aliasElem instanceof String) {
96+
return aliasElem == licenseMetadata.licenseName
97+
} else if(aliasElem instanceof LicenseMetadata) {
98+
return aliasElem == licenseMetadata
99+
}
100+
101+
}
102+
}
103+
if (alias) {
104+
licenseMetadata = alias.key
105+
}
106+
new DependencyMetadata(dependency: fileDependency, dependencyFileName: fileDependency, licenseMetadataList: [licenseMetadata])
107+
} else {
108+
noLicenseMetaData(fileDependency, fileDependency)
98109
}
99-
new DependencyMetadata(dependency: fileDependency, dependencyFileName: fileDependency, licenseMetadataList: [licenseMetadata])
100-
} else {
101-
noLicenseMetaData(fileDependency, fileDependency)
102110
}
103-
}
104111

105-
licenseSet << licenseMetadata()
112+
licenseSet << licenseMetadata()
113+
}
106114
}
107115

108116
licenseSet
@@ -142,7 +150,7 @@ class LicenseResolver {
142150
dependenciesToHandle
143151
}
144152

145-
Set<String> provideFileDependencies(List<String> dependenciesToIgnore) {
153+
Set<String> provideFileDependencies(Project project, List<String> dependenciesToIgnore) {
146154
Set<String> fileDependencies = newHashSet()
147155

148156
if (project.configurations.any { it.name == dependencyConfiguration }) {
@@ -177,12 +185,14 @@ class LicenseResolver {
177185
* Implementation note: We rely that while resolving configuration with one dependency we get one pom.
178186
* Otherwise we have IllegalStateException
179187
*
188+
* @param project the project
180189
* @param dependencyDesc dependency description
181190
* @param aliases alias mapping for similar license names
182191
* @param initialDependency base dependency (not parent)
183192
* @return dependency metadata, includes license info
184193
*/
185-
private DependencyMetadata retrieveLicensesForDependency(String dependencyDesc,
194+
private DependencyMetadata retrieveLicensesForDependency(Project project,
195+
String dependencyDesc,
186196
String initialDependency = dependencyDesc) {
187197
Dependency d = project.dependencies.create("$dependencyDesc@pom")
188198
Configuration pomConfiguration = project.configurations.detachedConfiguration(d)
@@ -227,7 +237,7 @@ class LicenseResolver {
227237
String parentName = xml.parent.artifactId.text().trim()
228238
String parentVersion = xml.parent.version.text().trim()
229239

230-
retrieveLicensesForDependency("$parentGroup:$parentName:$parentVersion", initialDependency)
240+
retrieveLicensesForDependency(project, "$parentGroup:$parentName:$parentVersion", initialDependency)
231241
} else {
232242
noLicenseMetaData(dependencyDesc)
233243
}

src/test/groovy/nl/javadude/gradle/plugins/license/DownloadLicensesIntegTest.groovy

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,36 @@ class DownloadLicensesIntegTest extends Specification {
7373

7474
}
7575

76-
def "Test that report generating in multi module build doesn't include unrelated subprojects dependencies"() {
76+
def "Test that report generating in multi module build include subprojects dependencies"() {
7777
setup:
7878
subproject.dependencies {
7979
compile "org.jboss.logging:jboss-logging:3.1.3.GA"
8080
compile "com.google.guava:guava:15.0"
8181
}
8282

83+
downloadLicenses.licenses = [
84+
"com.google.guava:guava:15.0": license("MY_LICENSE", "MY_URL"),
85+
"org.jboss.logging:jboss-logging:3.1.3.GA": license("MY_LICENSE", "MY_URL")
86+
]
87+
8388
when:
8489
downloadLicenses.execute()
8590

8691
then:
8792
File f = getLicenseReportFolder()
8893
assertLicenseReportsExist(f)
8994

90-
dependenciesInReport(xml4LicenseByDependencyReport(f)) == 0
91-
licensesInReport(xml4DependencyByLicenseReport(f)) == 0
95+
def xmlByDependency = xml4LicenseByDependencyReport(f)
96+
def xmlByLicense = xml4DependencyByLicenseReport(f)
97+
98+
dependenciesInReport(xmlByDependency) == 2
99+
licensesInReport(xmlByLicense) == 1
100+
101+
dependencyWithLicensePresent(xmlByDependency, "org.jboss.logging:jboss-logging:3.1.3.GA", "jboss-logging-3.1.3.GA.jar", "MY_LICENSE")
102+
dependencyWithLicensePresent(xmlByDependency, "com.google.guava:guava:15.0", "guava-15.0.jar", "MY_LICENSE")
103+
104+
dependencyWithLicenseUrlPresent(xmlByDependency, "org.jboss.logging:jboss-logging:3.1.3.GA", "MY_URL")
105+
dependencyWithLicenseUrlPresent(xmlByDependency, "com.google.guava:guava:15.0", "MY_URL")
92106
}
93107

94108
def "Test that report in multi module build includes transitive prj dependencies, prj dependencies included and specified"() {

0 commit comments

Comments
 (0)