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

reimplement license retention on service provider files #11

Merged
merged 8 commits into from
May 10, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -118,7 +117,7 @@ public static void main(String[] args) throws IOException {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");

Map<String, Set<String>> allServices = new TreeMap<>();
Map<String, List<String>> allServices = new TreeMap<>();
Set<String> excludedPaths = readExcludedClassNames(excludes);

// Ultimately, we want the entries in the output zip to be sorted
Expand Down Expand Up @@ -146,15 +145,9 @@ public static void main(String[] args) throws IOException {

if (entry.getName().startsWith("META-INF/services/") && !entry.isDirectory()) {
String servicesName = entry.getName().substring("META-INF/services/".length());
Set<String> services =
allServices.computeIfAbsent(servicesName, key -> new TreeSet<>());
String content = new String(ByteStreams.toByteArray(zis));
List<String> commentsRemoved = Arrays
.stream(content.split("\n"))
.filter(l -> !l.trim().startsWith("#"))
.filter(l -> !l.isEmpty())
.collect(Collectors.toList());
services.addAll(commentsRemoved);
List<String> services =
allServices.computeIfAbsent(servicesName, key -> new ArrayList<>());
services.add(new String(ByteStreams.toByteArray(zis)));
continue;
}

Expand Down Expand Up @@ -216,19 +209,11 @@ public static void main(String[] args) throws IOException {
jos.closeEntry();
createdDirectories.add(entry.getName());
}
for (Map.Entry<String, Set<String>> kv : allServices.entrySet()) {
for (Map.Entry<String, List<String>> kv : allServices.entrySet()) {
entry = new StableZipEntry("META-INF/services/" + kv.getKey());
bos = new ByteArrayOutputStream();
List<String> toWrite = new ArrayList<>();

if (prependServices != null) {
String prepend = Files.readString(prependServices);
toWrite.addAll(Arrays.asList(prepend.split("\n")));
toWrite.add("");
}

toWrite.addAll(kv.getValue());
bos.write(String.join("\n", toWrite).getBytes());
bos.write(String.join("\n\n", kv.getValue()).getBytes());
bos.write("\n".getBytes());
entry.setSize(bos.size());
jos.putNextEntry(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,19 @@ public void mergedJarManifestSpecialAttributesAreHandled() throws IOException {
}

@Test
public void mergedJarServiceProviderFileRemovesCommentLines() throws IOException {
public void mergedJarServiceProviderFilePreservesComments() throws IOException {
Path inputOne = temp.newFile("one.jar").toPath();
String inputOneContents = "# This is a comment\n# This is another comment\ncom.example.Foo";
createJar(
inputOne,
ImmutableMap.of("META-INF/services/com.example.ServiceProvider", "# This is a comment")
ImmutableMap.of("META-INF/services/com.example.ServiceProvider", inputOneContents)
);

Path inputTwo = temp.newFile("two.jar").toPath();
String inputTwoContents = "# My License\ncom.example.Bar";
createJar(
inputTwo,
ImmutableMap.of("META-INF/services/com.example.ServiceProvider", "com.example.Foo")
ImmutableMap.of("META-INF/services/com.example.ServiceProvider", inputTwoContents)
);

Path outputJar = temp.newFile("out.jar").toPath();
Expand All @@ -574,45 +576,14 @@ public void mergedJarServiceProviderFileRemovesCommentLines() throws IOException
"--output", outputJar.toAbsolutePath().toString(),
"--sources", inputOne.toAbsolutePath().toString(),
"--sources", inputTwo.toAbsolutePath().toString(),
});

Map<String, String> contents = readJar(outputJar);

assertEquals("com.example.Foo\n", contents.get("META-INF/services/com.example.ServiceProvider"));
}

@Test
public void mergedJarServiceProviderFilePrependsLines() throws IOException {
Path inputOne = temp.newFile("one.jar").toPath();
createJar(
inputOne,
ImmutableMap.of("META-INF/services/com.example.ServiceProvider", "# This is a comment")
);

Path inputTwo = temp.newFile("two.jar").toPath();
createJar(
inputTwo,
ImmutableMap.of("META-INF/services/com.example.ServiceProvider", "com.example.Foo")
);

String prepend = "# This is a LICENSE\n # It should be kept\n";
Path inputThree = temp.newFile("LICENSE").toPath();
Files.write(inputThree, prepend.getBytes(UTF_8));

Path outputJar = temp.newFile("out.jar").toPath();

MergeJars.main(
new String[] {
"--output", outputJar.toAbsolutePath().toString(),
"--sources", inputOne.toAbsolutePath().toString(),
"--sources", inputTwo.toAbsolutePath().toString(),
"--prepend_services", inputThree.toAbsolutePath().toString(),
}
);

Map<String, String> contents = readJar(outputJar);

assertEquals(prepend + "\ncom.example.Foo\n", contents.get("META-INF/services/com.example.ServiceProvider"));
String expected = String.join("\n\n", inputOneContents, inputTwoContents) + "\n";

assertEquals(expected, contents.get("META-INF/services/com.example.ServiceProvider"));
}

@Test
Expand Down