java.util.spi.ToolProvider Java Examples

The following examples show how to use java.util.spi.ToolProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PackagerPlugin.java    From pro with GNU General Public License v3.0 6 votes vote down vote up
private static int packageSourceOrDoc(Log log, ToolProvider jarTool, Path input, Path outputPath, PackagerConf packager, Map<String, Metadata> metadataMap, String suffix) {
  var moduleName = input.getFileName().toString();
  
  var metadataOpt = Optional.ofNullable(metadataMap.get(moduleName));
  var version = metadataOpt.flatMap(Metadata::version).orElse("1.0");
  
  var jar = new Jar(input, outputPath.resolve(moduleName + "-" + suffix + "-" + version + ".jar"));
  packager.rawArguments().ifPresent(jar::rawArguments);
  
  var cmdLine = new CmdLine().add("--create");
  cmdLine = OptionAction.gatherAll(JarOption.class, option -> option.action).apply(jar, cmdLine);
  var arguments = cmdLine.add(".").toArguments();
  
  log.verbose(jar, _jar -> OptionAction.toPrettyString(JarOption.class, option -> option.action).apply(_jar, "jar"));
  var exitCode = jarTool.run(System.out, System.err, arguments);
  return exitCode;
}
 
Example #2
Source File: ModuleReaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test ModuleReader to JMOD
 */
public void testJMod() throws IOException {
    Path dir = Files.createTempDirectory(USER_DIR, "mlib");

    // jmod create --class-path mods/${TESTMODULE}  mlib/${TESTMODULE}.jmod
    String cp = MODS_DIR.resolve(TEST_MODULE).toString();
    String jmod = dir.resolve("m.jmod").toString();
    String[] args = { "create", "--class-path", cp, jmod };
    ToolProvider jmodTool = ToolProvider.findFirst("jmod")
        .orElseThrow(() ->
            new RuntimeException("jmod tool not found")
        );
    assertEquals(jmodTool.run(System.out, System.out, args), 0);

    test(dir);
}
 
Example #3
Source File: PackagerPlugin.java    From pro with GNU General Public License v3.0 6 votes vote down vote up
private static int packageModule(Log log, ToolProvider jarTool, Path moduleExploded,  Path moduleArtifact, PackagerConf packager, Map<String, Metadata> metadataMap, String prefix) {
  var modules = ModuleFinder.of(moduleExploded).findAll();
  if (modules.size() != 1) {
    throw new IllegalStateException("more than one module packaged in the exploded module " + moduleExploded);
  }
  
  var moduleName = modules.iterator().next().descriptor().name(); 
  
  var metadataOpt = Optional.ofNullable(metadataMap.get(moduleName));
  var version = metadataOpt.flatMap(Metadata::version).orElse("1.0");
  var jar = new Jar(moduleExploded, moduleArtifact.resolve(prefix + moduleName + "-" + version + ".jar"));
  jar.setModuleVersion(version);
  metadataOpt.flatMap(Metadata::mainClass).ifPresent(jar::setMainClass);
  packager.rawArguments().ifPresent(jar::rawArguments);
  
  var cmdLine = new CmdLine().add("--create");
  cmdLine = OptionAction.gatherAll(JarOption.class, option -> option.action).apply(jar, cmdLine);
  var arguments = cmdLine.add(".").toArguments();
  
  log.verbose(jar, _jar -> OptionAction.toPrettyString(JarOption.class, option -> option.action).apply(_jar, "jar"));
  var exitCode = jarTool.run(System.out, System.err, arguments);
  return exitCode;
}
 
Example #4
Source File: DocerPlugin.java    From pro with GNU General Public License v3.0 6 votes vote down vote up
private static int generateDoc(Log log, ToolProvider javadocTool, DocerConf docerConf, List<Path> sourcePath, Optional<Integer> release, Path input, Path output) {
  var javadoc = new Javadoc(output.resolve(input.getFileName().toString()), sourcePath);
  docerConf.rawArguments().ifPresent(javadoc::rawArguments);
  javadoc.modulePath(docerConf.moduleDependencyPath());
  docerConf.upgradeModulePath().ifPresent(javadoc::upgradeModulePath);
  docerConf.rootModules().ifPresent(javadoc::rootModules);
  javadoc.quiet(docerConf.quiet());
  javadoc.html5(docerConf.html5());
  release.ifPresent(javadoc::release);
  docerConf.enablePreview().ifPresent(javadoc::enablePreview);
  docerConf.link().filter(url -> isLinkHostOnline(log, url)).ifPresent(javadoc::link);
  
  var cmdLine = gatherAll(JavadocOption.class, option -> option.action).apply(javadoc, new CmdLine());
  var files = docerConf.files().orElseGet(
      () -> walkIfNecessary(List.of(input), pathFilenameEndsWith(".java")));  //FIXME, use rootNames ??
  files.forEach(cmdLine::add);
  var arguments = cmdLine.toArguments();
  log.verbose(files, fs -> OptionAction.toPrettyString(JavadocOption.class, option -> option.action).apply(javadoc, "javadoc") + "\n" + fs.stream().map(Path::toString).collect(Collectors.joining(" ")));
  
  return javadocTool.run(System.out, System.err, arguments);
}
 
Example #5
Source File: Linker.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
private Linker(Configuration config) {
    this.jlink = ToolProvider.findFirst(JLINK_TOOL_NAME).orElseThrow(() -> new IllegalStateException("jlink not found"));
    this.jlinkArgs = new ArrayList<>();
    this.config = config;
    this.imageName = fileName(config.jriDirectory());

    if (config.verbose()) {
        System.setProperty(JLINK_DEBUG_PROPERTY, "true");
    }
}
 
Example #6
Source File: ToolProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testProviders() throws Exception {
    Map<String, ToolProvider> providers = new LinkedHashMap<>();
    for (ToolProvider tp : ServiceLoader.load(ToolProvider.class,
            ClassLoader.getSystemClassLoader())) {
        System.out.println("Provider: " + tp.name() + ": " + tp.getClass().getName());
        providers.put(tp.name(), tp);
    }
    if (!providers.containsKey("javadoc")) {
        error("javadoc ToolProvider not found");
    }
}
 
Example #7
Source File: ToolProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testProviders() throws Exception {
    Map<String, ToolProvider> providers = new LinkedHashMap<>();
    for (ToolProvider tp : ServiceLoader.load(ToolProvider.class,
            ClassLoader.getSystemClassLoader())) {
        System.out.println("Provider: " + tp.name() + ": " + tp.getClass().getName());
        providers.put(tp.name(), tp);
    }
    if (!providers.containsKey("javac")) {
        error("javac ToolProvider not found");
    }
}
 
Example #8
Source File: ToolProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void test() throws Exception {
    ToolProvider testProvider = ToolProvider.findFirst("test").get();
    int rc = testProvider.run(System.out, System.err, "hello test");
    if (rc != 0) {
        throw new Exception("unexpected exit code: " + rc);
    }
}
 
Example #9
Source File: ToolProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void initServices() throws IOException {
    Path testClasses = Paths.get(System.getProperty("test.classes"));
    Path services = testClasses.resolve(Paths.get("META-INF", "services"));
    Files.createDirectories(services);
    Files.write(services.resolve(ToolProvider.class.getName()),
            Arrays.asList(TestProvider.class.getName()));
}
 
Example #10
Source File: ImageModules.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static ToolResult execTool(ToolProvider tool, String... args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    List<String> filteredArgs = Stream.of(args)
                                      .map(s -> s.split(" ")).flatMap(Stream::of)
                                      .filter(s -> !s.equals(""))
                                      .collect(Collectors.toList());
    System.out.println(tool + " " + filteredArgs);
    int ec = tool.run(ps, ps, filteredArgs.toArray(new String[] {}));
    return new ToolResult(ec, new String(baos.toByteArray(), UTF_8));
}
 
Example #11
Source File: JavaClassPathTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeTest
public void setup() throws Exception {
    boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
                                             MODS_DIR.resolve(TEST_MODULE));
    assertTrue(compiled, "module " + TEST_MODULE + " did not compile");

    // add the class and a resource to the current working directory
    Path file = Paths.get("jdk/test/Main.class");
    Files.createDirectories(file.getParent());
    Files.copy(MODS_DIR.resolve(TEST_MODULE).resolve(file), file);

    Path res = Paths.get("jdk/test/res.properties");
    Files.createFile(res);

    ToolProvider jartool = ToolProvider.findFirst("jar").orElseThrow(
        () -> new RuntimeException("jar tool not found")
    );

    Path jarfile = LIB_DIR.resolve("m.jar");
    Files.createDirectories(LIB_DIR);
    assertTrue(jartool.run(System.out, System.err, "cfe",
                           jarfile.toString(), TEST_MAIN,
                           file.toString()) == 0);

    Path manifest = LIB_DIR.resolve("manifest");
    try (BufferedWriter writer = Files.newBufferedWriter(manifest)) {
        writer.write("CLASS-PATH: lib/m.jar");
    }
    jarfile = LIB_DIR.resolve("m1.jar");
    assertTrue(jartool.run(System.out, System.err, "cfme",
                           jarfile.toString(), manifest.toString(), TEST_MAIN,
                           file.toString()) == 0);
}
 
Example #12
Source File: ModuleFixerPlugin.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static int patchModularJar(ToolProvider jarTool, ModuleReference ref, Path generatedModuleInfo) {
  System.out.println("[modulefixer] fix " + ref.descriptor().name());
  return jarTool.run(System.out, System.err,
      "--update",
      "--file", Path.of(ref.location().orElseThrow()).toString(),
      "-C", generatedModuleInfo.getParent().toString(),
      generatedModuleInfo.getFileName().toString());
}
 
Example #13
Source File: CompilerPlugin.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static int compileAllFiles(Log log, ToolProvider javacTool, Javac javac, Optional<Integer> release, List<Path> files) {
  release.ifPresent(javac::release);
  var cmdLine = gatherAll(JavacOption.class, option -> option.action).apply(javac, new CmdLine());
  files.forEach(cmdLine::add);

  var arguments = cmdLine.toArguments();
  log.verbose(files, fs -> toPrettyString(JavacOption.class, option -> option.action).apply(javac, "javac") + "\n" + fs.stream().map(Path::toString).collect(joining(" ")));

  return javacTool.run(System.out, System.err, arguments);
}
 
Example #14
Source File: LinkerPlugin.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int execute(Config config) throws IOException {
  var log = Log.create(name(), config.getOrThrow("pro", ProConf.class).loglevel());
  log.debug(config, conf -> "config " + config);
  
  var jlinkTool = ToolProvider.findFirst("jlink").orElseThrow(() -> new IllegalStateException("can not find jlink"));
  var linkerConf = config.getOrThrow(name(), LinkerConf.class);
  
  var systemModulePath = linkerConf.systemModulePath();
  if (!(Files.exists(systemModulePath))) {
    throw new IOException("unable to find system modules at " + systemModulePath);
  }
  
  var moduleFinder = ModuleFinder.of(linkerConf.moduleArtifactSourcePath());
  var rootModules = linkerConf.rootModules().map(HashSet::new).orElseGet(() -> {
    return moduleFinder.findAll().stream()
        .map(reference -> reference.descriptor().name())
        .collect(Collectors.toCollection(HashSet::new));
  });
  linkerConf.serviceNames().ifPresent(serviceNames -> {
    ModuleFinder rootFinder = ModuleFinder.compose(moduleFinder, ModuleHelper.systemModulesFinder());
    ModuleHelper.findAllModulesWhichProvideAService(serviceNames, rootFinder)
      .map(ref -> ref.descriptor().name())
      .forEach(rootModules::add);
  });
  
  // find launchers
  var launchers = linkerConf.launchers().orElseGet(() -> findLaunchersFromMainClasses(rootModules, moduleFinder));
  if (launchers.isEmpty()) {
    log.error(null, __ -> "no launcher found and no main classes defined in the root modules");
    return 1; //FIXME
  }
  
  var modulePath =
      linkerConf.modulePath()
        .orElseGet(() -> StableList.of(linkerConf.moduleArtifactSourcePath())
              .appendAll(FileHelper.pathFromFilesThatExist(linkerConf.moduleDependencyPath()))
              .append(systemModulePath));
  
  log.debug(rootModules, roots -> "rootModules " + roots);
  log.debug(launchers, launcherMains -> "launchers " + launcherMains);
  var jlink = new Jlink(linkerConf, rootModules, launchers, modulePath);
  
  var destination = linkerConf.destination();
  FileHelper.deleteAllFiles(destination, true);
  
  var arguments = OptionAction.gatherAll(JlinkOption.class, option -> option.action).apply(jlink, new CmdLine()).toArguments();
  log.verbose(null, __ -> OptionAction.toPrettyString(JlinkOption.class, option -> option.action).apply(jlink, "jlink"));
  
  var errorCode = jlinkTool.run(System.out, System.err, arguments);
  if (errorCode != 0) {
    return errorCode; 
  }
  
  if (linkerConf.includeSystemJMODs()) {
    var jmods = destination.resolve("jmods");
    Files.createDirectories(jmods);
    try(var directoryStream = Files.newDirectoryStream(systemModulePath)) {
      for(var path: directoryStream) {
        Files.copy(path, jmods.resolve(path.getFileName()));
      }
    }
  }
  return 0;
}
 
Example #15
Source File: PackagerPlugin.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int execute(Config config) throws IOException {
  var log = Log.create(name(), config.getOrThrow("pro", ProConf.class).loglevel());
  log.debug(config, conf -> "config " + config);
  
  var jarTool = ToolProvider.findFirst("jar").orElseThrow(() -> new IllegalStateException("can not find jar"));
  var packagerConf = config.getOrThrow(name(), PackagerConf.class);
  
  var moduleExplodedSourcePath = FileHelper.pathFromFilesThatExist(packagerConf.moduleExplodedSourcePath());
  var moduleArtifactSourcePath = packagerConf.moduleArtifactSourcePath();
  var moduleExplodedTestPath = FileHelper.pathFromFilesThatExist(packagerConf.moduleExplodedTestPath());
  var moduleArtifactTestPath = packagerConf.moduleArtifactTestPath();
  
  @SuppressWarnings("deprecation")
  var modules = packagerConf.modules().or(() -> packagerConf.moduleMetadata());
  var metadataMap = modules.map(MetadataParser::parse).orElse(Map.of());
  
  var errorCode = packageAll(moduleExplodedSourcePath, moduleArtifactSourcePath,
      (input, output) -> packageModule(log, jarTool, input, output, packagerConf, metadataMap, ""));
  if (errorCode != 0) {
    return errorCode;
  }
  
  if (Files.exists(packagerConf.moduleDocSourcePath())) {
    errorCode = packageAll(List.of(packagerConf.moduleDocSourcePath()), packagerConf.moduleDocArtifactSourcePath(),
        (input, output) -> packageSourceOrDoc(log, jarTool, input, output, packagerConf, metadataMap, "doc"));
    if (errorCode != 0) {
      return errorCode;
    }
  }
  errorCode = packageAll(FileHelper.pathFromFilesThatExist(packagerConf.moduleSourcePath()), packagerConf.moduleSrcArtifactSourcePath(),
      (input, output) -> packageSourceOrDoc(log, jarTool, input, output, packagerConf, metadataMap, "source"));
  if (errorCode != 0) {
    return errorCode;
  } 
  if (!moduleExplodedTestPath.stream().anyMatch(Files::exists)) {
    return 0;
  }
  errorCode = packageAll(moduleExplodedTestPath, moduleArtifactTestPath,
      (input, output) -> packageModule(log, jarTool, input, output, packagerConf, metadataMap, "test-"));
  if (errorCode != 0) {
    return errorCode;
  }
  if (Files.exists(packagerConf.moduleDocTestPath())) {
    errorCode = packageAll(List.of(packagerConf.moduleDocTestPath()), packagerConf.moduleDocArtifactTestPath(),
        (input, output) -> packageSourceOrDoc(log, jarTool, input, output, packagerConf, metadataMap, "test-doc"));
    if (errorCode != 0) {
      return errorCode;
    }
  }
  if (packagerConf.generateSourceTestBale()) { 
    packageAll(FileHelper.pathFromFilesThatExist(packagerConf.moduleTestPath()), packagerConf.moduleSrcArtifactTestPath(),
        (input, output) -> packageSourceOrDoc(log, jarTool, input, output, packagerConf, metadataMap, "test-source"));
  }
  return 0;
}
 
Example #16
Source File: DocerPlugin.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int execute(Config config) throws IOException {
  var log = Log.create(name(), config.getOrThrow("pro", ProConf.class).loglevel());
  log.debug(config, conf -> "config " + config);
  
  var javadocTool = ToolProvider.findFirst("javadoc")
      .orElseThrow(() -> new IllegalStateException("can not find javadoc"));
  var docerConf = config.getOrThrow(name(), DocerConf.class);

  // find layout
  var sourceLayoutFactory = ModuleSourceLayout.Factory.of(ModuleSourceLayout::lookupForJdkLayout)
      .or(ModuleSourceLayout::lookupForMavenMultiLayout);
  var layoutOpt = sourceLayoutFactory.createLayout(Path.of("."));
  if (layoutOpt.isEmpty()) {
    log.error(null, __ -> "no source layout found");
    return 1; //FIXME
  }
  var layout = layoutOpt.orElseThrow();
  log.verbose(layout, _layout -> layout + " detected");

  // generate javadoc of source
  var sourceModuleRefs = ModuleHelper.topologicalSort(layout.findModuleRefs(docerConf.moduleSourcePath()));
  if (!sourceModuleRefs.isEmpty()) {
    var docerModuleSourcePath = layout.toAllPath(docerConf.moduleSourcePath());
    log.verbose(docerModuleSourcePath, path -> "docerModuleSourcePath: " + path);

    var sourceRelease = docerConf.sourceRelease();
    var errorCode = generateAll(layout, sourceModuleRefs, docerConf.moduleSourcePath(), docerConf.moduleDocSourcePath(),
        (input, output) -> generateDoc(log, javadocTool, docerConf, docerModuleSourcePath, sourceRelease, input, output));
    if (errorCode != 0) {
      return errorCode;
    }
  } else {
    log.info(null, __ -> "no source found");
  }

  // generate javadoc of tests
  var mergedTestModuleRefs = ModuleHelper.topologicalSort(JDK_LAYOUT.findModuleRefs(docerConf.moduleMergedTestPath()));
  if (docerConf.generateTestDoc() && !mergedTestModuleRefs.isEmpty()) {
    var docerModuleMergedTestPath = layout.toAllPath(docerConf.moduleMergedTestPath());
    log.verbose(docerModuleMergedTestPath, path -> "docerModuleMergedTestPath: " + path);

    var testRelease = docerConf.testRelease().or(docerConf::sourceRelease);
    return generateAll(layout, mergedTestModuleRefs, docerConf.moduleMergedTestPath(), docerConf.moduleDocTestPath(),
        (input, output) -> generateDoc(log, javadocTool, docerConf, docerModuleMergedTestPath, testRelease, input, output));
  } else {
    log.info(null, __ -> "no test found");
  }

  return 0;
}
 
Example #17
Source File: ToolProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
ToolProviderTest() {
    super(System.err);
    javac = ToolProvider.findFirst("javac").get();
}
 
Example #18
Source File: ToolProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
ToolProviderTest() {
    super(System.err);
    javadoc = ToolProvider.findFirst("javadoc").get();
}