Skip to content

Commit 4816ddc

Browse files
authored
Use license aliases for a more consistent license report (#655)
1 parent 799d925 commit 4816ddc

4 files changed

Lines changed: 318 additions & 304 deletions

File tree

google-ads/build.gradle

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import groovy.json.JsonSlurper
2-
import com.google.common.collect.Sets
3-
41
/*
52
* Copyright 2020 Google LLC
63
*
@@ -20,10 +17,18 @@ import com.google.common.collect.Sets
2017
* depends on the google-ads-annotation-processing subproject.
2118
*/
2219

20+
21+
import com.google.common.collect.Sets
22+
import groovy.json.JsonSlurper
23+
24+
import java.util.stream.Collectors
25+
26+
import static nl.javadude.gradle.plugins.license.DownloadLicensesExtension.license
27+
2328
plugins {
24-
id 'com.google.api-ads.java-conventions'
25-
id 'com.google.protobuf' version '0.8.15'
26-
id "com.github.hierynomus.license-report" version "0.15.0"
29+
id 'com.google.api-ads.java-conventions'
30+
id 'com.google.protobuf' version '0.8.15'
31+
id "com.github.hierynomus.license-report" version "0.15.0"
2732
}
2833

2934
description = 'Google Ads API client library for Java'
@@ -115,6 +120,28 @@ task generateLicenses(type: nl.javadude.gradle.plugins.license.DownloadLicenses)
115120
// the exclusion is precise.
116121
excludeDependencies = rootProject.allprojects
117122
.collect { it.group + ':' + it.name + ':' + it.version }
123+
124+
// Sets an extension property that contains all allowed licenses and their
125+
// canonical names. Update this map if you need to add a new license.
126+
ext.allowedLicenses = [
127+
'apacheTwo' : license('Apache License, Version 2.0', 'http://opensource.org/licenses/Apache-2.0'),
128+
'bsdThreeClause' : license('3-Clause BSD License', 'https://opensource.org/licenses/BSD-3-Clause'),
129+
'mit' : license('MIT License', 'https://opensource.org/licenses/mit-license.php'),
130+
'cddlAndGplv2Cpe': license('CDDL + GPLv2 with classpath exception', 'https://github.com/javaee/javax.annotation/blob/master/LICENSE')
131+
]
132+
133+
// Specifies aliases for the known licenses so the report will use the
134+
// canonical name of each license.
135+
aliases = [
136+
(ext.allowedLicenses['apacheTwo']) : ['Apache License 2.0',
137+
'The Apache License, Version 2.0',
138+
'The Apache Software License, Version 2.0',
139+
'Apache 2.0',
140+
'Apache-2.0',
141+
'Apache 2'],
142+
(ext.allowedLicenses['bsdThreeClause']): ['BSD 3-clause', 'BSD', 'BSD New license'],
143+
(ext.allowedLicenses['mit']) : ['The MIT License', 'MIT license'],
144+
]
118145
}
119146

120147
task verifyLicenses() {
@@ -127,43 +154,30 @@ task verifyLicenses() {
127154
// contains the JSON file (among other files).
128155
def inputFile = tasks.generateLicenses.outputs.files.singleFile.listFiles().find { it.path.endsWith(".json") }
129156
def json = new JsonSlurper().parseText(inputFile.text)
130-
// Gets all the licenses except those for ourselves (which aren't set yet).
131-
def licenses = json
157+
// Gets the names of all the licenses except those for ourselves
158+
// (which aren't set yet).
159+
def licenseNames = json
132160
.dependencies
133161
.findAll { !it.name.startsWith("com.google.api-ads") }
134162
.licenses
135163
.name
136164
.flatten() as Set
137-
// Defines licenses which are allowed. Update this list if you need to add
138-
// a new license.
139-
def allowedLicenses = [
140-
"Apache License 2.0",
141-
"Apache License, Version 2.0",
142-
"The Apache License, Version 2.0",
143-
"The Apache Software License, Version 2.0",
144-
"Apache 2.0",
145-
"Apache-2.0",
146-
"Apache 2",
147-
"BSD",
148-
"BSD 3-clause",
149-
"3-Clause BSD License",
150-
"The MIT License",
151-
"MIT License",
152-
"MIT license",
153-
"BSD New license",
154-
"The New BSD License",
155-
"GNU General Public License, version 2 (GPL2), with the classpath exception",
156-
"CDDL + GPLv2 with classpath exception"
157-
] as Set
165+
// Gets the canonical names of allowed licenses from the
166+
// generateLicenses task.
167+
def allowedLicenseNames = tasks.generateLicenses.ext.allowedLicenses
168+
.values()
169+
.stream()
170+
.map { it.licenseName }
171+
.collect(Collectors.toSet())
158172
// Computes which licenses are in use but not permitted.
159-
def notAllowed = Sets.difference(licenses, allowedLicenses)
173+
def notAllowed = Sets.difference(licenseNames, allowedLicenseNames)
160174
if (!notAllowed.empty) {
161175
throw new GradleException(
162176
"""Licenses found which were not allowed: ${notAllowed}
163177
- Note that this script is not very smart. It doesn't attempt to do partial
164178
string comparison on the license name. It is possible that the new license is
165179
already allowed with a different spelling. If the new license is already allowed
166-
just add the new spelling to google-ads/build.gradle.
180+
just add the new spelling to the aliases in google-ads/build.gradle.
167181
- Otherwise you need to consider if we should accept this new license. For
168182
Googlers, there is a company policy for this available internally.""")
169183
}
@@ -274,11 +288,11 @@ task copyThirdPartyBinaries(type: Copy) {
274288
}
275289

276290
publishing {
277-
publications {
278-
maven(MavenPublication) { publication ->
279-
configurePom(publication,
280-
"Google Ads API client library for Java - main library",
281-
"Main library for the Google Ads API client library for Java")
291+
publications {
292+
maven(MavenPublication) { publication ->
293+
configurePom(publication,
294+
"Google Ads API client library for Java - main library",
295+
"Main library for the Google Ads API client library for Java")
296+
}
282297
}
283-
}
284298
}

0 commit comments

Comments
 (0)