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

skip non-class files in the exclusions processing for MergeJars #1125

Open
wants to merge 3 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public static void main(String[] args) throws IOException {
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");

Map<String, Set<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 @@ -132,7 +131,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 @@ -295,7 +293,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 @@ -307,6 +305,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 @@ -553,6 +553,103 @@ public void mergedJarManifestSpecialAttributesAreHandled() throws IOException {
}
}

@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