private void compile()

in tomee-patch-plugin/src/main/java/org/apache/tomee/patch/plugin/PatchMojo.java [473:629]


    private void compile(final List<File> jars) throws MojoExecutionException, MojoFailureException {
        final List<File> files = resolve(dependencies);

        getLog().debug("Using compiler '" + compilerId + "'.");

        final Compiler compiler;

        try {
            compiler = compilerManager.getCompiler(compilerId);
        } catch (NoSuchCompilerException e) {
            throw new MojoExecutionException("No such compiler '" + e.getCompilerId() + "'.");
        }

        final Toolchain tc = getToolchain();
        if (tc != null) {
            getLog().info("Toolchain in maven-compiler-plugin: " + tc);
            //TODO somehow shaky dependency between compilerId and tool executable.
            executable = tc.findTool(compilerId);
        }

        final File defaultPatchSources = new File(basedir, "src/patch/java");
        Files.mkdir(patchSourceDirectory);
        for (final File patchSource : patchSources) {
            if (patchSource == null) continue;
            if (!patchSource.exists()) {

                if (patchSource.getAbsolutePath().equals(defaultPatchSources.getAbsolutePath())) {
                    // If the default directory does not exist, the user likely did not explicitly
                    // ask for it.  Just silently skip it.
                    continue;
                }

                final String message = "Patch source directory does not exist: " + patchSource.getAbsolutePath();
                getLog().error(message);
                throw new MojoExecutionException(message);
            }
            if (!patchSource.isDirectory()) {
                final String message = "Patch source directory is not a directory: " + patchSource.getAbsolutePath();
                getLog().error(message);
                throw new MojoExecutionException(message);
            }
        }
        patchSources.stream()
                .filter(Objects::nonNull)
                .filter(File::exists)
                .forEach(file -> copy(file, file, patchSourceDirectory));


        final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setOutputLocation(buildDirectory.getAbsolutePath());
        compilerConfiguration.setOptimize(false);
        compilerConfiguration.setDebug(true);
        compilerConfiguration.setParameters(false);
        compilerConfiguration.setVerbose(false);
        compilerConfiguration.setShowWarnings(false);
        compilerConfiguration.setFailOnWarning(false);
        compilerConfiguration.setShowDeprecation(false);
        compilerConfiguration.setSourceVersion(source);
        compilerConfiguration.setTargetVersion(target);
        compilerConfiguration.setReleaseVersion(null);
        compilerConfiguration.setProc(null);
        compilerConfiguration.setSourceLocations(Collections.singletonList(patchSourceDirectory.getAbsolutePath()));
        compilerConfiguration.setAnnotationProcessors(null);
        compilerConfiguration.setSourceEncoding(encoding);
        compilerConfiguration.setFork(true);
        compilerConfiguration.setExecutable(executable);
        compilerConfiguration.setWorkingDirectory(basedir);
        compilerConfiguration.setCompilerVersion(compilerVersion);
        compilerConfiguration.setBuildDirectory(buildDirectory);
        compilerConfiguration.setOutputFileName(null);

        // Add each jar as a classpath entry
        jars.stream()
                .map(File::getAbsolutePath)
                .forEach(compilerConfiguration::addClasspathEntry);

        files.stream()
                .map(File::getAbsolutePath)
                .forEach(compilerConfiguration::addClasspathEntry);

        // Now we can compile!
        final CompilerResult compilerResult;
        try {
            compilerResult = compiler.performCompile(compilerConfiguration);
        } catch (Exception e) {
            throw new MojoExecutionException("Fatal error compiling", e);
        }

        List<CompilerMessage> warnings = new ArrayList<>();
        List<CompilerMessage> errors = new ArrayList<>();
        List<CompilerMessage> others = new ArrayList<>();
        for (CompilerMessage message : compilerResult.getCompilerMessages()) {
            if (message.getKind() == CompilerMessage.Kind.ERROR) {
                errors.add(message);
            } else if (message.getKind() == CompilerMessage.Kind.WARNING
                    || message.getKind() == CompilerMessage.Kind.MANDATORY_WARNING) {
                warnings.add(message);
            } else {
                others.add(message);
            }
        }

        if (!compilerResult.isSuccess()) {
            for (CompilerMessage message : others) {
                assert message.getKind() != CompilerMessage.Kind.ERROR
                        && message.getKind() != CompilerMessage.Kind.WARNING
                        && message.getKind() != CompilerMessage.Kind.MANDATORY_WARNING;
                getLog().info(message.toString());
            }
            if (!warnings.isEmpty()) {
                getLog().info("-------------------------------------------------------------");
                getLog().warn("COMPILATION WARNING : ");
                getLog().info("-------------------------------------------------------------");
                for (CompilerMessage warning : warnings) {
                    getLog().warn(warning.toString());
                }
                getLog().info(warnings.size() + ((warnings.size() > 1) ? " warnings " : " warning"));
                getLog().info("-------------------------------------------------------------");
            }

            if (!errors.isEmpty()) {
                getLog().info("-------------------------------------------------------------");
                getLog().error("COMPILATION ERROR : ");
                getLog().info("-------------------------------------------------------------");
                for (CompilerMessage error : errors) {
                    getLog().error(error.toString());
                }
                getLog().info(errors.size() + ((errors.size() > 1) ? " errors " : " error"));
                getLog().info("-------------------------------------------------------------");
            }

            if (!errors.isEmpty()) {
                throw new CompilationFailureException(errors);
            } else {
                throw new CompilationFailureException(warnings);
            }
        } else {
            for (CompilerMessage message : compilerResult.getCompilerMessages()) {
                switch (message.getKind()) {
                    case NOTE:
                    case OTHER:
                        getLog().info(message.toString());
                        break;

                    case ERROR:
                        getLog().error(message.toString());
                        break;

                    case MANDATORY_WARNING:
                    case WARNING:
                    default:
                        getLog().warn(message.toString());
                        break;
                }
            }
        }
    }