Skip to content

Commit

Permalink
Do not include Target-Label in java_export manifests (bazel-contrib#766)
Browse files Browse the repository at this point in the history
Bazel labels are an internal detail of the project producing the jar.
  • Loading branch information
fmeum authored Oct 12, 2022
1 parent f6cec9c commit 762f94d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions private/tools/java/rules/jvm/external/jar/MergeJars.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public static void main(String[] args) throws IOException {
}

manifest.getMainAttributes().put(new Attributes.Name("Created-By"), "mergejars");
// Bazel labels are an internal detail of the project producing the merged
// jar and not useful for consumers.
manifest.getMainAttributes().remove(new Attributes.Name("Target-Label"));

// Now create the output jar
Files.createDirectories(out.getParent());
Expand Down
32 changes: 32 additions & 0 deletions tests/com/jvm/external/jar/MergeJarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -432,6 +435,35 @@ public void orderingOfAutomaticallyCreatedDirectoriesIsConduciveToSensibleUnpack
assertTrue(indexOfQux > indexOfBaz);
}

@Test
public void mergedJarManifestSpecialAttributesAreHandled() throws IOException {
// This is required to allow JarInputStream to read the manifest properly
Path inputOne = temp.newFile("one.jar").toPath();

Manifest firstManifest = new Manifest();
firstManifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
firstManifest.getMainAttributes().put(new Attributes.Name("First"), "foo");
firstManifest.getMainAttributes().put(new Attributes.Name("Target-Label"), "@secret_corp//:foo");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
firstManifest.write(bos);

createJar(
inputOne,
ImmutableMap.of(
"META-INF/MANIFEST.MF", bos.toString("UTF-8")));

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

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

try (JarFile jar = new JarFile(outputJar.toFile())) {
assertTrue(jar.getManifest().getMainAttributes().containsKey(new Attributes.Name("Created-By")));
assertFalse(jar.getManifest().getMainAttributes().containsKey(new Attributes.Name("Target-Label")));
}
}

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 762f94d

Please sign in to comment.