Write manifests at the beginning of the jar, not at the end

This is the convention, and tools that only read manifests may optimize
assuming that the manifest is at the beginning.

PiperOrigin-RevId: 190167351
diff --git a/third_party/ijar/ijar.cc b/third_party/ijar/ijar.cc
index 2ed559d..bcbe242 100644
--- a/third_party/ijar/ijar.cc
+++ b/third_party/ijar/ijar.cc
@@ -198,12 +198,13 @@
   }
   processor.SetZipBuilder(out.get());
 
+  WriteManifest(out.get(), target_label, injecting_rule_kind);
+
   // Process all files in the zip
   if (in->ProcessAll() < 0) {
     fprintf(stderr, "%s\n", in->GetError());
     abort();
   }
-  WriteManifest(out.get(), target_label, injecting_rule_kind);
 
   // Add dummy file, since javac doesn't like truly empty jars.
   if (out->GetNumberFiles() == 0) {
diff --git a/third_party/ijar/test/IjarTests.java b/third_party/ijar/test/IjarTests.java
index dc6c020..f88616f 100644
--- a/third_party/ijar/test/IjarTests.java
+++ b/third_party/ijar/test/IjarTests.java
@@ -12,12 +12,14 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import static com.google.common.collect.ImmutableList.toImmutableList;
 import static com.google.common.truth.Truth.assertThat;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.io.ByteStreams;
 import com.google.devtools.build.java.bazel.BazelJavaCompiler;
@@ -283,6 +285,9 @@
   public void testTargetLabel() throws Exception {
     try (JarFile jf =
         new JarFile("third_party/ijar/test/interface_ijar_testlib_with_target_label.jar")) {
+      ImmutableList<String> entries = jf.stream().map(JarEntry::getName).collect(toImmutableList());
+      assertThat(entries.get(0)).isEqualTo("META-INF/");
+      assertThat(entries.get(1)).isEqualTo("META-INF/MANIFEST.MF");
       Manifest manifest = jf.getManifest();
       Attributes attributes = manifest.getMainAttributes();
       assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo");