diff --git a/src/main/groovy/nl/javadude/gradle/plugins/license/LicenseResolver.groovy b/src/main/groovy/nl/javadude/gradle/plugins/license/LicenseResolver.groovy index b44bcca..3326b2c 100644 --- a/src/main/groovy/nl/javadude/gradle/plugins/license/LicenseResolver.groovy +++ b/src/main/groovy/nl/javadude/gradle/plugins/license/LicenseResolver.groovy @@ -42,67 +42,75 @@ class LicenseResolver { Set licenseSet = newHashSet() def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString()} - // Resolve each dependency - resolveProjectDependencies(project).each { - rd -> - String dependencyDesc = "$rd.moduleVersion.id.group:$rd.moduleVersion.id.name:$rd.moduleVersion.id.version".toString() - Map.Entry licenseEntry = licenses.find { - dep -> - if(dep.key instanceof String) { - dep.key == dependencyDesc - } else if (dep.key instanceof DependencyGroup) { - rd.moduleVersion.id.group == dep.key.group - } - } - if (licenseEntry != null) { - def license = licenseEntry.value - def licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license - licenseSet << new DependencyMetadata( - dependency: dependencyDesc, dependencyFileName: rd.file.name, licenseMetadataList: [ licenseMetadata ] - ) - } else { - Closure dependencyMetadata = { - if(!subprojects[dependencyDesc]) { - def depMetadata = retrieveLicensesForDependency(dependencyDesc) - depMetadata.dependencyFileName = rd.file.name - depMetadata - } else { - noLicenseMetaData(dependencyDesc, rd.file.name) + Set projects = newHashSet() + projects.add(project) + projects.addAll(project.subprojects) + + projects.each { + p -> + + // Resolve each dependency + resolveProjectDependencies(p).each { + rd -> + String dependencyDesc = "$rd.moduleVersion.id.group:$rd.moduleVersion.id.name:$rd.moduleVersion.id.version".toString() + Map.Entry licenseEntry = licenses.find { + dep -> + if(dep.key instanceof String) { + dep.key == dependencyDesc + } else if (dep.key instanceof DependencyGroup) { + rd.moduleVersion.id.group == dep.key.group } } + if (licenseEntry != null) { + def license = licenseEntry.value + def licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license + licenseSet << new DependencyMetadata( + dependency: dependencyDesc, dependencyFileName: rd.file.name, licenseMetadataList: [ licenseMetadata ] + ) + } else { + Closure dependencyMetadata = { + if(!subprojects[dependencyDesc]) { + def depMetadata = retrieveLicensesForDependency(p, dependencyDesc) + depMetadata.dependencyFileName = rd.file.name + depMetadata + } else { + noLicenseMetaData(dependencyDesc, rd.file.name) + } + } - licenseSet << dependencyMetadata() + licenseSet << dependencyMetadata() + } } - } - provideFileDependencies(dependenciesToIgnore).each { - fileDependency -> - Closure licenseMetadata = { - if (licenses.containsKey(fileDependency)) { - def license = licenses[fileDependency] - LicenseMetadata licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license - def alias = aliases.find { - aliasEntry -> - aliasEntry.value.any { - aliasElem -> - if (aliasElem instanceof String) { - return aliasElem == licenseMetadata.licenseName - } else if(aliasElem instanceof LicenseMetadata) { - return aliasElem == licenseMetadata - } - - } - } - if (alias) { - licenseMetadata = alias.key + provideFileDependencies(p, dependenciesToIgnore).each { + fileDependency -> + Closure licenseMetadata = { + if (licenses.containsKey(fileDependency)) { + def license = licenses[fileDependency] + LicenseMetadata licenseMetadata = license instanceof String ? DownloadLicensesExtension.license(license) : license + def alias = aliases.find { + aliasEntry -> + aliasEntry.value.any { + aliasElem -> + if (aliasElem instanceof String) { + return aliasElem == licenseMetadata.licenseName + } else if(aliasElem instanceof LicenseMetadata) { + return aliasElem == licenseMetadata + } + + } + } + if (alias) { + licenseMetadata = alias.key + } + new DependencyMetadata(dependency: fileDependency, dependencyFileName: fileDependency, licenseMetadataList: [licenseMetadata]) + } else { + noLicenseMetaData(fileDependency, fileDependency) } - new DependencyMetadata(dependency: fileDependency, dependencyFileName: fileDependency, licenseMetadataList: [licenseMetadata]) - } else { - noLicenseMetaData(fileDependency, fileDependency) } - } - licenseSet << licenseMetadata() + licenseSet << licenseMetadata() + } } licenseSet @@ -142,7 +150,7 @@ class LicenseResolver { dependenciesToHandle } - Set provideFileDependencies(List dependenciesToIgnore) { + Set provideFileDependencies(Project project, List dependenciesToIgnore) { Set fileDependencies = newHashSet() if (project.configurations.any { it.name == dependencyConfiguration }) { @@ -177,12 +185,14 @@ class LicenseResolver { * Implementation note: We rely that while resolving configuration with one dependency we get one pom. * Otherwise we have IllegalStateException * + * @param project the project * @param dependencyDesc dependency description * @param aliases alias mapping for similar license names * @param initialDependency base dependency (not parent) * @return dependency metadata, includes license info */ - private DependencyMetadata retrieveLicensesForDependency(String dependencyDesc, + private DependencyMetadata retrieveLicensesForDependency(Project project, + String dependencyDesc, String initialDependency = dependencyDesc) { Dependency d = project.dependencies.create("$dependencyDesc@pom") Configuration pomConfiguration = project.configurations.detachedConfiguration(d) @@ -227,7 +237,7 @@ class LicenseResolver { String parentName = xml.parent.artifactId.text().trim() String parentVersion = xml.parent.version.text().trim() - retrieveLicensesForDependency("$parentGroup:$parentName:$parentVersion", initialDependency) + retrieveLicensesForDependency(project, "$parentGroup:$parentName:$parentVersion", initialDependency) } else { noLicenseMetaData(dependencyDesc) } diff --git a/src/test/groovy/nl/javadude/gradle/plugins/license/DownloadLicensesIntegTest.groovy b/src/test/groovy/nl/javadude/gradle/plugins/license/DownloadLicensesIntegTest.groovy index 44cec18..cb30553 100644 --- a/src/test/groovy/nl/javadude/gradle/plugins/license/DownloadLicensesIntegTest.groovy +++ b/src/test/groovy/nl/javadude/gradle/plugins/license/DownloadLicensesIntegTest.groovy @@ -73,13 +73,18 @@ class DownloadLicensesIntegTest extends Specification { } - def "Test that report generating in multi module build doesn't include unrelated subprojects dependencies"() { + def "Test that report generating in multi module build include subprojects dependencies"() { setup: subproject.dependencies { compile "org.jboss.logging:jboss-logging:3.1.3.GA" compile "com.google.guava:guava:15.0" } + downloadLicenses.licenses = [ + "com.google.guava:guava:15.0": license("MY_LICENSE", "MY_URL"), + "org.jboss.logging:jboss-logging:3.1.3.GA": license("MY_LICENSE", "MY_URL") + ] + when: downloadLicenses.execute() @@ -87,8 +92,17 @@ class DownloadLicensesIntegTest extends Specification { File f = getLicenseReportFolder() assertLicenseReportsExist(f) - dependenciesInReport(xml4LicenseByDependencyReport(f)) == 0 - licensesInReport(xml4DependencyByLicenseReport(f)) == 0 + def xmlByDependency = xml4LicenseByDependencyReport(f) + def xmlByLicense = xml4DependencyByLicenseReport(f) + + dependenciesInReport(xmlByDependency) == 2 + licensesInReport(xmlByLicense) == 1 + + dependencyWithLicensePresent(xmlByDependency, "org.jboss.logging:jboss-logging:3.1.3.GA", "jboss-logging-3.1.3.GA.jar", "MY_LICENSE") + dependencyWithLicensePresent(xmlByDependency, "com.google.guava:guava:15.0", "guava-15.0.jar", "MY_LICENSE") + + dependencyWithLicenseUrlPresent(xmlByDependency, "org.jboss.logging:jboss-logging:3.1.3.GA", "MY_URL") + dependencyWithLicenseUrlPresent(xmlByDependency, "com.google.guava:guava:15.0", "MY_URL") } def "Test that report in multi module build includes transitive prj dependencies, prj dependencies included and specified"() {