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. 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..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 @@ -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,8 @@ abstract class LicensesTask extends DefaultTask { } } + addThirdPartyLicenses(); + writeMetadata() } @@ -266,6 +272,11 @@ abstract class LicensesTask extends DefaultTask { } } + private void addLicensesFromFile(File filename) { + String licenseName = filename.getName() + appendDependency(licenseName, filename.getBytes()) + } + protected void appendDependency(String key, byte[] license) { appendDependency(new Dependency(key, key), license) } @@ -293,6 +304,15 @@ abstract class LicensesTask extends DefaultTask { start += content.length } + protected void addThirdPartyLicenses() { + if (thirdPartyLicenses == null) { + return + } + 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/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}") 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