234 .showUsage(true); 235 } 236 if (options.help) { 237 optionsHelper.showHelp(PROGNAME); 238 return EXIT_OK; 239 } 240 if (optionsHelper.shouldListPlugins()) { 241 optionsHelper.listPlugins(); 242 return EXIT_OK; 243 } 244 if (options.version || options.fullVersion) { 245 taskHelper.showVersion(options.fullVersion); 246 return EXIT_OK; 247 } 248 249 if (taskHelper.getExistingImage() != null) { 250 postProcessOnly(taskHelper.getExistingImage()); 251 return EXIT_OK; 252 } 253 254 if (options.modulePath.isEmpty()) { 255 throw taskHelper.newBadArgs("err.modulepath.must.be.specified") 256 .showUsage(true); 257 } 258 259 JlinkConfiguration config = initJlinkConfig(); 260 if (options.suggestProviders) { 261 suggestProviders(config, remaining); 262 } else { 263 createImage(config); 264 if (options.saveoptsfile != null) { 265 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes()); 266 } 267 } 268 269 return EXIT_OK; 270 } catch (PluginException | IllegalArgumentException | 271 UncheckedIOException |IOException | FindException | ResolutionException e) { 272 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage()); 273 if (DEBUG) { 274 e.printStackTrace(log); 275 } 276 return EXIT_ERROR; 277 } catch (BadArgs e) { 349 if (mod.equals(ALL_MODULE_PATH)) { 350 Path[] entries = options.modulePath.toArray(new Path[0]); 351 ModuleFinder finder = ModulePath.of(Runtime.version(), true, entries); 352 if (!options.limitMods.isEmpty()) { 353 // finder for the observable modules specified in 354 // the --module-path and --limit-modules options 355 finder = limitFinder(finder, options.limitMods, Collections.emptySet()); 356 } 357 358 // all observable modules are roots 359 finder.findAll() 360 .stream() 361 .map(ModuleReference::descriptor) 362 .map(ModuleDescriptor::name) 363 .forEach(mn -> roots.add(mn)); 364 } else { 365 roots.add(mod); 366 } 367 } 368 369 return new JlinkConfiguration(options.output, 370 options.modulePath, 371 roots, 372 options.limitMods, 373 options.endian); 374 } 375 376 private void createImage(JlinkConfiguration config) throws Exception { 377 if (options.output == null) { 378 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true); 379 } 380 if (options.addMods.isEmpty()) { 381 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules") 382 .showUsage(true); 383 } 384 385 // First create the image provider 386 ImageProvider imageProvider = createImageProvider(config, 387 options.packagedModulesPath, 388 options.ignoreSigning, 389 options.bindServices, 390 options.verbose, 391 log); 392 393 // Then create the Plugin Stack 394 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration( 395 taskHelper.getPluginsConfig(options.output, options.launchers)); 396 397 //Ask the stack to proceed 398 stack.operate(imageProvider); 399 } 400 401 /* 402 * Returns a module finder of the given module path that limits 403 * the observable modules to those in the transitive closure of 404 * the modules specified in {@code limitMods} plus other modules 405 * specified in the {@code roots} set. 406 */ 407 public static ModuleFinder newModuleFinder(List<Path> paths, 408 Set<String> limitMods, 409 Set<String> roots) 410 { 411 Path[] entries = paths.toArray(new Path[0]); 412 ModuleFinder finder = ModulePath.of(Runtime.version(), true, entries); 413 414 // if limitmods is specified then limit the universe 415 if (!limitMods.isEmpty()) { 416 finder = limitFinder(finder, limitMods, roots); 417 } 418 return finder; 419 } 420 421 private static Path toPathLocation(ResolvedModule m) { 422 Optional<URI> ouri = m.reference().location(); 423 if (!ouri.isPresent()) 424 throw new InternalError(m + " does not have a location"); 425 URI uri = ouri.get(); 426 return Paths.get(uri); 427 } 428 429 430 private static ImageProvider createImageProvider(JlinkConfiguration config, 431 Path retainModulesPath, 432 boolean ignoreSigning, 433 boolean bindService, 434 boolean verbose, 435 PrintWriter log) 436 throws IOException | 234 .showUsage(true); 235 } 236 if (options.help) { 237 optionsHelper.showHelp(PROGNAME); 238 return EXIT_OK; 239 } 240 if (optionsHelper.shouldListPlugins()) { 241 optionsHelper.listPlugins(); 242 return EXIT_OK; 243 } 244 if (options.version || options.fullVersion) { 245 taskHelper.showVersion(options.fullVersion); 246 return EXIT_OK; 247 } 248 249 if (taskHelper.getExistingImage() != null) { 250 postProcessOnly(taskHelper.getExistingImage()); 251 return EXIT_OK; 252 } 253 254 255 if (options.modulePath.isEmpty()) { 256 // no --module-path specified - try to set $JAVA_HOME/jmods if that exists 257 Path jmods = getDefaultModulePath(); 258 if (jmods != null) { 259 options.modulePath.add(jmods); 260 } 261 262 if (options.modulePath.isEmpty()) { 263 throw taskHelper.newBadArgs("err.modulepath.must.be.specified") 264 .showUsage(true); 265 } 266 } 267 268 JlinkConfiguration config = initJlinkConfig(); 269 if (options.suggestProviders) { 270 suggestProviders(config, remaining); 271 } else { 272 createImage(config); 273 if (options.saveoptsfile != null) { 274 Files.write(Paths.get(options.saveoptsfile), getSaveOpts().getBytes()); 275 } 276 } 277 278 return EXIT_OK; 279 } catch (PluginException | IllegalArgumentException | 280 UncheckedIOException |IOException | FindException | ResolutionException e) { 281 log.println(taskHelper.getMessage("error.prefix") + " " + e.getMessage()); 282 if (DEBUG) { 283 e.printStackTrace(log); 284 } 285 return EXIT_ERROR; 286 } catch (BadArgs e) { 358 if (mod.equals(ALL_MODULE_PATH)) { 359 Path[] entries = options.modulePath.toArray(new Path[0]); 360 ModuleFinder finder = ModulePath.of(Runtime.version(), true, entries); 361 if (!options.limitMods.isEmpty()) { 362 // finder for the observable modules specified in 363 // the --module-path and --limit-modules options 364 finder = limitFinder(finder, options.limitMods, Collections.emptySet()); 365 } 366 367 // all observable modules are roots 368 finder.findAll() 369 .stream() 370 .map(ModuleReference::descriptor) 371 .map(ModuleDescriptor::name) 372 .forEach(mn -> roots.add(mn)); 373 } else { 374 roots.add(mod); 375 } 376 } 377 378 ModuleFinder finder = newModuleFinder(options.modulePath, options.limitMods, roots); 379 if (!finder.find("java.base").isPresent()) { 380 Path defModPath = getDefaultModulePath(); 381 if (defModPath != null) { 382 options.modulePath.add(defModPath); 383 } 384 finder = newModuleFinder(options.modulePath, options.limitMods, roots); 385 } 386 387 return new JlinkConfiguration(options.output, 388 roots, 389 options.endian, 390 finder); 391 } 392 393 private void createImage(JlinkConfiguration config) throws Exception { 394 if (options.output == null) { 395 throw taskHelper.newBadArgs("err.output.must.be.specified").showUsage(true); 396 } 397 if (options.addMods.isEmpty()) { 398 throw taskHelper.newBadArgs("err.mods.must.be.specified", "--add-modules") 399 .showUsage(true); 400 } 401 402 // First create the image provider 403 ImageProvider imageProvider = createImageProvider(config, 404 options.packagedModulesPath, 405 options.ignoreSigning, 406 options.bindServices, 407 options.verbose, 408 log); 409 410 // Then create the Plugin Stack 411 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration( 412 taskHelper.getPluginsConfig(options.output, options.launchers)); 413 414 //Ask the stack to proceed 415 stack.operate(imageProvider); 416 } 417 418 /** 419 * @return the system module path or null 420 */ 421 public static Path getDefaultModulePath() { 422 Path jmods = Paths.get(System.getProperty("java.home"), "jmods"); 423 return Files.isDirectory(jmods)? jmods : null; 424 } 425 426 /* 427 * Returns a module finder of the given module path that limits 428 * the observable modules to those in the transitive closure of 429 * the modules specified in {@code limitMods} plus other modules 430 * specified in the {@code roots} set. 431 */ 432 public static ModuleFinder newModuleFinder(List<Path> paths, 433 Set<String> limitMods, 434 Set<String> roots) 435 { 436 if (Objects.requireNonNull(paths).isEmpty()) { 437 throw new IllegalArgumentException("Empty module path"); 438 } 439 Path[] entries = paths.toArray(new Path[0]); 440 ModuleFinder finder = ModulePath.of(Runtime.version(), true, entries); 441 442 // if limitmods is specified then limit the universe 443 if (limitMods != null && !limitMods.isEmpty()) { 444 finder = limitFinder(finder, limitMods, Objects.requireNonNull(roots)); 445 } 446 return finder; 447 } 448 449 private static Path toPathLocation(ResolvedModule m) { 450 Optional<URI> ouri = m.reference().location(); 451 if (!ouri.isPresent()) 452 throw new InternalError(m + " does not have a location"); 453 URI uri = ouri.get(); 454 return Paths.get(uri); 455 } 456 457 458 private static ImageProvider createImageProvider(JlinkConfiguration config, 459 Path retainModulesPath, 460 boolean ignoreSigning, 461 boolean bindService, 462 boolean verbose, 463 PrintWriter log) 464 throws IOException |