From a1d0b30968ce16caae100f4c67300e08adbff1f8 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 31 Jan 2025 14:03:39 -0800 Subject: [PATCH] Added missing prepend classpath and excludes options --- .../java/rife/bld/extension/PmdOperation.java | 127 +++++++++++++++--- .../rife/bld/extension/PmdOperationTest.java | 24 ++++ 2 files changed, 136 insertions(+), 15 deletions(-) diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index 0e5ef6f..aa0a61d 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -49,6 +49,10 @@ public class PmdOperation extends AbstractOperation { public static final String RULE_SET_DEFAULT = "rulesets/java/quickstart.xml"; private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName()); private static final String PMD_DIR = "pmd"; + /** + * The list of paths to exclude. + */ + private final List excludes_ = new ArrayList<>(); /** * The input paths (source) list. */ @@ -101,6 +105,10 @@ public class PmdOperation extends AbstractOperation { * The default language version(s). */ private Collection languageVersions_ = new ArrayList<>(); + /** + * The classpath to prepend. + */ + private String prependClasspath_; /** * The project reference. */ @@ -272,7 +280,6 @@ public class PmdOperation extends AbstractOperation { return cache(Path.of(cache)); } - /** * Sets the default language version to be used for all input files. * @@ -314,6 +321,37 @@ public class PmdOperation extends AbstractOperation { return this; } + /** + * Adds paths to exclude from the analysis. + * + * @param excludes one or more paths to exclude + * @return this operation + */ + public PmdOperation excludes(Path... excludes) { + excludes_.addAll(List.of(excludes)); + return this; + } + + /** + * Adds paths to exclude from the analysis. + * + * @param excludes paths to exclude + * @return this operation + */ + public PmdOperation excludes(Collection excludes) { + excludes_.addAll(excludes); + return this; + } + + /** + * Returns the paths to exclude from the analysis. + * + * @return the exclude paths + */ + public List excludes() { + return excludes_; + } + /** * Performs the PMD code analysis operation. */ @@ -456,8 +494,17 @@ public class PmdOperation extends AbstractOperation { * @return this operation */ public PMDConfiguration initConfiguration(String commandName) { - PMDConfiguration config = new PMDConfiguration(); + var config = new PMDConfiguration(); + // addRelativizeRoots + config.addRelativizeRoots(relativizeRoots_.stream().toList()); + + // prependAuxClasspath + if (prependClasspath_ != null) { + config.prependAuxClasspath(prependClasspath_); + } + + // setAnalysisCacheLocation if (cache_ == null && project_ != null && incrementalAnalysis_) { config.setAnalysisCacheLocation( Paths.get(project_.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-cache").toFile().getAbsolutePath()); @@ -465,38 +512,50 @@ public class PmdOperation extends AbstractOperation { config.setAnalysisCacheLocation(cache_.toFile().getAbsolutePath()); } - config.setFailOnError(failOnError_); - config.setFailOnViolation(failOnViolation_); - + // setDefaultLanguageVersions if (languageVersions_ != null) { config.setDefaultLanguageVersions(languageVersions_.stream().toList()); } + // setExcludes + if (!excludes_.isEmpty()) { + config.setExcludes(excludes_); + } + + // setFailOnError + config.setFailOnError(failOnError_); + // setFailOnViolation + config.setFailOnViolation(failOnViolation_); + + // setForceLanguageVersion if (forcedLanguageVersion_ != null) { config.setForceLanguageVersion(forcedLanguageVersion_); } + // setIgnoreFilePath if (ignoreFile_ != null) { config.setIgnoreFilePath(ignoreFile_); } + // setIgnoreIncrementalAnalysis config.setIgnoreIncrementalAnalysis(!incrementalAnalysis_); + // setInputPathList if (inputPaths_.isEmpty()) { throw new IllegalArgumentException(commandName + ": InputPaths required."); } else { config.setInputPathList(inputPaths_.stream().toList()); } - if (reportProperties_ != null) { - config.setReportProperties(reportProperties_); - } + // setInputUri if (inputUri_ != null) { config.setInputUri(inputUri_); } + // setMinimumPriority config.setMinimumPriority(rulePriority_); + // setReportFile if (project_ != null) { config.setReportFile(Objects.requireNonNullElseGet(reportFile_, () -> Paths.get(project_.buildDirectory().getPath(), @@ -505,12 +564,25 @@ public class PmdOperation extends AbstractOperation { config.setReportFile(reportFile_); } - config.addRelativizeRoots(relativizeRoots_.stream().toList()); + // setReportFormat config.setReportFormat(reportFormat_); + + // setReportProperties + if (reportProperties_ != null) { + config.setReportProperties(reportProperties_); + } + + // setRuleSets config.setRuleSets(ruleSets_.stream().toList()); + + // setShowSuppressedViolations config.setShowSuppressedViolations(showSuppressed_); + // setSourceEncoding config.setSourceEncoding(encoding_); + // setSuppressMarker config.setSuppressMarker(suppressedMarker_); + + // setThreads config.setThreads(threads_); return config; @@ -710,11 +782,36 @@ public class PmdOperation extends AbstractOperation { return numViolations; } + /** + * Prepend the specified classpath like string to the current ClassLoader of the configuration. If no ClassLoader + * is currently configured, the ClassLoader used to load the PMDConfiguration class will be used as the parent + * ClassLoader of the created ClassLoader. + *

+ * If the classpath String looks like a URL to a file (i.e. starts with {@code file://}) the file will be read with + * each line representing an entry on the classpath. + * + * @param classpath one or more classpath + * @return this operation + */ + public PmdOperation prependAuxClasspath(String... classpath) { + prependClasspath_ = String.join(File.pathSeparator, classpath); + return this; + } + + /** + * Returns the prepended classpath. + * + * @return the classpath + */ + public String prependAuxClasspath() { + return prependClasspath_; + } + /** * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot one or more relative root paths - * @return this operations + * @return this operation * @see #relativizeRoots(Collection) */ public PmdOperation relativizeRoots(Path... relativeRoot) { @@ -725,7 +822,7 @@ public class PmdOperation extends AbstractOperation { * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot one or more relative root paths - * @return this operations + * @return this operation * @see #relativizeRootsFiles(Collection) */ public PmdOperation relativizeRoots(File... relativeRoot) { @@ -736,7 +833,7 @@ public class PmdOperation extends AbstractOperation { * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot one or more relative root paths - * @return this operations + * @return this operation * @see #relativizeRootsStrings(Collection) */ public PmdOperation relativizeRoots(String... relativeRoot) { @@ -747,7 +844,7 @@ public class PmdOperation extends AbstractOperation { * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot a collection of relative root paths - * @return this operations + * @return this operation * @see #relativizeRoots(Path...) */ public PmdOperation relativizeRoots(Collection relativeRoot) { @@ -768,7 +865,7 @@ public class PmdOperation extends AbstractOperation { * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot a collection of relative root paths - * @return this operations + * @return this operation * @see #relativizeRoots(File...) */ public PmdOperation relativizeRootsFiles(Collection relativeRoot) { @@ -779,7 +876,7 @@ public class PmdOperation extends AbstractOperation { * Adds several paths to shorten paths that are output in the report. * * @param relativeRoot a collection of relative root paths - * @return this operations + * @return this operation * @see #relativizeRoots(String...) */ public PmdOperation relativizeRootsStrings(Collection relativeRoot) { diff --git a/src/test/java/rife/bld/extension/PmdOperationTest.java b/src/test/java/rife/bld/extension/PmdOperationTest.java index 08df70f..48545e5 100644 --- a/src/test/java/rife/bld/extension/PmdOperationTest.java +++ b/src/test/java/rife/bld/extension/PmdOperationTest.java @@ -166,6 +166,24 @@ class PmdOperationTest { assertThat(config.getSourceEncoding()).as("ISO_8859").isEqualTo(StandardCharsets.ISO_8859_1); } + @Test + void testExcludes() { + var foo = Path.of("foo/bar"); + var bar = Path.of("bar/foo"); + var foz = Path.of("foz/baz"); + var baz = Path.of("baz/foz"); + + var excludes = List.of(foo, bar); + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(excludes); + var config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).containsExactly(excludes.toArray(new Path[0])); + + pmd = pmd.excludes(baz, foz); + assertThat(pmd.excludes()).hasSize(4); + config = pmd.initConfiguration(COMMAND_NAME); + assertThat(config.getExcludes()).hasSize(4).contains(bar, foz); + } + @Test void testExecute() throws ExitStatusException { var pmd = new PmdOperation().fromProject(new BaseProject()); @@ -368,6 +386,12 @@ class PmdOperationTest { assertThat(pmd).isEqualTo(0); } + @Test + void testPrependAuxClasspath() { + var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).prependAuxClasspath("foo", "bar"); + assertThat(pmd.prependAuxClasspath()).isEqualTo("foo" + File.pathSeparator + "bar"); + } + @Test void testPriority() throws ExitStatusException { var pmd = newPmdOperation().inputPaths(CODE_STYLE_SAMPLE).minimumPriority(RulePriority.HIGH);