src/jdk.packager/share/classes/jdk/packager/internal/JLinkBundlerHelper.java
branchJDK-8200758-branch
changeset 56869 41e17fe9fbeb
parent 56821 565d54ca1f41
child 56882 0ec8559f599a
equal deleted inserted replaced
56867:6ebaadd9a627 56869:41e17fe9fbeb
    50 import jdk.tools.jlink.internal.packager.AppRuntimeImageBuilder;
    50 import jdk.tools.jlink.internal.packager.AppRuntimeImageBuilder;
    51 
    51 
    52 
    52 
    53 public final class JLinkBundlerHelper {
    53 public final class JLinkBundlerHelper {
    54 
    54 
    55     private static final ResourceBundle I18N =
    55     private static final ResourceBundle I18N = ResourceBundle.getBundle(
    56             ResourceBundle.getBundle("jdk.packager.internal.resources.JLinkBundlerHelper");
    56            "jdk.packager.internal.resources.JLinkBundlerHelper");
    57     
       
    58     private static final String JRE_MODULES_FILENAME =
    57     private static final String JRE_MODULES_FILENAME =
    59             "jdk/packager/internal/resources/jre.list";
    58             "jdk/packager/internal/resources/jre.list";
    60     private static final String SERVER_JRE_MODULES_FILENAME =
    59     private static final String SERVER_JRE_MODULES_FILENAME =
    61             "jdk/packager/internal/resources/server.jre.list";
    60             "jdk/packager/internal/resources/server.jre.list";
    62 
    61 
   140         return result;
   139         return result;
   141     }
   140     }
   142 
   141 
   143     public static File getMainJar(Map<String, ? super Object> params) {
   142     public static File getMainJar(Map<String, ? super Object> params) {
   144         File result = null;
   143         File result = null;
   145         RelativeFileSet fileset = StandardBundlerParam.MAIN_JAR.fetchFrom(params);
   144         RelativeFileSet fileset =
       
   145                 StandardBundlerParam.MAIN_JAR.fetchFrom(params);
   146 
   146 
   147         if (fileset != null) {
   147         if (fileset != null) {
   148             String filename = fileset.getIncludedFiles().iterator().next();
   148             String filename = fileset.getIncludedFiles().iterator().next();
   149             result = fileset.getBaseDirectory().toPath().resolve(filename).toFile();
   149             result = fileset.getBaseDirectory().toPath().
       
   150                     resolve(filename).toFile();
   150 
   151 
   151             if (result == null || !result.exists()) {
   152             if (result == null || !result.exists()) {
   152                 String srcdir = StandardBundlerParam.SOURCE_DIR.fetchFrom(params);
   153                 String srcdir =
       
   154                     StandardBundlerParam.SOURCE_DIR.fetchFrom(params);
   153 
   155 
   154                 if (srcdir != null) {
   156                 if (srcdir != null) {
   155                     result = new File(srcdir + File.separator + filename);
   157                     result = new File(srcdir + File.separator + filename);
   156                 }
   158                 }
   157             }
   159             }
   193         return result;
   195         return result;
   194     }
   196     }
   195 
   197 
   196     public static String getJDKVersion(Map<String, ? super Object> params) {
   198     public static String getJDKVersion(Map<String, ? super Object> params) {
   197         String result = "";
   199         String result = "";
   198         List<Path> modulePath = StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   200         List<Path> modulePath =
   199         Set<String> limitModules = StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
   201                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
       
   202         Set<String> limitModules =
       
   203                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
   200         Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
   204         Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
   201         Set<String> addModules = getRedistributableModules(modulePath,
   205         Set<String> addModules = getRedistributableModules(modulePath,
   202                 StandardBundlerParam.ADD_MODULES.fetchFrom(params),
   206                 StandardBundlerParam.ADD_MODULES.fetchFrom(params),
   203                 limitModules, JRE_MODULES_FILENAME);
   207                 limitModules, JRE_MODULES_FILENAME);
   204 
   208 
   205         if (javaBasePath != null && javaBasePath.toFile().exists()) {
   209         if (javaBasePath != null && javaBasePath.toFile().exists()) {
   206             result = RedistributableModules.getModuleVersion(javaBasePath.toFile(),
   210             result = RedistributableModules.getModuleVersion(
   207                         modulePath, addModules, limitModules);
   211                    javaBasePath.toFile(), modulePath, addModules, limitModules);
   208         }
   212         }
   209 
   213 
   210         return result;
   214         return result;
   211     }
   215     }
   212 
   216 
   213     public static Path getJDKHome(Map<String, ? super Object> params) {
   217     public static Path getJDKHome(Map<String, ? super Object> params) {
   214         Path result = null;
   218         Path result = null;
   215         List<Path> modulePath = StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   219         List<Path> modulePath =
       
   220                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   216         Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
   221         Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
   217 
   222 
   218         if (javaBasePath != null && javaBasePath.toFile().exists()) {
   223         if (javaBasePath != null && javaBasePath.toFile().exists()) {
   219             result = javaBasePath.getParent();
   224             result = javaBasePath.getParent();
   220 
   225 
   224             if (result != null) {
   229             if (result != null) {
   225                 boolean found = false;
   230                 boolean found = false;
   226                 Path bin = result.resolve("bin");
   231                 Path bin = result.resolve("bin");
   227 
   232 
   228                 if (Files.exists(bin)) {
   233                 if (Files.exists(bin)) {
   229                     final String exe = (Platform.getPlatform() == Platform.WINDOWS) ? ".exe" : "";
   234                     final String exe =
       
   235                             (Platform.getPlatform() == Platform.WINDOWS) ?
       
   236                             ".exe" : "";
   230                     Path javaExe = bin.resolve("java" + exe);
   237                     Path javaExe = bin.resolve("java" + exe);
   231 
   238 
   232                     if (Files.exists(javaExe)) {
   239                     if (Files.exists(javaExe)) {
   233                         found = true;
   240                         found = true;
   234                     }
   241                     }
   243         return result;
   250         return result;
   244     }
   251     }
   245 
   252 
   246     private static Set<String> getRedistributableModules(List<Path> modulePath,
   253     private static Set<String> getRedistributableModules(List<Path> modulePath,
   247             Set<String> addModules, Set<String> limitModules, String filename) {
   254             Set<String> addModules, Set<String> limitModules, String filename) {
   248         ModuleHelper moduleHelper = new ModuleHelper(modulePath, addModules, limitModules, filename);
   255         ModuleHelper moduleHelper = new ModuleHelper(
       
   256                 modulePath, addModules, limitModules, filename);
   249         return removeInvalidModules(modulePath, moduleHelper.modules());
   257         return removeInvalidModules(modulePath, moduleHelper.modules());
   250     }
   258     }
   251 
   259 
   252     public static void execute(Map<String, ? super Object> params,
   260     public static void execute(Map<String, ? super Object> params,
   253             AbstractAppImageBuilder imageBuilder) throws IOException, Exception {
   261             AbstractAppImageBuilder imageBuilder)
   254         List<Path> modulePath = StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   262             throws IOException, Exception {
   255         Set<String> addModules = StandardBundlerParam.ADD_MODULES.fetchFrom(params);
   263         List<Path> modulePath =
   256         Set<String> limitModules = StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
   264                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   257         boolean stripNativeCommands = StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
   265         Set<String> addModules =
       
   266                 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
       
   267         Set<String> limitModules =
       
   268                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
       
   269         boolean stripNativeCommands =
       
   270                 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
   258         Path outputDir = imageBuilder.getRoot();
   271         Path outputDir = imageBuilder.getRoot();
   259         String excludeFileList = imageBuilder.getExcludeFileList();
   272         String excludeFileList = imageBuilder.getExcludeFileList();
   260         File mainJar = getMainJar(params);
   273         File mainJar = getMainJar(params);
   261         Module.ModuleType mainJarType = Module.ModuleType.Unknown;
   274         Module.ModuleType mainJarType = Module.ModuleType.Unknown;
   262 
   275 
   263         if (mainJar != null) {
   276         if (mainJar != null) {
   264             mainJarType = new Module(mainJar).getModuleType();
   277             mainJarType = new Module(mainJar).getModuleType();
   265         } else if (mainJar == null && StandardBundlerParam.MODULE.fetchFrom(params) == null) {
   278         } else if (mainJar == null &&
       
   279                 StandardBundlerParam.MODULE.fetchFrom(params) == null) {
   266             // user specified only main class, all jars will be on the classpath
   280             // user specified only main class, all jars will be on the classpath
   267             mainJarType = Module.ModuleType.UnnamedJar;
   281             mainJarType = Module.ModuleType.UnnamedJar;
   268         }
   282         }
   269 
   283 
   270         // Modules
   284         // Modules
   271 
   285 
   272         // The default for an unnamed jar is ALL_DEFAULT with the
   286         // The default for an unnamed jar is ALL_DEFAULT with the
   273         // non-redistributable modules removed.
   287         // non-redistributable modules removed.
   274         if (mainJarType == Module.ModuleType.UnnamedJar) {
   288         if (mainJarType == Module.ModuleType.UnnamedJar) {
   275             addModules.add(ModuleHelper.ALL_RUNTIME);
   289             addModules.add(ModuleHelper.ALL_RUNTIME);
   276         } else if (mainJarType == Module.ModuleType.Unknown || mainJarType == Module.ModuleType.ModularJar) {
   290         } else if (mainJarType == Module.ModuleType.Unknown ||
       
   291                 mainJarType == Module.ModuleType.ModularJar) {
   277             String mainModule = getMainModule(params);
   292             String mainModule = getMainModule(params);
   278             addModules.add(mainModule);
   293             addModules.add(mainModule);
   279 
   294 
   280             // Error if any of the srcfiles are modular jars.
   295             // Error if any of the srcfiles are modular jars.
   281             Set<String> modularJars = getResourceFileJarList(params, Module.JarType.ModularJar);
   296             Set<String> modularJars =
       
   297                     getResourceFileJarList(params, Module.JarType.ModularJar);
   282 
   298 
   283             if (!modularJars.isEmpty()) {
   299             if (!modularJars.isEmpty()) {
   284                 throw new Exception(MessageFormat.format(I18N.getString(
   300                 throw new Exception(MessageFormat.format(I18N.getString(
   285                         "error.srcfiles.contain.modules"), modularJars.toString()));
   301                         "error.srcfiles.contain.modules"),
       
   302                         modularJars.toString()));
   286             }
   303             }
   287         }
   304         }
   288 
   305 
   289         Set<String> redistModules = getRedistributableModules(
   306         Set<String> redistModules = getRedistributableModules(
   290                 modulePath, addModules, limitModules, JRE_MODULES_FILENAME);
   307                 modulePath, addModules, limitModules, JRE_MODULES_FILENAME);
   291         addModules.addAll(redistModules);
   308         addModules.addAll(redistModules);
   292 
   309 
   293         if (imageBuilder.getPlatformSpecificModulesFile() != null) {
   310         if (imageBuilder.getPlatformSpecificModulesFile() != null) {
   294             Set<String> platformModules = RedistributableModules.getRedistributableModules(
   311             Set<String> platformModules =
       
   312                     RedistributableModules.getRedistributableModules(
   295                 modulePath, imageBuilder.getPlatformSpecificModulesFile());
   313                 modulePath, imageBuilder.getPlatformSpecificModulesFile());
   296             addModules.addAll(platformModules);    
   314             addModules.addAll(platformModules);    
   297         }
   315         }
   298 
   316 
   299         Log.info(MessageFormat.format(I18N.getString("message.modules"), addModules.toString()));
   317         Log.info(MessageFormat.format(
       
   318                 I18N.getString("message.modules"), addModules.toString()));
   300 
   319 
   301         if (StandardBundlerParam.ECHO_MODE.fetchFrom(params)) {
   320         if (StandardBundlerParam.ECHO_MODE.fetchFrom(params)) {
   302             Log.info("\nECHO-MODE: Running jlink [ ");
   321             Log.info("\nECHO-MODE: Running jlink [ ");
   303             Log.info("--output = " + outputDir.toString());
   322             Log.info("--output = " + outputDir.toString());
   304             Log.info("--module-path = " + modulePath.toString());
   323             Log.info("--module-path = " + modulePath.toString());
   321         appRuntimeBuilder.build();
   340         appRuntimeBuilder.build();
   322         imageBuilder.prepareApplicationFiles();
   341         imageBuilder.prepareApplicationFiles();
   323     }
   342     }
   324 
   343 
   325     public static void generateServerJre(Map<String, ? super Object> params,
   344     public static void generateServerJre(Map<String, ? super Object> params,
   326             AbstractAppImageBuilder imageBuilder) throws IOException, Exception {
   345             AbstractAppImageBuilder imageBuilder)
   327         List<Path> modulePath = StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   346             throws IOException, Exception {
   328         Set<String> addModules = StandardBundlerParam.ADD_MODULES.fetchFrom(params);
   347         List<Path> modulePath =
   329         Set<String> limitModules = StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
   348                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
   330         boolean stripNativeCommands = StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
   349         Set<String> addModules =
       
   350                 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
       
   351         Set<String> limitModules =
       
   352                 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
       
   353         boolean stripNativeCommands =
       
   354                 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
   331         Path outputDir = imageBuilder.getRoot();
   355         Path outputDir = imageBuilder.getRoot();
   332         addModules.add(ModuleHelper.ALL_RUNTIME);
   356         addModules.add(ModuleHelper.ALL_RUNTIME);
   333         Set<String> redistModules = getRedistributableModules(
   357         Set<String> redistModules = getRedistributableModules(modulePath,
   334                 modulePath, addModules, limitModules, SERVER_JRE_MODULES_FILENAME);
   358                 addModules, limitModules, SERVER_JRE_MODULES_FILENAME);
   335         addModules.addAll(redistModules);
   359         addModules.addAll(redistModules);
   336 
   360 
   337         if (imageBuilder.getPlatformSpecificModulesFile() != null) {
   361         if (imageBuilder.getPlatformSpecificModulesFile() != null) {
   338             Set<String> platformModules = RedistributableModules.getRedistributableModules(
   362             Set<String> platformModules =
   339                 modulePath, imageBuilder.getPlatformSpecificModulesFile());
   363                     RedistributableModules.getRedistributableModules(
       
   364                     modulePath, imageBuilder.getPlatformSpecificModulesFile());
   340             addModules.addAll(platformModules);    
   365             addModules.addAll(platformModules);    
   341         }
   366         }
   342 
   367 
   343         Log.info(MessageFormat.format(I18N.getString("message.modules"), addModules.toString()));
   368         Log.info(MessageFormat.format(
       
   369                 I18N.getString("message.modules"), addModules.toString()));
   344 
   370 
   345         if (StandardBundlerParam.ECHO_MODE.fetchFrom(params)) {
   371         if (StandardBundlerParam.ECHO_MODE.fetchFrom(params)) {
   346             Log.info("\nECHO-MODE: Running jlink [ ");
   372             Log.info("\nECHO-MODE: Running jlink [ ");
   347             Log.info("--output = " + outputDir.toString());
   373             Log.info("--output = " + outputDir.toString());
   348             Log.info("--module-path = " + modulePath.toString());
   374             Log.info("--module-path = " + modulePath.toString());
   364         appRuntimeBuilder.build();
   390         appRuntimeBuilder.build();
   365         imageBuilder.prepareServerJreFiles();
   391         imageBuilder.prepareServerJreFiles();
   366     }
   392     }
   367 
   393 
   368     // Returns the path to the JDK modules in the user defined module path.
   394     // Returns the path to the JDK modules in the user defined module path.
   369     public static Path findPathOfModule(List<Path> modulePath, String moduleName) {
   395     public static Path findPathOfModule(
       
   396             List<Path> modulePath, String moduleName) {
   370         Path result = null;
   397         Path result = null;
   371 
   398 
   372         for (Path path : modulePath) {
   399         for (Path path : modulePath) {
   373             Path moduleNamePath = path.resolve(moduleName);
   400             Path moduleNamePath = path.resolve(moduleName);
   374 
   401 
   385             Map<String, ? super Object> params, Module.JarType Query) {
   412             Map<String, ? super Object> params, Module.JarType Query) {
   386         Set<String> files = new LinkedHashSet();
   413         Set<String> files = new LinkedHashSet();
   387 
   414 
   388         String srcdir = StandardBundlerParam.SOURCE_DIR.fetchFrom(params);
   415         String srcdir = StandardBundlerParam.SOURCE_DIR.fetchFrom(params);
   389 
   416 
   390         for (RelativeFileSet appResources : StandardBundlerParam.APP_RESOURCES_LIST.fetchFrom(params)) {
   417         for (RelativeFileSet appResources :
       
   418                 StandardBundlerParam.APP_RESOURCES_LIST.fetchFrom(params)) {
   391             for (String resource : appResources.getIncludedFiles()) {
   419             for (String resource : appResources.getIncludedFiles()) {
   392                 if (resource.endsWith(".jar")) {
   420                 if (resource.endsWith(".jar")) {
   393                     String filename = srcdir + File.separator + resource;
   421                     String filename = srcdir + File.separator + resource;
   394 
   422 
   395                     switch (Query) {
   423                     switch (Query) {
   398                             break;
   426                             break;
   399                         }
   427                         }
   400                         case ModularJar: {
   428                         case ModularJar: {
   401                             Module module = new Module(new File(filename));
   429                             Module module = new Module(new File(filename));
   402 
   430 
   403                             if (module.getModuleType() == Module.ModuleType.ModularJar) {
   431                             if (module.getModuleType() ==
       
   432                                     Module.ModuleType.ModularJar) {
   404                                 files.add(filename);
   433                                 files.add(filename);
   405                             }
   434                             }
   406                             break;
   435                             break;
   407                         }
   436                         }
   408                         case UnnamedJar: {
   437                         case UnnamedJar: {
   409                             Module module = new Module(new File(filename));
   438                             Module module = new Module(new File(filename));
   410 
   439 
   411                             if (module.getModuleType() == Module.ModuleType.UnnamedJar) {
   440                             if (module.getModuleType() ==
       
   441                                     Module.ModuleType.UnnamedJar) {
   412                                 files.add(filename);
   442                                 files.add(filename);
   413                             }
   443                             }
   414                             break;
   444                             break;
   415                         }
   445                         }
   416                     }
   446                     }
   419         }
   449         }
   420 
   450 
   421         return files;
   451         return files;
   422     }
   452     }
   423 
   453 
   424     private static Set<String> removeInvalidModules(List<Path> modulePath, Set<String> modules) {
   454     private static Set<String> removeInvalidModules(
       
   455             List<Path> modulePath, Set<String> modules) {
   425         Set<String> result = new LinkedHashSet();
   456         Set<String> result = new LinkedHashSet();
   426         ModuleManager mm = new ModuleManager(modulePath);
   457         ModuleManager mm = new ModuleManager(modulePath);
   427         List<Module> lmodules = mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
   458         List<Module> lmodules =
   428                                               ModuleManager.SearchType.Jmod,
   459                 mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
   429                                               ModuleManager.SearchType.ExplodedModule));
   460                         ModuleManager.SearchType.Jmod,
       
   461                         ModuleManager.SearchType.ExplodedModule));
   430 
   462 
   431         HashMap<String, Module> validModules = new HashMap<>();
   463         HashMap<String, Module> validModules = new HashMap<>();
   432 
   464 
   433         for (Module module : lmodules) {
   465         for (Module module : lmodules) {
   434             validModules.put(module.getModuleName(), module);
   466             validModules.put(module.getModuleName(), module);
   437         for (String name : modules) {
   469         for (String name : modules) {
   438             if (validModules.containsKey(name)) {
   470             if (validModules.containsKey(name)) {
   439                 result.add(name);
   471                 result.add(name);
   440             }
   472             }
   441             else {
   473             else {
   442                 Log.info(MessageFormat.format(I18N.getString("warning.module.does.not.exist"), name));
   474                 Log.info(MessageFormat.format(
       
   475                         I18N.getString("warning.module.does.not.exist"), name));
   443             }
   476             }
   444         }
   477         }
   445 
   478 
   446         return result;
   479         return result;
   447     }
   480     }
   458 
   491 
   459         public ModuleHelper(List<Path> paths, Set<String> roots,
   492         public ModuleHelper(List<Path> paths, Set<String> roots,
   460                 Set<String> limitMods, String filename) {
   493                 Set<String> limitMods, String filename) {
   461             Macros macro = Macros.None;
   494             Macros macro = Macros.None;
   462 
   495 
   463             for (Iterator<String> iterator = roots.iterator(); iterator.hasNext();) {
   496             for (Iterator<String> iterator = roots.iterator();
       
   497                     iterator.hasNext();) {
   464                 String module = iterator.next();
   498                 String module = iterator.next();
   465 
   499 
   466                 switch (module) {
   500                 switch (module) {
   467                     case ALL_MODULE_PATH:
   501                     case ALL_MODULE_PATH:
   468                         iterator.remove();
   502                         iterator.remove();
   480             switch (macro) {
   514             switch (macro) {
   481                 case AllModulePath:
   515                 case AllModulePath:
   482                     this.modules.addAll(getModuleNamesFromPath(paths));
   516                     this.modules.addAll(getModuleNamesFromPath(paths));
   483                     break;
   517                     break;
   484                 case AllRuntime:
   518                 case AllRuntime:
   485                     Set<String> m = RedistributableModules.getRedistributableModules(paths, filename);
   519                     Set<String> m =
       
   520                             RedistributableModules.getRedistributableModules(
       
   521                             paths, filename);
   486 
   522 
   487                     if (m != null) {
   523                     if (m != null) {
   488                         this.modules.addAll(m);
   524                         this.modules.addAll(m);
   489                     }
   525                     }
   490 
   526 
   497         }
   533         }
   498 
   534 
   499         private static Set<String> getModuleNamesFromPath(List<Path> Value) {
   535         private static Set<String> getModuleNamesFromPath(List<Path> Value) {
   500                 Set<String> result = new LinkedHashSet();
   536                 Set<String> result = new LinkedHashSet();
   501                 ModuleManager mm = new ModuleManager(Value);
   537                 ModuleManager mm = new ModuleManager(Value);
   502                 List<Module> modules = mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
   538                 List<Module> modules =
   503                                                      ModuleManager.SearchType.Jmod,
   539                         mm.getModules(
   504                                                      ModuleManager.SearchType.ExplodedModule));
   540                                 EnumSet.of(ModuleManager.SearchType.ModularJar,
       
   541                                 ModuleManager.SearchType.Jmod,
       
   542                                 ModuleManager.SearchType.ExplodedModule));
   505 
   543 
   506                 for (Module module : modules) {
   544                 for (Module module : modules) {
   507                     result.add(module.getModuleName());
   545                     result.add(module.getModuleName());
   508                 }
   546                 }
   509 
   547