private PmdResult run()

in src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java [151:266]


    private PmdResult run() throws MavenReportException {
        PMDConfiguration configuration = new PMDConfiguration();
        LanguageVersion languageVersion = null;
        Language language = configuration
                .getLanguageRegistry()
                .getLanguageById(request.getLanguage() != null ? request.getLanguage() : "java");
        if (language == null) {
            throw new MavenReportException("Unsupported language: " + request.getLanguage());
        }
        if (request.getLanguageVersion() != null) {
            languageVersion = language.getVersion(request.getLanguageVersion());
            if (languageVersion == null) {
                throw new MavenReportException("Unsupported targetJdk value '" + request.getLanguageVersion() + "'.");
            }
        } else {
            languageVersion = language.getDefaultVersion();
        }
        LOG.debug("Using language " + languageVersion);
        configuration.setDefaultLanguageVersion(languageVersion);

        if (request.getSourceEncoding() != null) {
            configuration.setSourceEncoding(Charset.forName(request.getSourceEncoding()));
        }

        configuration.prependAuxClasspath(request.getAuxClasspath());

        if (request.getSuppressMarker() != null) {
            configuration.setSuppressMarker(request.getSuppressMarker());
        }
        if (request.getAnalysisCacheLocation() != null) {
            configuration.setAnalysisCacheLocation(request.getAnalysisCacheLocation());
            LOG.debug("Using analysis cache location: " + request.getAnalysisCacheLocation());
        } else {
            configuration.setIgnoreIncrementalAnalysis(true);
        }

        configuration.setRuleSets(request.getRulesets());
        configuration.setMinimumPriority(RulePriority.valueOf(request.getMinimumPriority()));
        if (request.getBenchmarkOutputLocation() != null) {
            TimeTracker.startGlobalTracking();
        }
        List<File> files = request.getFiles();

        Report report = null;

        if (request.getRulesets().isEmpty()) {
            LOG.debug("Skipping PMD execution as no rulesets are defined.");
        } else {
            if (request.getBenchmarkOutputLocation() != null) {
                TimeTracker.startGlobalTracking();
            }

            try {
                report = processFilesWithPMD(configuration, files);
            } finally {
                if (request.getAuxClasspath() != null) {
                    ClassLoader classLoader = configuration.getClassLoader();
                    if (classLoader instanceof Closeable) {
                        Closeable closeable = (Closeable) classLoader;
                        try {
                            closeable.close();
                        } catch (IOException ex) {
                            // ignore
                        }
                    }
                }
                if (request.getBenchmarkOutputLocation() != null) {
                    TimingReport timingReport = TimeTracker.stopGlobalTracking();
                    writeBenchmarkReport(
                            timingReport, request.getBenchmarkOutputLocation(), request.getOutputEncoding());
                }
            }
        }

        if (report != null && !report.getProcessingErrors().isEmpty()) {
            List<Report.ProcessingError> errors = report.getProcessingErrors();
            if (!request.isSkipPmdError()) {
                LOG.error("PMD processing errors:");
                LOG.error(getErrorsAsString(errors, request.isDebugEnabled()));
                String msg = errors.size() > 1
                        ? "Found " + errors.size() + " PMD processing errors"
                        : "Found 1 PMD processing error";
                throw new MavenReportException(msg);
            }
            String message = errors.size() > 1
                    ? "There are " + errors.size() + " PMD processing errors:"
                    : "There is 1 PMD processing error:";
            LOG.warn(message);
            LOG.warn(getErrorsAsString(errors, request.isDebugEnabled()));
        }

        report = removeExcludedViolations(report);
        // Always write the XML report, as this might be needed by the check mojo.
        // We need to output it even if the file list is empty or there are no violations
        // so the "check" goals can check for violations.
        try {
            writeXmlReport(report);
        } catch (IOException e) {
            throw new MavenReportException("Failed to write XML report", e);
        }

        // write any other format except for xml and html. xml has just been produced.
        // html format is produced by the maven site formatter. Excluding html here
        // avoids using PMD's own html formatter, which doesn't fit into the maven site
        // considering the html/css styling
        String format = request.getFormat();
        if (!"html".equals(format) && !"xml".equals(format)) {
            try {
                writeFormattedReport(report);
            } catch (IOException e) {
                throw new MavenReportException("Failed to write formatted " + format + " report", e);
            }
        }

        return new PmdResult(new File(request.getTargetDirectory(), "pmd.xml"), request.getOutputEncoding());
    }