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

Use the library name from the POM file of the dependency #191

Merged
merged 11 commits into from
Aug 25, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LicensesTask extends DefaultTask {
protected int start = 0
protected Set<String> googleServiceLicenses = []
protected Map<String, String> licensesMap = [:]
protected Map<String, String> licenseOffsets = [:]

@InputFile
File dependenciesJson
Expand Down Expand Up @@ -165,7 +166,7 @@ class LicensesTask extends DefaultTask {
licensesZip.getInputStream(txtFile),
startValue,
lengthValue)
appendLicense(key, content)
appendDependency(key, content)
}
}
}
Expand Down Expand Up @@ -216,16 +217,19 @@ class LicensesTask extends DefaultTask {
return
}

String licenseKey = "${group}:${name}"
String libraryName = rootNode.name
rekire marked this conversation as resolved.
Show resolved Hide resolved
if (libraryName == null || libraryName.trim() == "") {
libraryName = "${group}:${name}"
}
if (rootNode.licenses.license.size() > 1) {
rootNode.licenses.license.each { node ->
String nodeName = node.name
String nodeUrl = node.url
appendLicense("${licenseKey} ${nodeName}", nodeUrl.getBytes(UTF_8))
rootNode.licenses.license.each { license ->
String licenseName = license.name
String licenseUrl = license.url
appendDependency("${libraryName} ${licenseName}", licenseUrl.getBytes(UTF_8))
}
} else {
String nodeUrl = rootNode.licenses.license.url
appendLicense(licenseKey, nodeUrl.getBytes(UTF_8))
appendDependency(libraryName, nodeUrl.getBytes(UTF_8))
}
}

Expand Down Expand Up @@ -255,14 +259,22 @@ class LicensesTask extends DefaultTask {
return ((ResolvedArtifactResult) artifacts[0]).getFile()
}

protected void appendLicense(String key, byte[] content) {
if (licensesMap.containsKey(key)) {
protected void appendDependency(String dependency, byte[] license) {
String licenseText = new String(license, UTF_8)
if (licensesMap.containsKey(dependency)) {
return
}

licensesMap.put(key, "${start}:${content.length}")
appendLicenseContent(content)
appendLicenseContent(LINE_SEPARATOR)
String offsets
if (licenseOffsets.containsKey(licenseText)) {
offsets = licenseOffsets.get(licenseText)
} else {
offsets = "${start}:${license.length}"
licenseOffsets.put(licenseText, offsets)
appendLicenseContent(license)
appendLicenseContent(LINE_SEPARATOR)
}
licensesMap.put(dependency, offsets)
}

protected void appendLicenseContent(byte[] content) {
Expand All @@ -280,5 +292,4 @@ class LicensesTask extends DefaultTask {
private static ModuleComponentIdentifier createModuleComponentIdentifier(String group, String name, String version) {
return new DefaultModuleComponentIdentifier(DefaultModuleIdentifier.newId(group, name), version)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void testAddLicensesFromPom() throws IOException {

String content = new String(Files.readAllBytes(licensesTask.getLicenses().toPath()), UTF_8);
String expected = "http://www.opensource.org/licenses/mit-license.php" + LINE_BREAK;
assertTrue(licensesTask.licensesMap.containsKey("groupA:deps1"));
assertTrue(licensesTask.licensesMap.containsKey("groupA deps1"));
assertEquals(expected, content);
}

Expand All @@ -152,8 +152,8 @@ public void testAddLicensesFromPom_withoutDuplicate() throws IOException {
+ LINE_BREAK;

assertThat(licensesTask.licensesMap.size(), is(2));
assertTrue(licensesTask.licensesMap.containsKey("groupA:deps1"));
assertTrue(licensesTask.licensesMap.containsKey("groupB:deps2"));
assertTrue(licensesTask.licensesMap.containsKey("groupA deps1"));
assertTrue(licensesTask.licensesMap.containsKey("groupB deps2"));
assertEquals(expected, content);
}

Expand All @@ -172,16 +172,14 @@ public void testAddLicensesFromPom_withMultiple() throws IOException {
String content = new String(Files.readAllBytes(licensesTask.getLicenses().toPath()), UTF_8);
String expected =
"http://www.opensource.org/licenses/mit-license.php"
rekire marked this conversation as resolved.
Show resolved Hide resolved
+ LINE_BREAK
+ "http://www.opensource.org/licenses/mit-license.php"
+ LINE_BREAK
+ "https://www.apache.org/licenses/LICENSE-2.0"
+ LINE_BREAK;

assertThat(licensesTask.licensesMap.size(), is(3));
assertTrue(licensesTask.licensesMap.containsKey("groupA:deps1"));
assertTrue(licensesTask.licensesMap.containsKey("groupE:deps5 MIT License"));
assertTrue(licensesTask.licensesMap.containsKey("groupE:deps5 Apache License 2.0"));
assertTrue(licensesTask.licensesMap.containsKey("groupA deps1"));
assertTrue(licensesTask.licensesMap.containsKey("groupE deps5 MIT License"));
assertTrue(licensesTask.licensesMap.containsKey("groupE deps5 Apache License 2.0"));
rekire marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(expected, content);
}

Expand All @@ -201,7 +199,7 @@ public void testAddLicensesFromPom_withDuplicate() throws IOException {
String expected = "http://www.opensource.org/licenses/mit-license.php" + LINE_BREAK;

assertThat(licensesTask.licensesMap.size(), is(1));
assertTrue(licensesTask.licensesMap.containsKey("groupA:deps1"));
assertTrue(licensesTask.licensesMap.containsKey("groupA deps1"));
assertEquals(expected, content);
}

Expand Down Expand Up @@ -284,7 +282,7 @@ public void testAddGooglePlayServiceLicenses_withoutDuplicate() throws IOExcepti

@Test
public void testAppendLicense() throws IOException {
licensesTask.appendLicense("license1", "test".getBytes(UTF_8));
licensesTask.appendDependency("license1", "test".getBytes(UTF_8));

String expected = "test" + LINE_BREAK;
String content = new String(Files.readAllBytes(licensesTask.getLicenses().toPath()), UTF_8);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>groupA</groupId>
<artifactId>groupA-deps1</artifactId>
<groupId>groupB</groupId>
<artifactId>groupB-deps2</artifactId>
<version>1</version>
<name>groupA deps1</name>
<description>groupA deps1</description>
<name>groupB deps2</name>
<description>groupB deps2</description>

<licenses>
<license>
Expand Down