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

Support third party licenses #185

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions oss-licenses-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,6 +61,9 @@ abstract class LicensesTask extends DefaultTask {
@InputFile
abstract RegularFileProperty getDependenciesJson()

@InputFiles
File[] thirdPartyLicenses

@OutputDirectory
File rawResourceDir

Expand Down Expand Up @@ -104,6 +108,8 @@ abstract class LicensesTask extends DefaultTask {
}
}

addThirdPartyLicenses();

writeMetadata()
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class OssLicensesPlugin implements Plugin<Project> {
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",
Expand All @@ -55,6 +57,7 @@ class OssLicensesPlugin implements Plugin<Project> {
it.rawResourceDir = rawResourceDir
it.licenses = licensesFile
it.licensesMetadata = licensesMetadataFile
it.thirdPartyLicenses = thirdPartyLicenses
}.get()
logger.debug("Created task ${licenseTask.name}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test