Skip to content

Commit

Permalink
Let license aliases be defined using the DSL. Fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Oct 9, 2018
1 parent c7fd65b commit 8dd5ef9
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 81 deletions.
16 changes: 9 additions & 7 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ license {
url
distribution
comments
aliases
}
}
}
Expand Down Expand Up @@ -425,13 +426,14 @@ This block maps to the `<licenses>` block in POM. At least one nested `license`
.License.licenses.license
[header, cols="5*"]
|===
| Name | Type | Required | Default Value | Description
| id | LicenseId | no* | |
| primary | boolean | no* | false | Identifies this as the main license if there are more than one
| name | String | yes | | Maps to the `<name>` block
| url | String | no | | Maps to the `<url>` block
| distribution | String | no | 'repo' | Maps to the `<distribution>` block
| comments | String | no | | Maps to the `<comments>` block
| Name | Type | Required | Default Value | Description
| id | LicenseId | no* | |
| primary | boolean | no* | false | Identifies this as the main license if there are more than one
| name | String | yes | | Maps to the `<name>` block
| url | String | no | | Maps to the `<url>` block
| distribution | String | no | 'repo' | Maps to the `<distribution>` block
| comments | String | no | | Maps to the `<comments>` block
| aliases | List<String> | no | [] | List of license aliases
|===

This entry maps to a `<license>` block nested inside `<licenses>` in POM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class License {
String distribution = 'repo'
String comments
boolean primary
List<String> aliases = []

void setId(LicenseId id) {
this.id = id
Expand All @@ -60,6 +61,7 @@ class License {
copy.distribution = distribution
copy.comments = comments
copy.primary
copy.aliases.addAll(aliases)
copy
}

Expand All @@ -70,5 +72,6 @@ class License {
o1.distribution = o1.distribution ?: o2?.distribution
o1.comments = o1.comments ?: o2?.comments
o1.primary = o1.primary ?: o2?.primary
o1.aliases = (o1.aliases + o2.aliases).unique()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,89 @@ import static org.kordamp.gradle.plugin.base.BasePlugin.isRootProject
class LicensePlugin implements Plugin<Project> {
private static final String VISITED = LicensePlugin.class.name.replace('.', '_') + '_VISITED'

private static final LicenseMetadata LICENSE_APACHE_TWO = new LicenseMetadata('The Apache Software License, Version 2.0', LicenseId.APACHE_2_0.url())
private static final LicenseMetadata LICENSE_EPL1 = new LicenseMetadata('Eclipse Public License v1.0', LicenseId.EPL_1_0.url())
private static final LicenseMetadata LICENSE_EPL2 = new LicenseMetadata('Eclipse Public License v2.0', LicenseId.EPL_2_0.url())
private static final LicenseMetadata LICENSE_LGPL21 = new LicenseMetadata('GNU Lesser General Public License v2.1 or later', LicenseId.LGPL_2_1_OR_LATER.url())
private static final LicenseMetadata LICENSE_MIT = new LicenseMetadata('MIT License', LicenseId.MIT.url())
private static final LicenseMetadata LICENSE_BSD2 = new LicenseMetadata('BSD 2-Clause FreeBSD License', LicenseId.BSD_2_CLAUSE_FREEBSD.url())
private static final LicenseMetadata LICENSE_BSD3 = new LicenseMetadata('BSD 3-Clause "New" or "Revised" License', LicenseId.BSD_3_CLAUSE.url())

private static final Map<LicenseId, LicenseMetadata> LICENSES_MAP = [
(LicenseId.APACHE_2_0) : LICENSE_APACHE_TWO,
(LicenseId.EPL_1_0) : LICENSE_EPL1,
(LicenseId.EPL_2_0) : LICENSE_EPL2,
(LicenseId.LGPL_2_1_OR_LATER) : LICENSE_LGPL21,
(LicenseId.MIT) : LICENSE_MIT,
(LicenseId.BSD_2_CLAUSE_FREEBSD): LICENSE_BSD2,
(LicenseId.BSD_3_CLAUSE) : LICENSE_BSD3
]

private static final Map<Object, List<Object>> DEFAULT_ALIASES = [
(LICENSE_APACHE_TWO): ['The Apache Software License, Version 2.0',
'The Apache Software License, version 2.0',
'Apache Software License - Version 2.0',
'Apache Software License - version 2.0',
'the Apache License, ASL Version 2.0',
'The Apache License, Version 2.0',
'The Apache License Version 2.0',
'Apache License, Version 2.0',
'Apache License, version 2.0',
'Apache License Version 2.0',
'Apache License version 2.0',
'The Apache License 2.0',
'Apache 2.0 License',
'Apache License 2.0',
'Apache 2.0',
'Apache-2.0',
'Apache 2',
LICENSE_APACHE_TWO],
(LICENSE_EPL1) : ['Eclipse Public License - Version 1.0',
'Eclipse Public License v1.0',
'Eclipse Public License 1.0',
'Eclipse Public License',
'EPL v1.0',
'EPL 1.0',
'EPL-1.0',
LICENSE_EPL1],
(LICENSE_EPL2) : ['Eclipse Public License v2.0',
'Eclipse Public License 2.0',
'EPL v2.0',
'EPL 2.0',
'EPL-2.0',
LICENSE_EPL2],
(LICENSE_LGPL21) : ['GNU Library General Public License v2.1 or later',
'GNU Lesser General Public License v2.1 or later',
'GNU Lesser General Public License, Version 2.1',
'LGPL 2.1',
'LGPL-2.1',
LICENSE_LGPL21],
(LICENSE_MIT) : ['The MIT License',
'The MIT license',
'MIT License',
'MIT license',
'MIT',
LICENSE_MIT],
(LICENSE_BSD2) : ['BSD 2-Clause FreeBSD License',
'The BSD License',
'The BSD license',
LICENSE_BSD2],
(LICENSE_BSD3) : ['BSD 3-Clause "New" or "Revised" License',
'3-Clause BSD License',
'3-Clause BSD license',
'Revised BSD License',
'Revised BSD license',
'BSD Revised License',
'BSD Revised license',
'New BSD License',
'New BSD license',
'BSD New License',
'BSD New license',
'BSD 3-Clause',
'BSD 3-clause',
LICENSE_BSD3]
]

Project project

void apply(Project project) {
Expand Down Expand Up @@ -81,10 +164,11 @@ class LicensePlugin implements Plugin<Project> {
project.plugins.apply(nl.javadude.gradle.plugins.license.LicensePlugin)
}

preconfigureDownloadLicensesExtension(project)
preConfigureDownloadLicensesExtension(project)

project.afterEvaluate {
configureLicenseExtension(project)
postConfigureDownloadLicensesExtension(project)
}

if (isRootProject(project) && !project.childProjects.isEmpty()) {
Expand Down Expand Up @@ -132,80 +216,40 @@ class LicensePlugin implements Plugin<Project> {
licenseExtension.exclude 'META-INF/services/*'
}

private void preconfigureDownloadLicensesExtension(Project project) {
private void preConfigureDownloadLicensesExtension(Project project) {
DownloadLicensesExtension extension = project.extensions.findByType(DownloadLicensesExtension)
extension.aliases = new LinkedHashMap<>(DEFAULT_ALIASES)
}

private void postConfigureDownloadLicensesExtension(Project project) {
ProjectConfigurationExtension mergedConfiguration = project.ext.mergedConfiguration

Map<Object, List<Object>> defaultAliases = new LinkedHashMap<>(DEFAULT_ALIASES)
mergedConfiguration.license.licenses.licenses.each { license ->
if (license.id && license.aliases) {
LicenseMetadata licenseMetadata = LICENSES_MAP.get(license.id)
if (!licenseMetadata) {
licenseMetadata = new LicenseMetadata(license.name, license.url)
LICENSES_MAP.put(license.id, licenseMetadata)
}

List<Object> aliases = defaultAliases.get(licenseMetadata)
if(!aliases) {
aliases = []
defaultAliases.put(licenseMetadata, aliases)
}
List<String> combined = aliases + license.aliases
combined = combined.unique() - aliases
aliases.addAll(combined)
}
}

DownloadLicensesExtension extension = project.extensions.findByType(DownloadLicensesExtension)
LicenseMetadata apacheTwo = extension.license('The Apache Software License, Version 2.0', LicenseId.APACHE_2_0.url())
LicenseMetadata epl1 = extension.license('Eclipse Public License v1.0', LicenseId.EPL_1_0.url())
LicenseMetadata epl2 = extension.license('Eclipse Public License v2.0', LicenseId.EPL_2_0.url())
LicenseMetadata lgpl21 = extension.license('GNU Lesser General Public License v2.1 or later', LicenseId.LGPL_2_1_OR_LATER.url())
LicenseMetadata mit = extension.license('MIT License', LicenseId.MIT.url())
LicenseMetadata bsd2 = extension.license('BSD 2-Clause FreeBSD License', LicenseId.BSD_2_CLAUSE_FREEBSD.url())
LicenseMetadata bsd3 = extension.license('BSD 3-Clause "New" or "Revised" License', LicenseId.BSD_3_CLAUSE.url())

extension.aliases = [
(apacheTwo): ['The Apache Software License, Version 2.0',
'The Apache Software License, version 2.0',
'Apache Software License - Version 2.0',
'Apache Software License - version 2.0',
'the Apache License, ASL Version 2.0',
'The Apache License, Version 2.0',
'The Apache License Version 2.0',
'Apache License, Version 2.0',
'Apache License, version 2.0',
'Apache License Version 2.0',
'Apache License version 2.0',
'The Apache License 2.0',
'Apache 2.0 License',
'Apache License 2.0',
'Apache 2.0',
'Apache-2.0',
'Apache 2',
apacheTwo],
(epl1) : ['Eclipse Public License - Version 1.0',
'Eclipse Public License v1.0',
'Eclipse Public License 1.0',
'Eclipse Public License',
'EPL v1.0',
'EPL 1.0',
'EPL-1.0',
epl1],
(epl2) : ['Eclipse Public License v2.0',
'Eclipse Public License 2.0',
'EPL v2.0',
'EPL 2.0',
'EPL-2.0',
epl1],
(lgpl21) : ['GNU Library General Public License v2.1 or later',
'GNU Lesser General Public License v2.1 or later',
'GNU Lesser General Public License, Version 2.1',
'LGPL 2.1',
'LGPL-2.1',
epl1],
(mit) : ['The MIT License',
'The MIT license',
'MIT License',
'MIT license',
'MIT',
mit],
(bsd2) : ['BSD 2-Clause FreeBSD License',
'The BSD License',
'The BSD license',
bsd2],
(bsd3) : ['BSD 3-Clause "New" or "Revised" License',
'3-Clause BSD License',
'3-Clause BSD license',
'Revised BSD License',
'Revised BSD license',
'BSD Revised License',
'BSD Revised license',
'New BSD License',
'New BSD license',
'BSD New License',
'BSD New license',
'BSD 3-Clause',
'BSD 3-clause',
bsd3]
]
extension.aliases = new LinkedHashMap<>(defaultAliases)

project.tasks.withType(DownloadLicenses) { DownloadLicenses task ->
task.aliases = defaultAliases
}
}

private void configureAggregateLicenseReportTask(Project project) {
Expand Down

0 comments on commit 8dd5ef9

Please sign in to comment.