Skip to content

Commit

Permalink
Skip non-class files in the exclusions processing (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnybod authored May 3, 2024
1 parent f19799a commit 57f3fa7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,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 @@ -141,7 +140,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 @@ -319,7 +317,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 @@ -331,6 +329,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 @@ -615,6 +615,35 @@ public void mergedJarServiceProviderFilePrependsLines() throws IOException {
assertEquals(prepend + "\ncom.example.Foo\n", 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"));

}

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

0 comments on commit 57f3fa7

Please sign in to comment.