Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change downloadLicenses task to accept a list of configurationDependencies #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= License Gradle Plugin
Jeroen van Erp
:license_plugin_version: 0.15.0
:license_plugin_version: 0.17.0

image:https://travis-ci.org/hierynomus/license-gradle-plugin.svg?branch=master[Build Status,link=https://travis-ci.org/hierynomus/license-gradle-plugin]

Expand Down Expand Up @@ -361,7 +361,7 @@ The `downloadLicenses` task has a set of properties, most can be set in the exte
|licenses |a pre-defined mapping of a dependency to a license; useful if the external repositories do not have license information available
|aliases |a mapping between licenses; useful to consolidate the various POM definitions of different spelled/named licenses
|excludeDependencies |a List of dependencies that are to be excluded from reporting
|dependencyConfiguration |Gradle dependency configuration to report on (defaults to "runtime").
|configurationDependencies |Gradle dependency configuration to report on (defaults to "runtime").
|====

A 'license()' method is made available by the License Extension that takes two Strings, the first is the license name, the second is the URL to the license.
Expand All @@ -388,12 +388,18 @@ downloadLicenses {
'com.some-other-project.bar:foobar:1.0'
]

dependencyConfiguration = 'compile'
configurationDependencies = ["compile"]
}
----

== Changelog

=== v0.17.0 (2022-05-31)
- Change downloadLicenses task to accept a list of configurationDependencies

=== v0.15.0 (2018-11-22)
- Correctly published the split-up plugins (I hope..)

=== v0.15.0 (2018-11-22)
- Correctly published the split-up plugins (I hope..)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ downloadLicenses {
html.enabled = false
json.enabled = true
}
dependencyConfiguration = "runtimeClasspath"
configurationDependencies = ["runtimeClasspath"]
}
"""
subProject = addSubproject("subproject")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class LicenseReportingPlugin implements Plugin<Project> {
licenses = [:]
aliases = [:]
report = new DownloadLicensesReportExtension(html: html, xml: xml, json: json)
dependencyConfiguration = LicenseBasePlugin.DEFAULT_DEPENDENCY_CONFIGURATION_TO_HANDLE
configurationDependencies = [LicenseBasePlugin.DEFAULT_DEPENDENCY_CONFIGURATION_TO_HANDLE]
}

project.logger.info("Adding download licenses extension");
Expand Down Expand Up @@ -106,7 +106,7 @@ class LicenseReportingPlugin implements Plugin<Project> {
xmlDestination = { new File("${downloadLicensesExtension.report.xml.destination}") }
htmlDestination = { new File("${downloadLicensesExtension.report.html.destination}") }
jsonDestination = { new File("${downloadLicensesExtension.report.json.destination}") }
dependencyConfiguration = { downloadLicensesExtension.dependencyConfiguration }
configurationDependencies = { downloadLicensesExtension.configurationDependencies }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public class DownloadLicenses extends ConventionTask {
@Input boolean json

/**
* The dependency configuration to report on.
* List of configurations to report on.
*/
@Input String dependencyConfiguration
@Input List<String> configurationDependencies

@TaskAction
def downloadLicenses() {
Expand All @@ -124,7 +124,7 @@ public class DownloadLicenses extends ConventionTask {
},
licenses: getLicenses(),
dependenciesToIgnore: excludeDependencies,
dependencyConfiguration: dependencyConfiguration)
configurationDependencies: configurationDependencies)
licenseResolver.provideLicenseMap4Dependencies()
}.memoize()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package nl.javadude.gradle.plugins.license

import org.gradle.api.tasks.Input
import org.gradle.util.ConfigureUtil

/**
Expand Down Expand Up @@ -84,9 +85,9 @@ class DownloadLicensesExtension {


/**
* The dependency configuration to report on.
* List of configurations to report on.
*/
String dependencyConfiguration
@Input List<String> configurationDependencies

/**
* Report extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.gradle.api.logging.Logging
import java.util.regex.Pattern

import static DependencyMetadata.noLicenseMetaData

/**
* License resolver for dependencies.
*/
Expand All @@ -41,7 +42,7 @@ class LicenseResolver {
private Map<Object, Object> licenses
private Map<LicenseMetadata, List<Object>> aliases
private boolean includeProjectDependencies
private String dependencyConfiguration
private List<String> configurationDependencies
private boolean ignoreFatalParseErrors
private List<Pattern> patternsToIgnore

Expand All @@ -55,7 +56,7 @@ class LicenseResolver {
*/
public Set<DependencyMetadata> provideLicenseMap4Dependencies() {
Set<DependencyMetadata> licenseSet = new HashSet<DependencyMetadata>()
def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString()}
def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString() }
project.logger.info("Found subprojects: $subprojects")

Set<Project> projects = new HashSet<Project>()
Expand All @@ -65,68 +66,68 @@ class LicenseResolver {
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)
// 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(p).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
}

}
provideFileDependencies(p).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)
}
if (alias) {
licenseMetadata = alias.key
}
new DependencyMetadata(dependency: fileDependency, dependencyFileName: fileDependency, licenseMetadataList: [licenseMetadata])
} else {
noLicenseMetaData(fileDependency, fileDependency)
}
}

licenseSet << licenseMetadata()
}
licenseSet << licenseMetadata()
}
}

licenseSet
Expand All @@ -139,30 +140,32 @@ class LicenseResolver {
/**
* Provide full list of resolved artifacts to handle for a given project.
*
* @param project the project
* @param project the project
* @return Set with resolved artifacts
*/
Set<ResolvedArtifact> resolveProjectDependencies(Project project, HashSet<Project> subprojectsAlreadyScanned) {

Set<ResolvedArtifact> dependenciesToHandle = new HashSet<ResolvedArtifact>()
def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString()}
def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString() }

project.configurations.each { c ->
project.logger.info("Project $project -> Configuration $c (can be resolved? ${c.isCanBeResolved()})")
if (c.name == dependencyConfiguration && isResolvable(c)) {
project.logger.debug("Resolvable $c in project $project")
c.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact d ->
String dependencyDesc = "$d.moduleVersion.id.group:$d.moduleVersion.id.name:$d.moduleVersion.id.version".toString()
if(isDependencyIncluded(dependencyDesc)) {
Project subproject = subprojects[dependencyDesc]?.first()
if (subproject && !subprojectsAlreadyScanned.contains(subproject)) {
subprojectsAlreadyScanned.add(subproject)
if(includeProjectDependencies) {
for (String conf : configurationDependencies) {
if (c.name == conf && isResolvable(c)) {
project.logger.debug("Resolvable $c in project $project")
c.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact d ->
String dependencyDesc = "$d.moduleVersion.id.group:$d.moduleVersion.id.name:$d.moduleVersion.id.version".toString()
if (isDependencyIncluded(dependencyDesc)) {
Project subproject = subprojects[dependencyDesc]?.first()
if (subproject && !subprojectsAlreadyScanned.contains(subproject)) {
subprojectsAlreadyScanned.add(subproject)
if (includeProjectDependencies) {
dependenciesToHandle.add(d)
}
dependenciesToHandle.addAll(resolveProjectDependencies(subproject, subprojectsAlreadyScanned))
} else if (!subproject) {
dependenciesToHandle.add(d)
}
dependenciesToHandle.addAll(resolveProjectDependencies(subproject, subprojectsAlreadyScanned))
} else if (!subproject) {
dependenciesToHandle.add(d)
}
}
}
Expand All @@ -175,23 +178,24 @@ class LicenseResolver {

Set<String> provideFileDependencies(Project project) {
Set<String> fileDependencies = new HashSet<String>()
for (String conf : configurationDependencies) {
if (project.configurations.any { it.name == conf }) {
Configuration configuration = project.configurations.getByName(conf)

if (project.configurations.any { it.name == dependencyConfiguration }) {
Configuration configuration = project.configurations.getByName(dependencyConfiguration)

Set<Dependency> d = configuration.allDependencies.findAll {
it instanceof FileCollectionDependency
}
Set<Dependency> d = configuration.allDependencies.findAll {
it instanceof FileCollectionDependency
}

d.each {
FileCollectionDependency fileDependency ->
fileDependency.resolve().each {
if (isDependencyIncluded(it.name)) {
fileDependencies.add(it.name)
d.each {
FileCollectionDependency fileDependency ->
fileDependency.resolve().each {
if (isDependencyIncluded(it.name)) {
fileDependencies.add(it.name)
}
}
}
}
}

}
}

logger.debug("Project $project.name found ${fileDependencies.size()} file dependencies to handle.")
Expand All @@ -210,9 +214,9 @@ class LicenseResolver {
return conf.metaClass.respondsTo(conf, "isCanBeResolved") ? conf.isCanBeResolved() : true
}

boolean isDependencyIncluded(String depName){
for(Pattern pattern: this.patternsToIgnore){
if(pattern.matcher(depName).matches()){
boolean isDependencyIncluded(String depName) {
for (Pattern pattern : this.patternsToIgnore) {
if (pattern.matcher(depName).matches()) {
return false;
}
}
Expand All @@ -230,7 +234,7 @@ 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 project the project
* @param dependencyDesc dependency description
* @param aliases alias mapping for similar license names
* @param initialDependency base dependency (not parent)
Expand Down Expand Up @@ -275,8 +279,8 @@ class LicenseResolver {
aliasEntry.value.any {
aliasElem ->
if (aliasElem instanceof String) {
return aliasElem == license.licenseName
} else if(aliasElem instanceof LicenseMetadata) {
return aliasElem == license.licenseName
} else if (aliasElem instanceof LicenseMetadata) {
return aliasElem == license
}

Expand All @@ -301,14 +305,14 @@ class LicenseResolver {
}
}

void setDependenciesToIgnore(List<String> dependenciesToIgnore){
if(dependenciesToIgnore == null){
void setDependenciesToIgnore(List<String> dependenciesToIgnore) {
if (dependenciesToIgnore == null) {
this.patternsToIgnore = Collections.emptyList();
return;
}

this.patternsToIgnore = new ArrayList<>(dependenciesToIgnore.size());
for(String toIgnore: dependenciesToIgnore){
for (String toIgnore : dependenciesToIgnore) {
this.patternsToIgnore.add(Pattern.compile(toIgnore))
}
}
Expand Down