Skip to content

Commit

Permalink
Merge pull request #84 from jonathannaguin/master
Browse files Browse the repository at this point in the history
Unified license reports for multi module builds
  • Loading branch information
hierynomus committed Oct 2, 2015
2 parents 01d5526 + 06b13e1 commit b692aa8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,75 @@ class LicenseResolver {
Set<DependencyMetadata> 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> dependencyMetadata = {
if(!subprojects[dependencyDesc]) {
def depMetadata = retrieveLicensesForDependency(dependencyDesc)
depMetadata.dependencyFileName = rd.file.name
depMetadata
} else {
noLicenseMetaData(dependencyDesc, rd.file.name)
Set<Project> 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> 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<DependencyMetadata> 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<DependencyMetadata> 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
Expand Down Expand Up @@ -142,7 +150,7 @@ class LicenseResolver {
dependenciesToHandle
}

Set<String> provideFileDependencies(List<String> dependenciesToIgnore) {
Set<String> provideFileDependencies(Project project, List<String> dependenciesToIgnore) {
Set<String> fileDependencies = newHashSet()

if (project.configurations.any { it.name == dependencyConfiguration }) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,36 @@ 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()

then:
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"() {
Expand Down

0 comments on commit b692aa8

Please sign in to comment.