diff options
author | cushon <cushon@google.com> | 2017-04-04 20:40:34 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2017-04-05 15:19:06 +0200 |
commit | 664a38f48f0172bf6b3ca6e81b54cb85167850fa (patch) | |
tree | 5dd103e10502628e21724842f4e1e5a3513f44a8 /src/java_tools/singlejar | |
parent | b5bb3a21bca352bf0e7ed8f5fe866369979be272 (diff) |
Create parent directories for resource entries in singlejar
This mirrors the change to the c++ implementation made in
https://github.com/bazelbuild/bazel/commit/874852ec52f9e03242fb4eccb41c2ea3f3036ac3.
PiperOrigin-RevId: 152173345
Diffstat (limited to 'src/java_tools/singlejar')
-rw-r--r-- | src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java | 16 | ||||
-rw-r--r-- | src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java | 40 |
2 files changed, 38 insertions, 18 deletions
diff --git a/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java b/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java index a7b7a10ffa..fb1b99714b 100644 --- a/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java +++ b/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java @@ -14,6 +14,8 @@ package com.google.devtools.build.singlejar; +import static com.google.devtools.build.singlejar.ZipCombiner.DOS_EPOCH; + import com.google.devtools.build.singlejar.DefaultJarEntryFilter.PathFilter; import com.google.devtools.build.singlejar.ZipCombiner.OutputMode; import java.io.ByteArrayInputStream; @@ -205,7 +207,8 @@ public class SingleJar { // Copy the resources into the jar file. for (String resource : resources) { - String from, to; + String from; + String to; int i = resource.indexOf(':'); if (i < 0) { to = from = resource; @@ -217,6 +220,17 @@ public class SingleJar { System.err.println("File " + from + " at " + to + " clashes with a previous file"); continue; } + + // Add parent directory entries. + int idx = to.indexOf('/'); + while (idx != -1) { + String dir = to.substring(0, idx + 1); + if (!combiner.containsFile(dir)) { + combiner.addDirectory(dir, DOS_EPOCH); + } + idx = to.indexOf('/', idx + 1); + } + combiner.addFile(to, date, fileSystem.getInputStream(from)); } diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java index 763c097b81..4b66d13e51 100644 --- a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java +++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/SingleJarTest.java @@ -22,11 +22,6 @@ import static org.junit.Assert.fail; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.devtools.build.singlejar.FakeZipFile.ByteValidator; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -34,6 +29,9 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.jar.JarFile; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; /** * Unit tests for {@link SingleJar}. @@ -563,6 +561,8 @@ public class SingleJarTest { .addEntry(JarFile.MANIFEST_NAME, new ManifestValidator( "Manifest-Version: 1.0", "Created-By: blaze-singlejar")) + .addEntry("c/", (String) null) + .addEntry("c/b/", (String) null) .addEntry("c/b/a", "Test"); expectedResult.assertSame(mockFs.toByteArray()); } @@ -574,12 +574,15 @@ public class SingleJarTest { SingleJar singleJar = new SingleJar(mockFs); singleJar.run(ImmutableList.of("--output", "output.jar", "--exclude_build_data", "--resources", "a/b/c")); - FakeZipFile expectedResult = new FakeZipFile() - .addEntry("META-INF/", EXTRA_FOR_META_INF) - .addEntry(JarFile.MANIFEST_NAME, new ManifestValidator( - "Manifest-Version: 1.0", - "Created-By: blaze-singlejar")) - .addEntry("a/b/c", "Test"); + FakeZipFile expectedResult = + new FakeZipFile() + .addEntry("META-INF/", EXTRA_FOR_META_INF) + .addEntry( + JarFile.MANIFEST_NAME, + new ManifestValidator("Manifest-Version: 1.0", "Created-By: blaze-singlejar")) + .addEntry("a/", (String) null) + .addEntry("a/b/", (String) null) + .addEntry("a/b/c", "Test"); expectedResult.assertSame(mockFs.toByteArray()); } @@ -604,12 +607,15 @@ public class SingleJarTest { SingleJar singleJar = new SingleJar(mockFs); singleJar.run(ImmutableList.of("--output", "output.jar", "--exclude_build_data", "--warn_duplicate_resources", "--resources", "a/b/c", "a/b/c")); - FakeZipFile expectedResult = new FakeZipFile() - .addEntry("META-INF/", EXTRA_FOR_META_INF) - .addEntry(JarFile.MANIFEST_NAME, new ManifestValidator( - "Manifest-Version: 1.0", - "Created-By: blaze-singlejar")) - .addEntry("a/b/c", "Test"); + FakeZipFile expectedResult = + new FakeZipFile() + .addEntry("META-INF/", EXTRA_FOR_META_INF) + .addEntry( + JarFile.MANIFEST_NAME, + new ManifestValidator("Manifest-Version: 1.0", "Created-By: blaze-singlejar")) + .addEntry("a/", (String) null) + .addEntry("a/b/", (String) null) + .addEntry("a/b/c", "Test"); expectedResult.assertSame(mockFs.toByteArray()); } |