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

Retain comments / licenses in service provider files when merging jars #1123

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
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 @@ -109,7 +110,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 = readExcludedFileNames(excludes);
Set<String> duplicateExceptions = Set.of("COPYRIGHT", "LICENSE", "NOTICE");

Expand Down Expand Up @@ -139,10 +140,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));
services.addAll(Arrays.asList(content.split("\n")));
List<String> services =
allServices.computeIfAbsent(servicesName, key -> new ArrayList<>());
services.add(new String(ByteStreams.toByteArray(zis)));
continue;
}

Expand Down Expand Up @@ -204,10 +204,12 @@ 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();
bos.write(String.join("\n", kv.getValue()).getBytes());

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

@Test
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", 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", inputTwoContents)
);

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

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

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

String expected = String.join("\n\n", inputOneContents, inputTwoContents) + "\n";

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

private void createJar(Path outputTo, Map<String, String> pathToContents) throws IOException {
try (OutputStream os = Files.newOutputStream(outputTo);
ZipOutputStream zos = new ZipOutputStream(os)) {
Expand Down