8230651: Use version string from main module JDK-8200758-branch
authorherrick
Tue, 24 Sep 2019 14:05:49 -0400
branchJDK-8200758-branch
changeset 58305 d42b1f6960aa
parent 58304 7a61351edad2
child 58306 42c7f9800e30
8230651: Use version string from main module Submitted-by: almatvee Reviewed-by: herrick, asemenyuk
src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources.properties
src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_ja.properties
src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_zh_CN.properties
test/jdk/tools/jpackage/helpers/JPackageHelper.java
test/jdk/tools/jpackage/share/AppVersionModuleTest.java
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Tue Sep 24 13:57:28 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Tue Sep 24 14:05:49 2019 -0400
@@ -27,7 +27,10 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.io.StringReader;
+import java.lang.module.ModuleDescriptor;
+import java.lang.module.ModuleDescriptor.Version;
+import java.lang.module.ModuleFinder;
+import java.lang.module.ModuleReference;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -36,13 +39,10 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.Set;
 import java.util.HashSet;
@@ -51,7 +51,6 @@
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
-import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -258,7 +257,7 @@
             new StandardBundlerParam<>(
                     Arguments.CLIOptions.VERSION.getId(),
                     String.class,
-                    params -> DEFAULT_VERSION,
+                    params -> getDefaultAppVersion(params),
                     (s, p) -> s
             );
 
@@ -756,4 +755,30 @@
 
         return result;
     }
+
+    static String getDefaultAppVersion(Map<String, ? super Object> params) {
+        String appVersion = DEFAULT_VERSION;
+        boolean hasModule = params.containsKey(MODULE.getID());
+        if (hasModule) {
+            List<Path> modulePath = MODULE_PATH.fetchFrom(params);
+            if (!modulePath.isEmpty()) {
+                ModuleFinder finder = ModuleFinder.of(modulePath.toArray(new Path[0]));
+                String mainModule = JLinkBundlerHelper.getMainModule(params);
+                Optional<ModuleReference> omref = finder.find(mainModule);
+                if (omref.isPresent()) {
+                    ModuleDescriptor descriptor = omref.get().descriptor();
+                    Optional<Version> oversion = descriptor.version();
+                    if (oversion.isPresent()) {
+                        Log.verbose(MessageFormat.format(I18N.getString(
+                                "message.module-version"),
+                                oversion.get().toString(),
+                                mainModule));
+                        appVersion = oversion.get().toString();
+                    }
+                }
+            }
+        }
+
+        return appVersion;
+    }
 }
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources.properties	Tue Sep 24 13:57:28 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources.properties	Tue Sep 24 14:05:49 2019 -0400
@@ -39,6 +39,7 @@
 message.runtime-image-dir-does-not-exist.advice=Confirm that the value for {0} exists
 message.debug-working-directory=Kept working directory for debug: {0}
 message.bundle-created=Succeeded in building {0} package
+message.module-version=Using version "{0}" from module "{1}" as application version
 
 error.cannot-create-output-dir=Destination directory {0} cannot be created
 error.cannot-write-to-output-dir=Destination directory {0} is not writable
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_ja.properties	Tue Sep 24 13:57:28 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_ja.properties	Tue Sep 24 14:05:49 2019 -0400
@@ -39,6 +39,7 @@
 message.runtime-image-dir-does-not-exist.advice=Confirm that the value for {0} exists
 message.debug-working-directory=Kept working directory for debug: {0}
 message.bundle-created=Succeeded in building {0} package
+message.module-version=Using version "{0}" from module "{1}" as application version
 
 error.cannot-create-output-dir=Destination directory {0} cannot be created
 error.cannot-write-to-output-dir=Destination directory {0} is not writable
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_zh_CN.properties	Tue Sep 24 13:57:28 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_zh_CN.properties	Tue Sep 24 14:05:49 2019 -0400
@@ -39,6 +39,7 @@
 message.runtime-image-dir-does-not-exist.advice=Confirm that the value for {0} exists
 message.debug-working-directory=Kept working directory for debug: {0}
 message.bundle-created=Succeeded in building {0} package
+message.module-version=Using version "{0}" from module "{1}" as application version
 
 error.cannot-create-output-dir=Destination directory {0} cannot be created
 error.cannot-write-to-output-dir=Destination directory {0} is not writable
--- a/test/jdk/tools/jpackage/helpers/JPackageHelper.java	Tue Sep 24 13:57:28 2019 -0400
+++ b/test/jdk/tools/jpackage/helpers/JPackageHelper.java	Tue Sep 24 14:05:49 2019 -0400
@@ -372,15 +372,19 @@
     }
 
     public static void createHelloModule() throws Exception {
-        createModule("Hello.java", "input", "hello", true);
+        createModule("Hello.java", "input", "hello", null, true);
+    }
+
+    public static void createHelloModule(String version) throws Exception {
+        createModule("Hello.java", "input", "hello", version, true);
     }
 
     public static void createOtherModule() throws Exception {
-        createModule("Other.java", "input-other", "other", false);
+        createModule("Other.java", "input-other", "other", null, false);
     }
 
     private static void createModule(String javaFile, String inputDir,
-            String aName, boolean createModularJar) throws Exception {
+            String aName, String version, boolean createModularJar) throws Exception {
         int retVal;
 
         File input = new File(inputDir);
@@ -427,6 +431,10 @@
                 args.add("--create");
                 args.add("--file");
                 args.add(inputDir + File.separator + "com." + aName + ".jar");
+                if (version != null) {
+                    args.add("--module-version");
+                    args.add(version);
+                }
                 args.add("-C");
                 args.add("module" + File.separator + "com." + aName);
                 args.add(".");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackage/share/AppVersionModuleTest.java	Tue Sep 24 14:05:49 2019 -0400
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+
+/*
+ * @test
+ * @summary jpackage create image using version from main module
+ * @library ../helpers
+ * @build JPackageHelper
+ * @build JPackagePath
+ * @modules jdk.jpackage
+ * @run main/othervm -Xmx512m AppVersionModuleTest
+ */
+public class AppVersionModuleTest {
+    private static final String OUTPUT = "output";
+    private static final String appCfg = JPackagePath.getAppCfg();
+    private static final String MODULE_VERSION = "2.7";
+    private static final String CLI_VERSION = "3.5";
+
+    private static final String[] CMD_MODULE_VERSION = {
+        "--package-type", "app-image",
+        "--input", "input",
+        "--dest", OUTPUT,
+        "--name", "test",
+        "--module", "com.hello/com.hello.Hello",
+        "--module-path", "input",
+   };
+
+    private static final String[] CMD_CLI_VERSION = {
+        "--package-type", "app-image",
+        "--input", "input",
+        "--dest", OUTPUT,
+        "--name", "test",
+        "--module", "com.hello/com.hello.Hello",
+        "--module-path", "input",
+        "--app-version", CLI_VERSION};
+
+    private static void validate(String version)
+            throws Exception {
+        File outfile = new File(appCfg);
+        if (!outfile.exists()) {
+            throw new AssertionError(appCfg + " was not created");
+        }
+
+        String output = Files.readString(outfile.toPath());
+        if (version == null) {
+            version = MODULE_VERSION;
+        }
+
+        String expected = "app.version=" + version;
+        if (!output.contains(expected)) {
+            System.err.println("Expected: " + expected);
+            throw new AssertionError("Cannot find expected entry in config file");
+        }
+    }
+
+    private static void testVersion() throws Exception {
+        JPackageHelper.executeCLI(true, CMD_MODULE_VERSION);
+        validate(null);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        JPackageHelper.executeCLI(true, CMD_CLI_VERSION);
+        validate(CLI_VERSION);
+    }
+
+    private static void testVersionToolProvider() throws Exception {
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        JPackageHelper.executeToolProvider(true, CMD_MODULE_VERSION);
+        validate(null);
+        JPackageHelper.deleteOutputFolder(OUTPUT);
+        JPackageHelper.executeToolProvider(true, CMD_CLI_VERSION);
+        validate(CLI_VERSION);
+    }
+
+    public static void main(String[] args) throws Exception {
+        JPackageHelper.createHelloModule(MODULE_VERSION);
+        testVersion();
+        testVersionToolProvider();
+    }
+
+}