private CpdResult run()

in src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java [137:184]


    private CpdResult run() throws MavenReportException {
        try {
            excludeDuplicationsFromFile.loadExcludeFromFailuresData(request.getExcludeFromFailureFile());
        } catch (MojoExecutionException e) {
            throw new MavenReportException("Error loading exclusions", e);
        }

        CPDConfiguration cpdConfiguration = new CPDConfiguration();
        cpdConfiguration.setMinimumTileSize(request.getMinimumTokens());
        cpdConfiguration.setIgnoreAnnotations(request.isIgnoreAnnotations());
        cpdConfiguration.setIgnoreLiterals(request.isIgnoreLiterals());
        cpdConfiguration.setIgnoreIdentifiers(request.isIgnoreIdentifiers());
        // we are not running CPD through CLI and deal with any errors during analysis on our own
        cpdConfiguration.setSkipLexicalErrors(true);

        String languageId = request.getLanguage();
        if ("javascript".equals(languageId)) {
            languageId = "ecmascript";
        } else if (languageId == null) {
            languageId = "java"; // default
        }
        Language cpdLanguage = cpdConfiguration.getLanguageRegistry().getLanguageById(languageId);

        cpdConfiguration.setOnlyRecognizeLanguage(cpdLanguage);
        cpdConfiguration.setSourceEncoding(Charset.forName(request.getSourceEncoding()));

        request.getFiles().forEach(f -> cpdConfiguration.addInputPath(f.toPath()));

        LOG.debug("Executing CPD...");
        try (CpdAnalysis cpd = CpdAnalysis.create(cpdConfiguration)) {
            CpdReportConsumer reportConsumer = new CpdReportConsumer(request, excludeDuplicationsFromFile);
            cpd.performAnalysis(reportConsumer);
        } catch (IOException e) {
            throw new MavenReportException("Error while executing CPD", e);
        }
        LOG.debug("CPD finished.");

        // in contrast to pmd goal, we don't have a parameter for cpd like "skipPmdError" - if there
        // are any errors during CPD analysis, the maven build fails.
        int cpdErrors = cpdConfiguration.getReporter().numErrors();
        if (cpdErrors == 1) {
            throw new MavenReportException("There was 1 error while executing CPD");
        } else if (cpdErrors > 1) {
            throw new MavenReportException("There were " + cpdErrors + " errors while executing CPD");
        }

        return new CpdResult(new File(request.getTargetDirectory(), "cpd.xml"), request.getOutputEncoding());
    }