148 StandardBundlerParam.MAIN_JAR.fetchFrom(params);
149 if (fileset != null) {
150 result = StandardBundlerParam.MAIN_CLASS.fetchFrom(params);
151 } else {
152 // possibly app-image
153 }
154 }
155
156 return result;
157 }
158
159 static String getMainModule(Map<String, ? super Object> params) {
160 String result = "";
161 String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
162
163 if (mainModule != null) {
164 int index = mainModule.indexOf("/");
165
166 if (index > 0) {
167 result = mainModule.substring(0, index);
168 }
169 else {
170 result = mainModule;
171 }
172 }
173
174 return result;
175 }
176
177 static String getJDKVersion(Map<String, ? super Object> params) {
178 String result = "";
179 List<Path> modulePath =
180 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
181 Set<String> limitModules =
182 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
183 Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
184 Set<String> addModules = getValidModules(modulePath,
185 StandardBundlerParam.ADD_MODULES.fetchFrom(params),
186 limitModules, true);
187
188
189 if (javaBasePath != null && javaBasePath.toFile().exists()) {
246 Set<String> addModules =
247 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
248 Set<String> limitModules =
249 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
250 boolean stripNativeCommands =
251 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
252 Path outputDir = imageBuilder.getRoot();
253 String excludeFileList = imageBuilder.getExcludeFileList();
254 File mainJar = getMainJar(params);
255 ModFile.ModType mainJarType = ModFile.ModType.Unknown;
256
257 if (mainJar != null) {
258 mainJarType = new ModFile(mainJar).getModType();
259 } else if (StandardBundlerParam.MODULE.fetchFrom(params) == null) {
260 // user specified only main class, all jars will be on the classpath
261 mainJarType = ModFile.ModType.UnnamedJar;
262 }
263
264 // Modules
265
266 // The default for an unnamed jar is ALL_DEFAULT with the
267 // non-valid modules removed.
268 if (mainJarType == ModFile.ModType.UnnamedJar) {
269 addModules.add(ModuleHelper.ALL_RUNTIME);
270 } else if (mainJarType == ModFile.ModType.Unknown ||
271 mainJarType == ModFile.ModType.ModularJar) {
272 String mainModule = getMainModule(params);
273 addModules.add(mainModule);
274
275 // Error if any of the srcfiles are modular jars.
276 Set<String> modularJars =
277 getResourceFileJarList(params, ModFile.JarType.ModularJar);
278
279 if (!modularJars.isEmpty()) {
280 throw new Exception(MessageFormat.format(I18N.getString(
281 "error.srcfiles.contain.modules"),
282 modularJars.toString()));
283 }
284 }
285 addModules.addAll(getValidModules(
286 modulePath, addModules, limitModules, false));
287
288 Log.verbose(MessageFormat.format(
289 I18N.getString("message.modules"), addModules.toString()));
290
291 AppRuntimeImageBuilder appRuntimeBuilder = new AppRuntimeImageBuilder();
292 appRuntimeBuilder.setOutputDir(outputDir);
293 appRuntimeBuilder.setModulePath(modulePath);
294 appRuntimeBuilder.setAddModules(addModules);
295 appRuntimeBuilder.setLimitModules(limitModules);
296 appRuntimeBuilder.setExcludeFileList(excludeFileList);
297 appRuntimeBuilder.setStripNativeCommands(stripNativeCommands);
298 appRuntimeBuilder.setUserArguments(new HashMap<String,String>());
299
300 appRuntimeBuilder.build();
301 imageBuilder.prepareApplicationFiles();
302 }
303
304 static void generateJre(Map<String, ? super Object> params,
305 AbstractAppImageBuilder imageBuilder)
306 throws IOException, Exception {
307 List<Path> modulePath =
308 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
309 Set<String> addModules =
310 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
311 Set<String> limitModules =
312 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
313 boolean stripNativeCommands =
314 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
315 Path outputDir = imageBuilder.getRoot();
316 addModules.add(ModuleHelper.ALL_RUNTIME);
317 Set<String> redistModules = getValidModules(modulePath,
318 addModules, limitModules, true);
319 addModules.addAll(redistModules);
320
321 Log.verbose(MessageFormat.format(
322 I18N.getString("message.modules"), addModules.toString()));
323
324 AppRuntimeImageBuilder appRuntimeBuilder = new AppRuntimeImageBuilder();
325 appRuntimeBuilder.setOutputDir(outputDir);
326 appRuntimeBuilder.setModulePath(modulePath);
327 appRuntimeBuilder.setAddModules(addModules);
328 appRuntimeBuilder.setLimitModules(limitModules);
329 appRuntimeBuilder.setStripNativeCommands(stripNativeCommands);
330 appRuntimeBuilder.setExcludeFileList("");
331 appRuntimeBuilder.setUserArguments(new HashMap<String,String>());
332
333 appRuntimeBuilder.build();
334 imageBuilder.prepareJreFiles();
335 }
336
337 // Returns the path to the JDK modules in the user defined module path.
338 static Path findPathOfModule(
339 List<Path> modulePath, String moduleName) {
340 Path result = null;
341
342 for (Path path : modulePath) {
343 Path moduleNamePath = path.resolve(moduleName);
344
345 if (Files.exists(moduleNamePath)) {
346 result = path;
347 break;
348 }
349 }
350
351 return result;
352 }
353
354 private static Set<String> getResourceFileJarList(
355 Map<String, ? super Object> params, ModFile.JarType Query) {
356 Set<String> files = new LinkedHashSet<String>();
357
358 String srcdir = StandardBundlerParam.SOURCE_DIR.fetchFrom(params);
359
360 for (RelativeFileSet appResources :
361 StandardBundlerParam.APP_RESOURCES_LIST.fetchFrom(params)) {
362 for (String resource : appResources.getIncludedFiles()) {
363 if (resource.endsWith(".jar")) {
364 String filename = srcdir + File.separator + resource;
365
366 switch (Query) {
367 case All: {
368 files.add(filename);
369 break;
370 }
371 case ModularJar: {
372 ModFile mod = new ModFile(new File(filename));
373 if (mod.getModType() == ModFile.ModType.ModularJar) {
374 files.add(filename);
375 }
376 break;
377 }
378 case UnnamedJar: {
379 ModFile mod = new ModFile(new File(filename));
380 if (mod.getModType() == ModFile.ModType.UnnamedJar) {
381 files.add(filename);
382 }
383 break;
384 }
385 }
386 }
387 }
388 }
389
390 return files;
391 }
392
393 private static Set<String> removeInvalidModules(
394 List<Path> modulePath, Set<String> modules) {
395 Set<String> result = new LinkedHashSet<String>();
396 ModuleManager mm = new ModuleManager(modulePath);
397 List<ModFile> lmodfiles =
398 mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
399 ModuleManager.SearchType.Jmod,
400 ModuleManager.SearchType.ExplodedModule));
401
402 HashMap<String, ModFile> validModules = new HashMap<>();
403
404 for (ModFile modFile : lmodfiles) {
405 validModules.put(modFile.getModName(), modFile);
406 }
407
408 for (String name : modules) {
409 if (validModules.containsKey(name)) {
410 result.add(name);
411 } else {
412 Log.error(MessageFormat.format(
428 Optional<ModuleReference> mref = finder.find(modFile.getModName());
429
430 if (mref.isPresent()) {
431 ModuleDescriptor descriptor = mref.get().descriptor();
432
433 if (descriptor != null) {
434 Optional<ModuleDescriptor.Version> version =
435 descriptor.version();
436
437 if (version.isPresent()) {
438 result = version.get().toString();
439 }
440 }
441 }
442
443 return result;
444 }
445
446 private static class ModuleHelper {
447 // The token for "all modules on the module path".
448 private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
449
450 // The token for "all valid runtime modules".
451 static final String ALL_RUNTIME = "ALL-RUNTIME";
452
453 private final Set<String> modules = new HashSet<>();
454 private enum Macros {None, AllModulePath, AllRuntime}
455
456 ModuleHelper(List<Path> paths, Set<String> roots,
457 Set<String> limitMods, boolean forJRE) {
458 Macros macro = Macros.None;
459
460 for (Iterator<String> iterator = roots.iterator();
461 iterator.hasNext();) {
462 String module = iterator.next();
463
464 switch (module) {
465 case ALL_MODULE_PATH:
466 iterator.remove();
467 macro = Macros.AllModulePath;
468 break;
469 case ALL_RUNTIME:
470 iterator.remove();
471 macro = Macros.AllRuntime;
472 break;
473 default:
474 this.modules.add(module);
475 }
476 }
477
478 switch (macro) {
479 case AllModulePath:
480 this.modules.addAll(getModuleNamesFromPath(paths));
481 break;
482 case AllRuntime:
483 Set<Module> runtimeModules =
484 ModuleLayer.boot().modules();
485 for (Module m : runtimeModules) {
486 String name = m.getName();
487 if (forJRE && isModuleExcludedFromJRE(name)) {
488 continue; // JRE does not include this module
489 }
490 this.modules.add(name);
491 }
492 break;
493 }
494 }
495
496 Set<String> modules() {
497 return modules;
498 }
499
500 private boolean isModuleExcludedFromJRE(String name) {
501 return false; // not excluding any modules from JRE at this time
502 }
503
504 private static Set<String> getModuleNamesFromPath(List<Path> Value) {
505 Set<String> result = new LinkedHashSet<String>();
506 ModuleManager mm = new ModuleManager(Value);
507 List<ModFile> modFiles =
508 mm.getModules(
509 EnumSet.of(ModuleManager.SearchType.ModularJar,
510 ModuleManager.SearchType.Jmod,
511 ModuleManager.SearchType.ExplodedModule));
512
|
148 StandardBundlerParam.MAIN_JAR.fetchFrom(params);
149 if (fileset != null) {
150 result = StandardBundlerParam.MAIN_CLASS.fetchFrom(params);
151 } else {
152 // possibly app-image
153 }
154 }
155
156 return result;
157 }
158
159 static String getMainModule(Map<String, ? super Object> params) {
160 String result = "";
161 String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
162
163 if (mainModule != null) {
164 int index = mainModule.indexOf("/");
165
166 if (index > 0) {
167 result = mainModule.substring(0, index);
168 } else {
169 result = mainModule;
170 }
171 }
172
173 return result;
174 }
175
176 static String getJDKVersion(Map<String, ? super Object> params) {
177 String result = "";
178 List<Path> modulePath =
179 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
180 Set<String> limitModules =
181 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
182 Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
183 Set<String> addModules = getValidModules(modulePath,
184 StandardBundlerParam.ADD_MODULES.fetchFrom(params),
185 limitModules, true);
186
187
188 if (javaBasePath != null && javaBasePath.toFile().exists()) {
245 Set<String> addModules =
246 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
247 Set<String> limitModules =
248 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
249 boolean stripNativeCommands =
250 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
251 Path outputDir = imageBuilder.getRoot();
252 String excludeFileList = imageBuilder.getExcludeFileList();
253 File mainJar = getMainJar(params);
254 ModFile.ModType mainJarType = ModFile.ModType.Unknown;
255
256 if (mainJar != null) {
257 mainJarType = new ModFile(mainJar).getModType();
258 } else if (StandardBundlerParam.MODULE.fetchFrom(params) == null) {
259 // user specified only main class, all jars will be on the classpath
260 mainJarType = ModFile.ModType.UnnamedJar;
261 }
262
263 // Modules
264
265 // The default for an unnamed jar is ALL_MODULES with invalid removed
266 if (mainJarType == ModFile.ModType.UnnamedJar) {
267 addModules.add(ModuleHelper.ALL_MODULES);
268 } else if (mainJarType == ModFile.ModType.Unknown ||
269 mainJarType == ModFile.ModType.ModularJar) {
270 String mainModule = getMainModule(params);
271 addModules.add(mainModule);
272 }
273 addModules.addAll(getValidModules(
274 modulePath, addModules, limitModules, false));
275
276 Log.verbose(MessageFormat.format(
277 I18N.getString("message.modules"), addModules.toString()));
278
279 AppRuntimeImageBuilder appRuntimeBuilder = new AppRuntimeImageBuilder();
280 appRuntimeBuilder.setOutputDir(outputDir);
281 appRuntimeBuilder.setModulePath(modulePath);
282 appRuntimeBuilder.setAddModules(addModules);
283 appRuntimeBuilder.setLimitModules(limitModules);
284 appRuntimeBuilder.setExcludeFileList(excludeFileList);
285 appRuntimeBuilder.setStripNativeCommands(stripNativeCommands);
286 appRuntimeBuilder.setUserArguments(new HashMap<String,String>());
287
288 appRuntimeBuilder.build();
289 imageBuilder.prepareApplicationFiles();
290 }
291
292 static void generateJre(Map<String, ? super Object> params,
293 AbstractAppImageBuilder imageBuilder)
294 throws IOException, Exception {
295 List<Path> modulePath =
296 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
297 Set<String> addModules =
298 StandardBundlerParam.ADD_MODULES.fetchFrom(params);
299 Set<String> limitModules =
300 StandardBundlerParam.LIMIT_MODULES.fetchFrom(params);
301 boolean stripNativeCommands =
302 StandardBundlerParam.STRIP_NATIVE_COMMANDS.fetchFrom(params);
303 Path outputDir = imageBuilder.getRoot();
304 addModules.add(ModuleHelper.ALL_MODULES);
305 Set<String> redistModules = getValidModules(modulePath,
306 addModules, limitModules, true);
307 addModules.addAll(redistModules);
308
309 Log.verbose(MessageFormat.format(
310 I18N.getString("message.modules"), addModules.toString()));
311
312 AppRuntimeImageBuilder appRuntimeBuilder = new AppRuntimeImageBuilder();
313 appRuntimeBuilder.setOutputDir(outputDir);
314 appRuntimeBuilder.setModulePath(modulePath);
315 appRuntimeBuilder.setAddModules(addModules);
316 appRuntimeBuilder.setLimitModules(limitModules);
317 appRuntimeBuilder.setStripNativeCommands(stripNativeCommands);
318 appRuntimeBuilder.setExcludeFileList("");
319 appRuntimeBuilder.setUserArguments(new HashMap<String,String>());
320
321 appRuntimeBuilder.build();
322 imageBuilder.prepareJreFiles();
323 }
324
325 // Returns the path to the JDK modules in the user defined module path.
326 static Path findPathOfModule(
327 List<Path> modulePath, String moduleName) {
328 Path result = null;
329
330 for (Path path : modulePath) {
331 Path moduleNamePath = path.resolve(moduleName);
332
333 if (Files.exists(moduleNamePath)) {
334 result = path;
335 break;
336 }
337 }
338
339 return result;
340 }
341
342 private static Set<String> removeInvalidModules(
343 List<Path> modulePath, Set<String> modules) {
344 Set<String> result = new LinkedHashSet<String>();
345 ModuleManager mm = new ModuleManager(modulePath);
346 List<ModFile> lmodfiles =
347 mm.getModules(EnumSet.of(ModuleManager.SearchType.ModularJar,
348 ModuleManager.SearchType.Jmod,
349 ModuleManager.SearchType.ExplodedModule));
350
351 HashMap<String, ModFile> validModules = new HashMap<>();
352
353 for (ModFile modFile : lmodfiles) {
354 validModules.put(modFile.getModName(), modFile);
355 }
356
357 for (String name : modules) {
358 if (validModules.containsKey(name)) {
359 result.add(name);
360 } else {
361 Log.error(MessageFormat.format(
377 Optional<ModuleReference> mref = finder.find(modFile.getModName());
378
379 if (mref.isPresent()) {
380 ModuleDescriptor descriptor = mref.get().descriptor();
381
382 if (descriptor != null) {
383 Optional<ModuleDescriptor.Version> version =
384 descriptor.version();
385
386 if (version.isPresent()) {
387 result = version.get().toString();
388 }
389 }
390 }
391
392 return result;
393 }
394
395 private static class ModuleHelper {
396 // The token for "all modules on the module path".
397 private static final String ALL_MODULES = "ALL-MODULE-PATH";
398
399 private final Set<String> modules = new HashSet<>();
400 private enum Macros {None, All}
401
402 ModuleHelper(List<Path> paths, Set<String> roots,
403 Set<String> limitMods, boolean forJRE) {
404 boolean addAll = false;
405
406 for (String module : roots) {
407 if (module.equals(ALL_MODULES)) {
408 roots.remove(ALL_MODULES);
409 addAll = true;
410 } else {
411 this.modules.add(module);
412 }
413 }
414
415 if (addAll) {
416 Set<String> allModNames = getModuleNamesFromPath(paths);
417 for (String name : allModNames) {
418 if (forJRE && isModuleExcludedFromJRE(name)) {
419 Log.info("--- skipping: " + name);
420 continue; // JRE does not include this module
421 }
422 this.modules.add(name);
423 }
424 }
425 }
426
427 Set<String> modules() {
428 return modules;
429 }
430
431 private boolean isModuleExcludedFromJRE(String name) {
432 return false; // not excluding any modules from JRE at this time
433 }
434
435 private static Set<String> getModuleNamesFromPath(List<Path> Value) {
436 Set<String> result = new LinkedHashSet<String>();
437 ModuleManager mm = new ModuleManager(Value);
438 List<ModFile> modFiles =
439 mm.getModules(
440 EnumSet.of(ModuleManager.SearchType.ModularJar,
441 ModuleManager.SearchType.Jmod,
442 ModuleManager.SearchType.ExplodedModule));
443
|