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

Specify dependency configuration #42

Merged
Merged
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ The downloadLicense task has a set of properties, most can be set in the extensi
- 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").

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 @@ -134,5 +135,7 @@ downloadLicenses {
excludeDependencies = [
'com.some-other-project.bar:foobar:1.0'
]

dependencyConfiguration = 'compile'
}
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class DownloadLicenses extends DefaultTask {
*/
@Input boolean html

/**
* The dependency configuration to report on.
*/
@Input String dependencyConfiguration

@TaskAction
def downloadLicenses() {
if (!enabled || (!isReportByDependency() && !isReportByLicenseType())
Expand All @@ -86,7 +91,8 @@ public class DownloadLicenses extends DefaultTask {
new MapEntry(resolveAliasKey(it.key), it.value)
},
licenses: getLicenses(),
dependenciesToIgnore: excludeDependencies)
dependenciesToIgnore: excludeDependencies,
dependencyConfiguration: dependencyConfiguration)
licenseResolver.provideLicenseMap4Dependencies()
}.memoize()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class DownloadLicensesExtension {
*/
boolean html

/**
* The dependency configuration to report on.
*/
String dependencyConfiguration

/**
* Report extension.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class LicensePlugin implements Plugin<Project> {

static final String DEFAULT_FILE_NAME_FOR_REPORTS_BY_DEPENDENCY = "dependency-license"
static final String DEFAULT_FILE_NAME_FOR_REPORTS_BY_LICENSE = "license-dependency"
static final String DEFAULT_DEPENDENCY_CONFIGURATION_TO_HANDLE = "runtime"

protected Project project
protected LicenseExtension extension
Expand Down Expand Up @@ -100,6 +101,7 @@ class LicensePlugin implements Plugin<Project> {
licenses = [:]
aliases = [:]
report = new DownloadLicensesReportExtension(html: html, xml: xml)
dependencyConfiguration = DEFAULT_DEPENDENCY_CONFIGURATION_TO_HANDLE
}

logger.info("Adding download licenses extension");
Expand Down Expand Up @@ -159,6 +161,7 @@ class LicensePlugin implements Plugin<Project> {
excludeDependencies = { downloadLicensesExtension.excludeDependencies }
xmlDestination = { downloadLicensesExtension.report.xml.destination }
htmlDestination = { downloadLicensesExtension.report.html.destination }
dependencyConfiguration = { downloadLicensesExtension.dependencyConfiguration }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class LicenseResolver {

private static final Logger logger = Logging.getLogger(LicenseResolver);

private static final String DEFAULT_CONFIGURATION_TO_HANDLE = "runtime"

/**
* Reference to gradle project.
*/
Expand All @@ -30,6 +28,7 @@ class LicenseResolver {
private Map<LicenseMetadata, List<Object>> aliases
private List<String> dependenciesToIgnore
private boolean includeProjectDependencies
private String dependencyConfiguration

/**
* Provide set with dependencies metadata.
Expand Down Expand Up @@ -121,8 +120,8 @@ class LicenseResolver {
Set<ResolvedArtifact> dependenciesToHandle = newHashSet()
def subprojects = project.rootProject.subprojects.groupBy { Project p -> "$p.group:$p.name:$p.version".toString()}

if (project.configurations.any { it.name == DEFAULT_CONFIGURATION_TO_HANDLE }) {
def runtimeConfiguration = project.configurations.getByName(DEFAULT_CONFIGURATION_TO_HANDLE)
if (project.configurations.any { it.name == dependencyConfiguration }) {
def runtimeConfiguration = project.configurations.getByName(dependencyConfiguration)
runtimeConfiguration.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact d ->
String dependencyDesc = "$d.moduleVersion.id.group:$d.moduleVersion.id.name:$d.moduleVersion.id.version".toString()
if(!dependenciesToIgnore.contains(dependencyDesc)) {
Expand All @@ -146,8 +145,8 @@ class LicenseResolver {
Set<String> provideFileDependencies(List<String> dependenciesToIgnore) {
Set<String> fileDependencies = newHashSet()

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

Set<Dependency> d = configuration.allDependencies.findAll {
it instanceof FileCollectionDependency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ class DownloadLicensesIntegTest extends Specification {
dependencyWithLicenseUrlPresent(xmlByDependency, "com.google.guava:guava:15.0", "MY_URL")
}

def "Test that default configuration is runtime"() {
def "Test that dependencyConfiguration defaults to runtime"() {
setup:
project.dependencies {
testCompile project.files("testDependency.jar")
testRuntime "org.jboss.logging:jboss-logging:3.1.3.GA"
testRuntime "com.google.guava:guava:15.0"
runtime "org.apache.ivy:ivy:2.3.0",
"org.jboss.logging:jboss-logging:3.1.3.GA"
}

when:
Expand All @@ -203,8 +205,32 @@ class DownloadLicensesIntegTest extends Specification {
def xmlByDependency = xml4LicenseByDependencyReport(f)
def xmlByLicense = xml4DependencyByLicenseReport(f)

dependenciesInReport(xmlByDependency) == 0
licensesInReport(xmlByLicense) == 0
dependenciesInReport(xmlByDependency) == 2
licensesInReport(xmlByLicense) == 2
}

def "Test that explicitly-specified dependencyConfiguration is respected"() {
setup:
project.dependencies {
runtime project.files("testDependency.jar")
testRuntime "com.google.guava:guava:15.0"
testCompile "org.apache.ivy:ivy:2.3.0",
"org.jboss.logging:jboss-logging:3.1.3.GA"
}
downloadLicenses.dependencyConfiguration = "testCompile"

when:
downloadLicenses.execute()

then:
File f = getLicenseReportFolder()
assertLicenseReportsExist(f)

def xmlByDependency = xml4LicenseByDependencyReport(f)
def xmlByLicense = xml4DependencyByLicenseReport(f)

dependenciesInReport(xmlByDependency) == 2
licensesInReport(xmlByLicense) == 2
}

def "Test that aliases works well for different dependencies with the same license for string->list mapping"() {
Expand Down