changeset 15250:acb358e088e2

Add jlink --keep-packaged-modules option
author mchung
date Mon, 15 Feb 2016 13:19:23 -0800
parents 0c64168d5972
children 425d07c79075
files src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Archive.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/DirArchive.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JarArchive.java src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties
diffstat 5 files changed, 56 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Archive.java	Mon Feb 15 17:37:42 2016 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Archive.java	Mon Feb 15 13:19:23 2016 -0800
@@ -26,6 +26,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Path;
 import java.util.Objects;
 import java.util.stream.Stream;
 
@@ -104,6 +105,11 @@
     String moduleName();
 
     /**
+     * Returns the path to this module's content
+     */
+    Path getPath();
+
+    /**
      * Stream of Entry.
      * The stream of entries needs to be closed after use
      * since it might cover lazy I/O based resources.
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/DirArchive.java	Mon Feb 15 17:37:42 2016 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/DirArchive.java	Mon Feb 15 13:19:23 2016 -0800
@@ -106,6 +106,11 @@
     }
 
     @Override
+    public Path getPath() {
+        return dirPath;
+    }
+
+    @Override
     public Stream<Entry> entries() {
         Stream<Entry> ret = null;
         try {
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JarArchive.java	Mon Feb 15 17:37:42 2016 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JarArchive.java	Mon Feb 15 13:19:23 2016 -0800
@@ -92,6 +92,11 @@
     }
 
     @Override
+    public Path getPath() {
+        return file;
+    }
+
+    @Override
     public Stream<Entry> entries() {
         try {
             if (zipFile == null) {
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Mon Feb 15 17:37:42 2016 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java	Mon Feb 15 13:19:23 2016 -0800
@@ -52,7 +52,6 @@
 
 import jdk.internal.module.ConfigurableModuleFinder;
 import jdk.internal.module.ConfigurableModuleFinder.Phase;
-import jdk.tools.jlink.Jlink;
 import jdk.tools.jlink.internal.TaskHelper.BadArgs;
 import jdk.tools.jlink.internal.TaskHelper.HiddenOption;
 import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
@@ -137,6 +136,13 @@
         new Option<JlinkTask>(false, (task, opt, arg) -> {
             task.options.version = true;
         }, "--version"),
+        new HiddenOption<JlinkTask>(true, (task, opt, arg) -> {
+            Path path = Paths.get(arg);
+            if (Files.exists(path)) {
+                throw taskHelper.newBadArgs("err.dir.exists", path);
+            }
+            task.options.packagedModulesPath = path;
+        }, "--keep-packaged-modules"),
         new HiddenOption<JlinkTask>(false, (task, opt, arg) -> {
             task.options.genbom = true;
         }, "--genbom"),
@@ -174,6 +180,7 @@
         Set<String> limitMods = new HashSet<>();
         Set<String> addMods = new HashSet<>();
         Path output;
+        Path packagedModulesPath;
         ByteOrder endian = ByteOrder.nativeOrder();
     }
 
@@ -288,8 +295,10 @@
         // First create the image provider
         ImageProvider imageProvider
                 = createImageProvider(finder,
-                        checkAddMods(config.getModules()),
-                        config.getLimitmods(), config.getByteOrder());
+                                      checkAddMods(config.getModules()),
+                                      config.getLimitmods(),
+                                      config.getByteOrder(),
+                                      null);
 
         // Then create the Plugin Stack
         ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(plugins,
@@ -339,7 +348,8 @@
                 = createImageProvider(finder,
                         options.addMods,
                         options.limitMods,
-                        options.endian);
+                        options.endian,
+                        options.packagedModulesPath);
 
         // Then create the Plugin Stack
         ImagePluginStack stack = ImagePluginConfiguration.
@@ -375,10 +385,12 @@
     }
 
     private static ImageProvider createImageProvider(ModuleFinder finder,
-            Set<String> addMods,
-            Set<String> limitMods,
-            ByteOrder order)
-            throws IOException {
+                                                     Set<String> addMods,
+                                                     Set<String> limitMods,
+                                                     ByteOrder order,
+                                                     Path retainModulesPath)
+            throws IOException
+    {
         if (addMods.isEmpty()) {
             throw new IllegalArgumentException("empty modules and limitmods");
         }
@@ -387,7 +399,7 @@
                 ModuleFinder.empty(),
                 addMods);
         Map<String, Path> mods = modulesToPath(finder, cf.descriptors());
-        return new ImageHelper(cf, mods, order);
+        return new ImageHelper(cf, mods, order, retainModulesPath);
     }
 
     /**
@@ -472,15 +484,17 @@
 
         final Set<Archive> archives;
         final ByteOrder order;
+        final Path packagedModulesPath;
 
         ImageHelper(Configuration cf,
-                Map<String, Path> modsPaths,
-                ByteOrder order)
-                throws IOException {
+                    Map<String, Path> modsPaths,
+                    ByteOrder order,
+                    Path packagedModulesPath) throws IOException {
             archives = modsPaths.entrySet().stream()
-                    .map(e -> newArchive(e.getKey(), e.getValue()))
-                    .collect(Collectors.toSet());
+                                .map(e -> newArchive(e.getKey(), e.getValue()))
+                                .collect(Collectors.toSet());
             this.order = order;
+            this.packagedModulesPath = packagedModulesPath;
         }
 
         private Archive newArchive(String module, Path path) {
@@ -501,7 +515,17 @@
 
         @Override
         public ExecutableImage retrieve(ImagePluginStack stack) throws IOException {
-            return ImageFileCreator.create(archives, order, stack);
+            ExecutableImage image = ImageFileCreator.create(archives, order, stack);
+            if (packagedModulesPath != null) {
+                // copy the packaged modules to the given path
+                Files.createDirectories(packagedModulesPath);
+                for (Archive a : archives) {
+                    Path file = a.getPath();
+                    Path dest = packagedModulesPath.resolve(file.getFileName());
+                    Files.copy(file, dest);
+                }
+            }
+            return image;
         }
     }
 
--- a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties	Mon Feb 15 17:37:42 2016 +0000
+++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties	Mon Feb 15 13:19:23 2016 -0800
@@ -56,6 +56,7 @@
 err.existing.image.invalid=existing image is not valid
 err.file.not.found=cannot find file: {0}
 err.file.error=cannot access file: {0}
+err.dir.exists={0} already exists
 err.badpattern=bad pattern {0}
 err.unknown.option=unknown option: {0}
 err.missing.arg=no value given for {0}