| diff --git a/private/tools/java/rules/jvm/external/jar/MergeJars.java b/private/tools/java/rules/jvm/external/jar/MergeJars.java |
| index 3efe36a1..d6ce9cc7 100644 |
| --- a/private/tools/java/rules/jvm/external/jar/MergeJars.java |
| +++ b/private/tools/java/rules/jvm/external/jar/MergeJars.java |
| @@ -118,7 +118,6 @@ public static void main(String[] args) throws IOException { |
| Map<String, Path> fileToSourceJar = new TreeMap<>(); |
| Map<String, byte[]> fileHashCodes = new HashMap<>(); |
| |
| - Set<String> createdDirectories = new HashSet<>(); |
| for (Path source : sources) { |
| try (InputStream fis = Files.newInputStream(source); |
| ZipInputStream zis = new ZipInputStream(fis)) { |
| @@ -144,10 +143,7 @@ public static void main(String[] args) throws IOException { |
| continue; |
| } |
| |
| - if (entry.isDirectory() && createdDirectories.add(entry.getName())) { |
| - fileToSourceJar.put(entry.getName(), source); |
| - createdDirectories.add(entry.getName()); |
| - } else { |
| + if (!entry.isDirectory()) { |
| // Duplicate files, however may not be. We need the hash to determine |
| // whether we should do anything. |
| byte[] hash = hash(zis); |
| @@ -175,6 +171,9 @@ public static void main(String[] args) throws IOException { |
| |
| // Now create the output jar |
| Files.createDirectories(out.getParent()); |
| + |
| + Set<String> createdDirectories = new HashSet<>(); |
| + |
| try (OutputStream os = Files.newOutputStream(out); |
| JarOutputStream jos = new JarOutputStream(os)) { |
| jos.setMethod(DEFLATED); |
| diff --git a/tests/com/jvm/external/jar/MergeJarsTest.java b/tests/com/jvm/external/jar/MergeJarsTest.java |
| index 2421ff47..f9c76bbd 100644 |
| --- a/tests/com/jvm/external/jar/MergeJarsTest.java |
| +++ b/tests/com/jvm/external/jar/MergeJarsTest.java |
| @@ -435,6 +435,26 @@ public void orderingOfAutomaticallyCreatedDirectoriesIsConduciveToSensibleUnpack |
| assertTrue(indexOfQux > indexOfBaz); |
| } |
| |
| + @Test |
| + public void shouldCreateIntermediateDirectoriesEvenIfTheyExistInTheSourceJar() throws IOException { |
| + Path input = temp.newFile("example.jar").toPath(); |
| + createJar(input, ImmutableMap.of( |
| + "foo/", "", |
| + "foo/bar", "", |
| + "foo/bar/baz.txt", "cheese")); |
| + |
| + Path output = temp.newFile("out.jar").toPath(); |
| + |
| + MergeJars.main(new String[] { |
| + "--output", output.toAbsolutePath().toString(), |
| + "--sources", input.toAbsolutePath().toString(), |
| + }); |
| + |
| + List<String> dirNames = readDirNames(output); |
| + assertTrue(dirNames.contains("foo/")); |
| + assertTrue(dirNames.contains("foo/bar/")); |
| + } |
| + |
| @Test |
| public void mergedJarManifestSpecialAttributesAreHandled() throws IOException { |
| // This is required to allow JarInputStream to read the manifest properly |
| @@ -471,7 +491,9 @@ private void createJar(Path outputTo, Map<String, String> pathToContents) throws |
| for (Map.Entry<String, String> entry : pathToContents.entrySet()) { |
| ZipEntry ze = new StableZipEntry(entry.getKey()); |
| zos.putNextEntry(ze); |
| - zos.write(entry.getValue().getBytes(UTF_8)); |
| + if (!ze.isDirectory()) { |
| + zos.write(entry.getValue().getBytes(UTF_8)); |
| + } |
| zos.closeEntry(); |
| } |
| } |