From 5a8cc9b77bf0b5da165e5f38cf10a46e4b72fe10 Mon Sep 17 00:00:00 2001 From: YOUNG HO CHA Date: Tue, 21 Apr 2020 13:28:39 +0900 Subject: [PATCH 1/6] Add 'third_party_licenses' directory contents when generate licenses --- .../gms/oss/licenses/plugin/LicensesTask.groovy | 13 +++++++++++++ .../oss/licenses/plugin/OssLicensesPlugin.groovy | 3 +++ 2 files changed, 16 insertions(+) diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy index 851846b5..ac7a1124 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy @@ -20,6 +20,7 @@ import groovy.json.JsonSlurper import org.gradle.api.DefaultTask import org.gradle.api.file.RegularFileProperty import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction @@ -60,6 +61,9 @@ abstract class LicensesTask extends DefaultTask { @InputFile abstract RegularFileProperty getDependenciesJson() + @InputFiles + File[] thirdPartyLicenses + @OutputDirectory File rawResourceDir @@ -104,6 +108,10 @@ abstract class LicensesTask extends DefaultTask { } } + for (filename in thirdPartyLicenses) { + addLicensesFromFile(filename) + } + writeMetadata() } @@ -266,6 +274,11 @@ abstract class LicensesTask extends DefaultTask { } } + private void addLicensesFromFile(File filename) { + String licenseName = filename.getName() + appendLicense(licenseName, filename.getBytes()) + } + protected void appendDependency(String key, byte[] license) { appendDependency(new Dependency(key, key), license) } diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy index 0c377b15..dad159e3 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/OssLicensesPlugin.groovy @@ -47,6 +47,8 @@ class OssLicensesPlugin implements Plugin { def licensesFile = new File(rawResourceDir, "third_party_licenses") def licensesMetadataFile = new File(rawResourceDir, "third_party_license_metadata") + def thirdPartyLicensesDir = new File(project.rootProject.projectDir, "third_party_licenses") + def thirdPartyLicenses = thirdPartyLicensesDir.exists() ? thirdPartyLicensesDir.listFiles() : [] def licenseTask = project.tasks.register( "${variant.name}OssLicensesTask", @@ -55,6 +57,7 @@ class OssLicensesPlugin implements Plugin { it.rawResourceDir = rawResourceDir it.licenses = licensesFile it.licensesMetadata = licensesMetadataFile + it.thirdPartyLicenses = thirdPartyLicenses }.get() logger.debug("Created task ${licenseTask.name}") From fdad5edb05583360829acb9dae1d66cc8d6f30ae Mon Sep 17 00:00:00 2001 From: YOUNG HO CHA Date: Tue, 21 Apr 2020 16:56:52 +0900 Subject: [PATCH 2/6] Put third party licenses with sorted --- .../google/android/gms/oss/licenses/plugin/LicensesTask.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy index ac7a1124..bbfa2e27 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy @@ -108,7 +108,7 @@ abstract class LicensesTask extends DefaultTask { } } - for (filename in thirdPartyLicenses) { + for (filename in thirdPartyLicenses.sort{it.getName().toLowerCase()}) { addLicensesFromFile(filename) } From 33e5acdd0a309b5b537b461510b8e8ded8c015fb Mon Sep 17 00:00:00 2001 From: Joost Funke Kupper Date: Thu, 24 Jun 2021 14:29:30 +1000 Subject: [PATCH 3/6] Third party license instructions --- oss-licenses-plugin/README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/oss-licenses-plugin/README.md b/oss-licenses-plugin/README.md index d96ea5b3..fbb42cf6 100755 --- a/oss-licenses-plugin/README.md +++ b/oss-licenses-plugin/README.md @@ -12,8 +12,8 @@ third_party_licenses.json and third_party_licenses.txt files in the distributed The plugin will generate two text files based on the gathered licenses info: - * third_party_licenses - * third_party_licenses_metadata +- third_party_licenses +- third_party_licenses_metadata and registers them as raw resources so that it can be consumed by the play-services-oss-licenses library. @@ -73,3 +73,11 @@ display additional license information for that library. You can also set the title of the displayed activity: OssLicensesMenuActivity.setActivityTitle(getString(R.string.custom_license_title)); + +### Include third party licenses + +You may come across the case where libraries don't include the licenses component +in the POM file, meaning it will be excluded from the list. To solve this you can +create a `third_party_licenses` directory in the root of your project and include +any license file as a plain text file, without the file extension. These files will +be included in the list using the file name as the list item's text. From 76962d5e901dab036b47ea35f4de1682c8f9e7e9 Mon Sep 17 00:00:00 2001 From: Joost Funke Kupper Date: Tue, 7 Sep 2021 13:23:22 +1000 Subject: [PATCH 4/6] Fix function call name --- .../google/android/gms/oss/licenses/plugin/LicensesTask.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy index bbfa2e27..cd9b3b12 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy @@ -276,7 +276,7 @@ abstract class LicensesTask extends DefaultTask { private void addLicensesFromFile(File filename) { String licenseName = filename.getName() - appendLicense(licenseName, filename.getBytes()) + appendDependency(licenseName, filename.getBytes()) } protected void appendDependency(String key, byte[] license) { From 0655153be44093080afc8408cca0e965ff44b487 Mon Sep 17 00:00:00 2001 From: Joost Funke Kupper Date: Wed, 6 Oct 2021 15:47:32 +1100 Subject: [PATCH 5/6] Add test to verify third party service directory --- .../gms/oss/licenses/plugin/LicensesTask.groovy | 10 +++++++--- .../gms/oss/licenses/plugin/LicensesTaskTest.java | 15 +++++++++++++++ .../test/resources/thirdPartyLicenses/license1 | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 oss-licenses-plugin/src/test/resources/thirdPartyLicenses/license1 diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy index cd9b3b12..be9ed9ae 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy @@ -108,9 +108,7 @@ abstract class LicensesTask extends DefaultTask { } } - for (filename in thirdPartyLicenses.sort{it.getName().toLowerCase()}) { - addLicensesFromFile(filename) - } + addThirdPartyLicenses(); writeMetadata() } @@ -306,6 +304,12 @@ abstract class LicensesTask extends DefaultTask { start += content.length } + protected void addThirdPartyLicenses() { + for (filename in thirdPartyLicenses.sort{it.getName().toLowerCase()}) { + addLicensesFromFile(filename) + } + } + protected void writeMetadata() { for (entry in licensesMap) { licensesMetadata.append(entry.value, UTF_8) diff --git a/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/LicensesTaskTest.java b/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/LicensesTaskTest.java index 2c8f80e2..497cc8b2 100644 --- a/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/LicensesTaskTest.java +++ b/oss-licenses-plugin/src/test/java/com/google/android/gms/oss/licenses/plugin/LicensesTaskTest.java @@ -348,4 +348,19 @@ public void action_absentDependencies_rendersAbsentData() throws Exception { assertEquals(line, LicensesTask.ABSENT_DEPENDENCY_TEXT); } + @Test + public void testThirdPartyLicenses() throws IOException { + File thirdPartyLicensesDir = new File(BASE_DIR + "/thirdPartyLicenses"); + File[] thirdPartyLicenses = thirdPartyLicensesDir.listFiles(); + assert thirdPartyLicenses != null; + licensesTask.setThirdPartyLicenses(thirdPartyLicenses); + licensesTask.addThirdPartyLicenses(); + + String expected = "test" + LINE_BREAK; + String content = new String(Files.readAllBytes(thirdPartyLicenses[0].toPath()), UTF_8); + + assertThat(licensesTask.licensesMap.size(), is(1)); + assertTrue(licensesTask.licensesMap.containsKey("license1")); + assertEquals(expected, content); + } } diff --git a/oss-licenses-plugin/src/test/resources/thirdPartyLicenses/license1 b/oss-licenses-plugin/src/test/resources/thirdPartyLicenses/license1 new file mode 100644 index 00000000..9daeafb9 --- /dev/null +++ b/oss-licenses-plugin/src/test/resources/thirdPartyLicenses/license1 @@ -0,0 +1 @@ +test From 0ed30b51475d452f3098e8785a7ea35e758752c9 Mon Sep 17 00:00:00 2001 From: Joost Funke Kupper Date: Tue, 8 Mar 2022 15:18:26 +1100 Subject: [PATCH 6/6] Guard against empty thirdPartyLicenses --- .../google/android/gms/oss/licenses/plugin/LicensesTask.groovy | 3 +++ 1 file changed, 3 insertions(+) diff --git a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy index be9ed9ae..c564b255 100644 --- a/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy +++ b/oss-licenses-plugin/src/main/groovy/com/google/android/gms/oss/licenses/plugin/LicensesTask.groovy @@ -305,6 +305,9 @@ abstract class LicensesTask extends DefaultTask { } protected void addThirdPartyLicenses() { + if (thirdPartyLicenses == null) { + return + } for (filename in thirdPartyLicenses.sort{it.getName().toLowerCase()}) { addLicensesFromFile(filename) }