Skip to content
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 @@ -111,8 +111,7 @@ public static void main(String[] args) throws IOException {
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");

Map<String, List<String>> allServices = new TreeMap<>();
Set<String> excludedPaths = readExcludedFileNames(excludes);
Set<String> duplicateExceptions = Set.of("COPYRIGHT", "LICENSE", "NOTICE");
Set<String> excludedPaths = readExcludedClassNames(excludes);

// Ultimately, we want the entries in the output zip to be sorted
// so that we have a deterministic output.
Expand All @@ -133,7 +132,6 @@ public static void main(String[] args) throws IOException {

if ("META-INF/".equals(entry.getName())
|| (!entry.getName().startsWith("META-INF/")
&& !duplicateExceptions.contains(entry.getName())
&& excludedPaths.contains(entry.getName()))) {
continue;
}
Expand Down Expand Up @@ -297,7 +295,7 @@ private static void createDirectories(JarOutputStream jos, String name, Set<Stri
}
}

private static Set<String> readExcludedFileNames(Set<Path> excludes) throws IOException {
private static Set<String> readExcludedClassNames(Set<Path> excludes) throws IOException {
Set<String> paths = new HashSet<>();

for (Path exclude : excludes) {
Expand All @@ -309,6 +307,9 @@ private static Set<String> readExcludedFileNames(Set<Path> excludes) throws IOEx
if (entry.isDirectory()) {
continue;
}
if (!entry.getName().endsWith(".class")) {
continue;
}

String name = entry.getName();
paths.add(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,103 @@ public void mergedJarServiceProviderFilePreservesComments() throws IOException {
assertEquals(expected, contents.get("META-INF/services/com.example.ServiceProvider"));
}

@Test
public void mergedJarKeepsNonClassFiles() throws IOException {
Path inputOne = temp.newFile("one.jar").toPath();
createJar(
inputOne,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR,stdout")
);

Path excludeOne = temp.newFile("two.jar").toPath();
createJar(
excludeOne,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR")
);

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

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

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

assertEquals("log4j.rootLogger=ERROR,stdout", contents.get("log4j.properties"));
}

@Test
public void mergedJarKeepsNonClassFilesDefaultDuplicateStrategy() throws IOException {
Path inputOne = temp.newFile("one.jar").toPath();
createJar(
inputOne,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR,stdout")
);
Path inputTwo = temp.newFile("two.jar").toPath();
createJar(
inputTwo,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR,stdout,stderr")
);

Path excludeOne = temp.newFile("three.jar").toPath();
createJar(
excludeOne,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR")
);

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

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

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

assertEquals("log4j.rootLogger=ERROR,stdout,stderr", contents.get("log4j.properties"));
}

@Test
public void mergedJarKeepsNonClassFilesFirstWinsStrategy() throws IOException {
Path inputOne = temp.newFile("one.jar").toPath();
createJar(
inputOne,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR,stdout")
);
Path inputTwo = temp.newFile("two.jar").toPath();
createJar(
inputTwo,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR,stdout,stderr")
);

Path excludeOne = temp.newFile("three.jar").toPath();
createJar(
excludeOne,
ImmutableMap.of("log4j.properties", "log4j.rootLogger=ERROR")
);

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

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

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

assertEquals("log4j.rootLogger=ERROR,stdout", contents.get("log4j.properties"));
}

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