Search in sources :

Example 1 with FileResourceCreationException

use of org.codehaus.plexus.resource.loader.FileResourceCreationException in project maven-plugins by apache.

the class PmdReport method executePmd.

private void executePmd() throws MavenReportException {
    if (renderer != null) {
        // PMD has already been run
        getLog().debug("PMD has already been run - skipping redundant execution.");
        return;
    }
    try {
        excludeFromFile.loadExcludeFromFailuresData(excludeFromFailureFile);
    } catch (MojoExecutionException e) {
        throw new MavenReportException("Unable to load exclusions", e);
    }
    // configure ResourceManager
    locator.addSearchPath(FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath());
    locator.addSearchPath("url", "");
    locator.setOutputDirectory(targetDirectory);
    renderer = new PmdCollectingRenderer();
    PMDConfiguration pmdConfiguration = getPMDConfiguration();
    String[] sets = new String[rulesets.length];
    try {
        for (int idx = 0; idx < rulesets.length; idx++) {
            String set = rulesets[idx];
            getLog().debug("Preparing ruleset: " + set);
            RuleSetReferenceId id = new RuleSetReferenceId(set);
            File ruleset = locator.getResourceAsFile(id.getRuleSetFileName(), getLocationTemp(set));
            if (null == ruleset) {
                throw new MavenReportException("Could not resolve " + set);
            }
            sets[idx] = ruleset.getAbsolutePath();
        }
    } catch (ResourceNotFoundException e) {
        throw new MavenReportException(e.getMessage(), e);
    } catch (FileResourceCreationException e) {
        throw new MavenReportException(e.getMessage(), e);
    }
    pmdConfiguration.setRuleSets(StringUtils.join(sets, ","));
    try {
        if (filesToProcess == null) {
            filesToProcess = getFilesToProcess();
        }
        if (filesToProcess.isEmpty() && !"java".equals(language)) {
            getLog().warn("No files found to process. Did you add your additional source folders like javascript?" + " (see also build-helper-maven-plugin)");
        }
    } catch (IOException e) {
        throw new MavenReportException("Can't get file list", e);
    }
    String encoding = getSourceEncoding();
    if (StringUtils.isEmpty(encoding) && !filesToProcess.isEmpty()) {
        getLog().warn("File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING + ", i.e. build is platform dependent!");
        encoding = ReaderFactory.FILE_ENCODING;
    }
    pmdConfiguration.setSourceEncoding(encoding);
    List<DataSource> dataSources = new ArrayList<>(filesToProcess.size());
    for (File f : filesToProcess.keySet()) {
        dataSources.add(new FileDataSource(f));
    }
    if (sets.length > 0) {
        processFilesWithPMD(pmdConfiguration, dataSources);
    } else {
        getLog().debug("Skipping PMD execution as no rulesets are defined.");
    }
    if (renderer.hasErrors()) {
        if (!skipPmdError) {
            getLog().error("PMD processing errors:");
            getLog().error(renderer.getErrorsAsString());
            throw new MavenReportException("Found " + renderer.getErrors().size() + " PMD processing errors");
        }
        getLog().warn("There are " + renderer.getErrors().size() + " PMD processing errors:");
        getLog().warn(renderer.getErrorsAsString());
    }
    removeExcludedViolations(renderer.getViolations());
    // so the "check" goals can check for violations
    if (isXml() && renderer != null) {
        writeNonHtml(renderer.asReport());
    }
    if (benchmark) {
        try (PrintStream benchmarkFileStream = new PrintStream(benchmarkOutputFilename)) {
            (new TextReport()).generate(Benchmarker.values(), benchmarkFileStream);
        } catch (FileNotFoundException fnfe) {
            getLog().error("Unable to generate benchmark file: " + benchmarkOutputFilename, fnfe);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) RuleSetReferenceId(net.sourceforge.pmd.RuleSetReferenceId) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) TextReport(net.sourceforge.pmd.benchmark.TextReport) DataSource(net.sourceforge.pmd.util.datasource.DataSource) FileDataSource(net.sourceforge.pmd.util.datasource.FileDataSource) FileDataSource(net.sourceforge.pmd.util.datasource.FileDataSource) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Example 2 with FileResourceCreationException

use of org.codehaus.plexus.resource.loader.FileResourceCreationException in project maven-plugins by apache.

the class DefaultCheckstyleExecutor method getOverridingProperties.

private Properties getOverridingProperties(CheckstyleExecutorRequest request) throws CheckstyleExecutorException {
    Properties p = new Properties();
    InputStream in = null;
    try {
        if (request.getPropertiesLocation() != null) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("request.getPropertiesLocation() " + request.getPropertiesLocation());
            }
            File propertiesFile = locator.getResourceAsFile(request.getPropertiesLocation(), "checkstyle-checker.properties");
            if (propertiesFile != null) {
                in = new FileInputStream(propertiesFile);
                p.load(in);
                in.close();
                in = null;
            }
        }
        if (StringUtils.isNotEmpty(request.getPropertyExpansion())) {
            String propertyExpansion = request.getPropertyExpansion();
            // Convert \ to \\, so that p.load will convert it back properly
            propertyExpansion = StringUtils.replace(propertyExpansion, "\\", "\\\\");
            p.load(new ByteArrayInputStream(propertyExpansion.getBytes()));
        }
        // Workaround for MCHECKSTYLE-48
        // Make sure that "config/maven-header.txt" is the default value
        // for headerLocation, if configLocation="config/maven_checks.xml"
        String headerLocation = request.getHeaderLocation();
        if ("config/maven_checks.xml".equals(request.getConfigLocation())) {
            if ("LICENSE.txt".equals(request.getHeaderLocation())) {
                headerLocation = "config/maven-header.txt";
            }
        }
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("headerLocation " + headerLocation);
        }
        if (StringUtils.isNotEmpty(headerLocation)) {
            try {
                File headerFile = licenseLocator.getResourceAsFile(headerLocation, "checkstyle-header.txt");
                if (headerFile != null) {
                    p.setProperty("checkstyle.header.file", headerFile.getAbsolutePath());
                }
            } catch (FileResourceCreationException | ResourceNotFoundException e) {
                getLogger().debug("Unable to process header location: " + headerLocation);
                getLogger().debug("Checkstyle will throw exception if ${checkstyle.header.file} is used");
            }
        }
        if (request.getCacheFile() != null) {
            p.setProperty("checkstyle.cache.file", request.getCacheFile());
        }
    } catch (IOException | ResourceNotFoundException | FileResourceCreationException e) {
        throw new CheckstyleExecutorException("Failed to get overriding properties", e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    if (request.getSuppressionsFileExpression() != null) {
        String suppressionsFilePath = getSuppressionsFilePath(request);
        if (suppressionsFilePath != null) {
            p.setProperty(request.getSuppressionsFileExpression(), suppressionsFilePath);
        }
    }
    return p;
}
Also used : FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 FileResourceCreationException (org.codehaus.plexus.resource.loader.FileResourceCreationException)2 ResourceNotFoundException (org.codehaus.plexus.resource.loader.ResourceNotFoundException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)1 RuleSetReferenceId (net.sourceforge.pmd.RuleSetReferenceId)1 TextReport (net.sourceforge.pmd.benchmark.TextReport)1 DataSource (net.sourceforge.pmd.util.datasource.DataSource)1 FileDataSource (net.sourceforge.pmd.util.datasource.FileDataSource)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1