use of net.sourceforge.pmd.PMDConfiguration 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);
}
}
}
use of net.sourceforge.pmd.PMDConfiguration in project maven-plugins by apache.
the class PmdReport method getPMDConfiguration.
/**
* Constructs the PMD configuration class, passing it an argument that configures the target JDK.
*
* @return the resulting PMD
* @throws org.apache.maven.reporting.MavenReportException if targetJdk is not supported
*/
public PMDConfiguration getPMDConfiguration() throws MavenReportException {
PMDConfiguration configuration = new PMDConfiguration();
LanguageVersion languageVersion = null;
if (("java".equals(language) || null == language) && null != targetJdk) {
languageVersion = LanguageRegistry.findLanguageVersionByTerseName("java " + targetJdk);
if (languageVersion == null) {
throw new MavenReportException("Unsupported targetJdk value '" + targetJdk + "'.");
}
} else if ("javascript".equals(language) || "ecmascript".equals(language)) {
languageVersion = LanguageRegistry.findLanguageVersionByTerseName("ecmascript");
} else if ("jsp".equals(language)) {
languageVersion = LanguageRegistry.findLanguageVersionByTerseName("jsp");
}
if (languageVersion != null) {
getLog().debug("Using language " + languageVersion);
configuration.setDefaultLanguageVersion(languageVersion);
}
if (typeResolution) {
try {
@SuppressWarnings("unchecked") List<String> classpath = includeTests ? project.getTestClasspathElements() : project.getCompileClasspathElements();
getLog().debug("Using aux classpath: " + classpath);
configuration.prependClasspath(StringUtils.join(classpath.iterator(), File.pathSeparator));
} catch (Exception e) {
throw new MavenReportException(e.getMessage(), e);
}
}
if (null != suppressMarker) {
configuration.setSuppressMarker(suppressMarker);
}
configuration.setBenchmark(benchmark);
if (analysisCache) {
configuration.setAnalysisCacheLocation(analysisCacheLocation);
getLog().debug("Using analysis cache location: " + analysisCacheLocation);
}
return configuration;
}
Aggregations