public void execute()

in src/main/org/apache/tools/ant/taskdefs/modules/Jmod.java [1074:1198]


    public void execute()
    throws BuildException {

        if (jmodFile == null) {
            throw new BuildException("Destination file is required.",
                getLocation());
        }

        if (classpath == null) {
            throw new BuildException("Classpath is required.",
                getLocation());
        }

        if (classpath.stream().noneMatch(Resource::isExists)) {
            throw new BuildException(
                "Classpath must contain at least one entry which exists.",
                getLocation());
        }

        if (version != null && moduleVersion != null) {
            throw new BuildException(
                "version attribute and nested <version> element "
                + "cannot both be present.",
                getLocation());
        }

        if (hashModulesPattern != null && !hashModulesPattern.isEmpty()
            && modulePath == null) {

            throw new BuildException(
                "hashModulesPattern requires a module path, since "
                + "it will generate hashes of the other modules which depend "
                + "on the module being created.",
                getLocation());
        }

        checkDirPaths();

        Path[] dependentPaths = {
            classpath,
            modulePath,
            commandPath,
            configPath,
            headerPath,
            legalPath,
            nativeLibPath,
            manPath,
        };
        Union allResources = new Union(getProject());
        for (Path path : dependentPaths) {
            if (path != null) {
                for (String entry : path.list()) {
                    File entryFile = new File(entry);
                    if (entryFile.isDirectory()) {
                        log("Will compare timestamp of all files in "
                            + "\"" + entryFile + "\" with timestamp of "
                            + jmodFile, Project.MSG_VERBOSE);
                        FileSet fileSet = new FileSet();
                        fileSet.setDir(entryFile);
                        allResources.add(fileSet);
                    } else {
                        log("Will compare timestamp of \"" + entryFile + "\" "
                            + "with timestamp of " + jmodFile,
                            Project.MSG_VERBOSE);
                        allResources.add(new FileResource(entryFile));
                    }
                }
            }
        }

        ResourceCollection outOfDate =
            ResourceUtils.selectOutOfDateSources(this, allResources,
                new MergingMapper(jmodFile.toString()),
                getProject(),
                FileUtils.getFileUtils().getFileTimestampGranularity());

        if (outOfDate.isEmpty()) {
            log("Skipping jmod creation, since \"" + jmodFile + "\" "
                + "is already newer than all files in paths.",
                Project.MSG_VERBOSE);
            return;
        }

        Collection<String> args = buildJmodArgs();

        try {
            log("Deleting " + jmodFile + " if it exists.", Project.MSG_VERBOSE);
            Files.deleteIfExists(jmodFile.toPath());
        } catch (IOException e) {
            throw new BuildException(
                "Could not remove old file \"" + jmodFile + "\": " + e, e,
                getLocation());
        }

        ToolProvider jmod = ToolProvider.findFirst("jmod").orElseThrow(
            () -> new BuildException("jmod tool not found in JDK.",
                getLocation()));

        log("Executing: jmod " + String.join(" ", args), Project.MSG_VERBOSE);

        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        ByteArrayOutputStream stderr = new ByteArrayOutputStream();

        int exitCode;
        try (PrintStream out = new PrintStream(stdout);
             PrintStream err = new PrintStream(stderr)) {

            exitCode = jmod.run(out, err, args.toArray(new String[0]));
        }

        if (exitCode != 0) {
            StringBuilder message = new StringBuilder();
            message.append("jmod failed (exit code ").append(exitCode).append(")");
            if (stdout.size() > 0) {
                message.append(", output is: ").append(stdout);
            }
            if (stderr.size() > 0) {
                message.append(", error output is: ").append(stderr);
            }

            throw new BuildException(message.toString(), getLocation());
        }

        log("Created " + jmodFile.getAbsolutePath(), Project.MSG_INFO);
    }