Put version.properties file into maven zip

Bug: b/182867353
Change-Id: Ia528779dd726c88dbabadfcdbce68bfb07cb4b90
diff --git a/tools/archive.py b/tools/archive.py
index a3f906f..893fc19 100755
--- a/tools/archive.py
+++ b/tools/archive.py
@@ -127,55 +127,7 @@
   if not utils.IsWindows():
     PrintResourceInfo()
 
-  # Create maven release which uses a build that exclude dependencies.
-  create_maven_release.generate_r8_maven_zip(utils.MAVEN_ZIP)
-  create_maven_release.generate_r8_maven_zip(
-      utils.MAVEN_ZIP_LIB, is_r8lib=True)
-
-  # Generate and copy a full build without dependencies.
-  gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
-  shutil.copyfile(utils.R8_JAR, utils.R8_FULL_EXCLUDE_DEPS_JAR)
-
-  # Ensure all archived artifacts has been built before archiving.
-  # The target tasks postfixed by 'lib' depend on the actual target task so
-  # building it invokes the original task first.
-  # The '-Pno_internal' flag is important because we generate the lib based on uses in tests.
-  gradle.RunGradle([
-    utils.R8,
-    utils.D8,
-    utils.R8LIB,
-    utils.R8LIB_NO_DEPS,
-    utils.R8RETRACE,
-    utils.R8RETRACE_NO_DEPS,
-    utils.LIBRARY_DESUGAR_CONVERSIONS,
-    '-Pno_internal'
-  ])
-
-  # Create maven release of the desuage_jdk_libs configuration. This require
-  # an r8.jar with dependencies to have been built.
-  create_maven_release.generate_desugar_configuration_maven_zip(
-      utils.DESUGAR_CONFIGURATION_MAVEN_ZIP,
-      utils.DESUGAR_CONFIGURATION,
-      utils.DESUGAR_IMPLEMENTATION)
-  create_maven_release.generate_desugar_configuration_maven_zip(
-      utils.DESUGAR_CONFIGURATION_LEGACY_JDK11_MAVEN_ZIP,
-      utils.DESUGAR_CONFIGURATION_JDK11_LEGACY,
-      utils.DESUGAR_IMPLEMENTATION_JDK11)
-
-  version = GetVersion()
-  is_main = IsMain(version)
-  if is_main:
-    # On main we use the git hash to archive with
-    print('On main, using git hash for archiving')
-    version = GetGitHash()
-
-  destination = GetVersionDestination('gs://', version, is_main)
-  if utils.cloud_storage_exists(destination) and not options.dry_run:
-    raise Exception('Target archive directory %s already exists' % destination)
   with utils.TempDir() as temp:
-    # Create pom file for our maven repository that we build for testing.
-    default_pom_file = os.path.join(temp, 'r8.pom')
-    create_maven_release.write_default_r8_pom_file(default_pom_file, version)
 
     version_file = os.path.join(temp, 'r8-version.properties')
     with open(version_file,'w') as version_writer:
@@ -190,6 +142,56 @@
       version_writer.write(releaser)
       version_writer.write('version-file.version.code=1\n')
 
+    # Create maven release which uses a build that exclude dependencies.
+    create_maven_release.generate_r8_maven_zip(utils.MAVEN_ZIP, version_file=version_file)
+    create_maven_release.generate_r8_maven_zip(
+        utils.MAVEN_ZIP_LIB, is_r8lib=True, version_file=version_file)
+
+    # Generate and copy a full build without dependencies.
+    gradle.RunGradleExcludeDeps([utils.R8, utils.R8_SRC])
+    shutil.copyfile(utils.R8_JAR, utils.R8_FULL_EXCLUDE_DEPS_JAR)
+
+    # Ensure all archived artifacts has been built before archiving.
+    # The target tasks postfixed by 'lib' depend on the actual target task so
+    # building it invokes the original task first.
+    # The '-Pno_internal' flag is important because we generate the lib based on uses in tests.
+    gradle.RunGradle([
+        utils.R8,
+        utils.D8,
+        utils.R8LIB,
+        utils.R8LIB_NO_DEPS,
+        utils.R8RETRACE,
+        utils.R8RETRACE_NO_DEPS,
+        utils.LIBRARY_DESUGAR_CONVERSIONS,
+        '-Pno_internal'
+    ])
+
+    # Create maven release of the desuage_jdk_libs configuration. This require
+    # an r8.jar with dependencies to have been built.
+    create_maven_release.generate_desugar_configuration_maven_zip(
+        utils.DESUGAR_CONFIGURATION_MAVEN_ZIP,
+        utils.DESUGAR_CONFIGURATION,
+        utils.DESUGAR_IMPLEMENTATION)
+    create_maven_release.generate_desugar_configuration_maven_zip(
+        utils.DESUGAR_CONFIGURATION_LEGACY_JDK11_MAVEN_ZIP,
+        utils.DESUGAR_CONFIGURATION_JDK11_LEGACY,
+        utils.DESUGAR_IMPLEMENTATION_JDK11)
+
+    version = GetVersion()
+    is_main = IsMain(version)
+    if is_main:
+      # On main we use the git hash to archive with
+      print('On main, using git hash for archiving')
+      version = GetGitHash()
+
+    destination = GetVersionDestination('gs://', version, is_main)
+    if utils.cloud_storage_exists(destination) and not options.dry_run:
+      raise Exception('Target archive directory %s already exists' % destination)
+
+    # Create pom file for our maven repository that we build for testing.
+    default_pom_file = os.path.join(temp, 'r8.pom')
+    create_maven_release.write_default_r8_pom_file(default_pom_file, version)
+
     for file in [
       utils.D8_JAR,
       utils.R8_JAR,
diff --git a/tools/create_maven_release.py b/tools/create_maven_release.py
index 10bbea4..6250c75 100755
--- a/tools/create_maven_release.py
+++ b/tools/create_maven_release.py
@@ -9,7 +9,7 @@
 import jdk
 import json
 from os import makedirs
-from os.path import join
+from os.path import join, basename
 from shutil import copyfile, make_archive, move, rmtree
 import subprocess
 import sys
@@ -309,7 +309,7 @@
     base_no_zip = out[0:len(out)-4]
     make_archive(base_no_zip, 'zip', tmp_dir)
 
-def generate_r8_maven_zip(out, is_r8lib=False):
+def generate_r8_maven_zip(out, is_r8lib=False, version_file=None):
   # Build the R8 no deps artifact.
   if not is_r8lib:
     gradle.RunGradleExcludeDeps([utils.R8])
@@ -318,6 +318,13 @@
 
   version = determine_version()
   with utils.TempDir() as tmp_dir:
+    file_copy = join(tmp_dir, 'copy_of_jar.jar')
+    copyfile(utils.R8LIB_JAR if is_r8lib else utils.R8_JAR, file_copy)
+
+    if version_file:
+      with zipfile.ZipFile(file_copy, 'a') as zip:
+        zip.write(version_file, basename(version_file))
+
     # Generate the pom file.
     pom_file = join(tmp_dir, 'r8.pom')
     write_pom_file(
@@ -331,7 +338,7 @@
         'r8',
         version,
         pom_file,
-        utils.R8LIB_JAR if is_r8lib else utils.R8_JAR,
+        file_copy,
         out)
 
 # Write the desugaring configuration of a jar file with the following content: