private void compile()

in src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java [1273:1360]


    private void compile(final JavaCompiler compiler, final Options configuration) throws IOException {
        final ToolExecutor executor = createExecutor(null);
        if (!executor.applyIncrementalBuild(this, configuration)) {
            return;
        }
        Exception failureCause = null;
        final var compilerOutput = new StringWriter();
        boolean success;
        try {
            success = executor.compile(compiler, configuration, compilerOutput);
        } catch (Exception e) {
            success = false;
            failureCause = e;
        }
        /*
         * The compilation errors or warnings should have already been reported by `DiagnosticLogger`.
         * However, the compiler may have other messages not associated to a particular source file.
         * For example, `ForkedCompiler` uses this writer if the compilation has been interrupted.
         */
        String additionalMessage = compilerOutput.toString();
        if (!additionalMessage.isBlank()) {
            if (success || failureCause != null) { // Keep the error level for the exception message.
                logger.warn(additionalMessage);
            } else {
                logger.error(additionalMessage);
            }
        }
        if (failureCause != null) {
            String message = failureCause.getMessage();
            if (message != null) {
                logger.error(message);
            } else {
                logger.error(failureCause);
            }
        }
        /*
         * In case of failure, or if debugging is enabled, dump the options to a file.
         * By default, the file will have the ".args" extension.
         */
        if (!success || verbose || logger.isDebugEnabled()) {
            IOException suppressed = null;
            try {
                writeDebugFile(executor, configuration);
                if (success && tipForCommandLineCompilation != null) {
                    logger.debug(tipForCommandLineCompilation);
                    tipForCommandLineCompilation = null;
                }
            } catch (IOException e) {
                suppressed = e;
            }
            if (!success) {
                var message = new StringBuilder(100)
                        .append("Cannot compile ")
                        .append(project.getId())
                        .append(' ')
                        .append(compileScope.projectScope().id())
                        .append(" classes.");
                if (executor.listener instanceof DiagnosticLogger diagnostic) {
                    diagnostic.firstError(failureCause).ifPresent((c) -> message.append(System.lineSeparator())
                            .append("The first error is: ")
                            .append(c));
                }
                var failure = new CompilationFailureException(message.toString(), failureCause);
                if (suppressed != null) {
                    failure.addSuppressed(suppressed);
                }
                throw failure;
            }
            if (suppressed != null) {
                throw suppressed;
            }
        }
        /*
         * Workaround for MCOMPILER-542, needed only if a modular project is compiled with a JDK older than Java 22.
         * Note: a previous version used as an heuristic way to detect if Reproducible Build was enabled. This check
         * has been removed because Reproducible Build are enabled by default in Maven now.
         */
        if (!isVersionEqualOrNewer(compiler, "RELEASE_22")) {
            Path moduleDescriptor = executor.outputDirectory.resolve(MODULE_INFO + CLASS_FILE_SUFFIX);
            if (Files.isRegularFile(moduleDescriptor)) {
                byte[] oridinal = Files.readAllBytes(moduleDescriptor);
                byte[] modified = ByteCodeTransformer.patchJdkModuleVersion(oridinal, getRelease(), logger);
                if (modified != null) {
                    Files.write(moduleDescriptor, modified);
                }
            }
        }
    }