Skip to content

Commit a1d0b30

Browse files
committedJan 31, 2025
Added missing prepend classpath and excludes options
1 parent bc60dcb commit a1d0b30

File tree

2 files changed

+136
-15
lines changed

2 files changed

+136
-15
lines changed
 

‎src/main/java/rife/bld/extension/PmdOperation.java

+112-15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
4949
public static final String RULE_SET_DEFAULT = "rulesets/java/quickstart.xml";
5050
private static final Logger LOGGER = Logger.getLogger(PmdOperation.class.getName());
5151
private static final String PMD_DIR = "pmd";
52+
/**
53+
* The list of paths to exclude.
54+
*/
55+
private final List<Path> excludes_ = new ArrayList<>();
5256
/**
5357
* The input paths (source) list.
5458
*/
@@ -101,6 +105,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
101105
* The default language version(s).
102106
*/
103107
private Collection<LanguageVersion> languageVersions_ = new ArrayList<>();
108+
/**
109+
* The classpath to prepend.
110+
*/
111+
private String prependClasspath_;
104112
/**
105113
* The project reference.
106114
*/
@@ -272,7 +280,6 @@ public PmdOperation cache(String cache) {
272280
return cache(Path.of(cache));
273281
}
274282

275-
276283
/**
277284
* Sets the default language version to be used for all input files.
278285
*
@@ -314,6 +321,37 @@ public PmdOperation encoding(Charset encoding) {
314321
return this;
315322
}
316323

324+
/**
325+
* Adds paths to exclude from the analysis.
326+
*
327+
* @param excludes one or more paths to exclude
328+
* @return this operation
329+
*/
330+
public PmdOperation excludes(Path... excludes) {
331+
excludes_.addAll(List.of(excludes));
332+
return this;
333+
}
334+
335+
/**
336+
* Adds paths to exclude from the analysis.
337+
*
338+
* @param excludes paths to exclude
339+
* @return this operation
340+
*/
341+
public PmdOperation excludes(Collection<Path> excludes) {
342+
excludes_.addAll(excludes);
343+
return this;
344+
}
345+
346+
/**
347+
* Returns the paths to exclude from the analysis.
348+
*
349+
* @return the exclude paths
350+
*/
351+
public List<Path> excludes() {
352+
return excludes_;
353+
}
354+
317355
/**
318356
* Performs the PMD code analysis operation.
319357
*/
@@ -456,47 +494,68 @@ public PmdOperation incrementalAnalysis(boolean incrementalAnalysis) {
456494
* @return this operation
457495
*/
458496
public PMDConfiguration initConfiguration(String commandName) {
459-
PMDConfiguration config = new PMDConfiguration();
497+
var config = new PMDConfiguration();
498+
499+
// addRelativizeRoots
500+
config.addRelativizeRoots(relativizeRoots_.stream().toList());
501+
502+
// prependAuxClasspath
503+
if (prependClasspath_ != null) {
504+
config.prependAuxClasspath(prependClasspath_);
505+
}
460506

507+
// setAnalysisCacheLocation
461508
if (cache_ == null && project_ != null && incrementalAnalysis_) {
462509
config.setAnalysisCacheLocation(
463510
Paths.get(project_.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-cache").toFile().getAbsolutePath());
464511
} else if (cache_ != null) {
465512
config.setAnalysisCacheLocation(cache_.toFile().getAbsolutePath());
466513
}
467514

468-
config.setFailOnError(failOnError_);
469-
config.setFailOnViolation(failOnViolation_);
470-
515+
// setDefaultLanguageVersions
471516
if (languageVersions_ != null) {
472517
config.setDefaultLanguageVersions(languageVersions_.stream().toList());
473518
}
474519

520+
// setExcludes
521+
if (!excludes_.isEmpty()) {
522+
config.setExcludes(excludes_);
523+
}
524+
525+
// setFailOnError
526+
config.setFailOnError(failOnError_);
527+
// setFailOnViolation
528+
config.setFailOnViolation(failOnViolation_);
529+
530+
// setForceLanguageVersion
475531
if (forcedLanguageVersion_ != null) {
476532
config.setForceLanguageVersion(forcedLanguageVersion_);
477533
}
478534

535+
// setIgnoreFilePath
479536
if (ignoreFile_ != null) {
480537
config.setIgnoreFilePath(ignoreFile_);
481538
}
482539

540+
// setIgnoreIncrementalAnalysis
483541
config.setIgnoreIncrementalAnalysis(!incrementalAnalysis_);
484542

543+
// setInputPathList
485544
if (inputPaths_.isEmpty()) {
486545
throw new IllegalArgumentException(commandName + ": InputPaths required.");
487546
} else {
488547
config.setInputPathList(inputPaths_.stream().toList());
489548
}
490-
if (reportProperties_ != null) {
491-
config.setReportProperties(reportProperties_);
492-
}
493549

550+
// setInputUri
494551
if (inputUri_ != null) {
495552
config.setInputUri(inputUri_);
496553
}
497554

555+
// setMinimumPriority
498556
config.setMinimumPriority(rulePriority_);
499557

558+
// setReportFile
500559
if (project_ != null) {
501560
config.setReportFile(Objects.requireNonNullElseGet(reportFile_,
502561
() -> Paths.get(project_.buildDirectory().getPath(),
@@ -505,12 +564,25 @@ public PMDConfiguration initConfiguration(String commandName) {
505564
config.setReportFile(reportFile_);
506565
}
507566

508-
config.addRelativizeRoots(relativizeRoots_.stream().toList());
567+
// setReportFormat
509568
config.setReportFormat(reportFormat_);
569+
570+
// setReportProperties
571+
if (reportProperties_ != null) {
572+
config.setReportProperties(reportProperties_);
573+
}
574+
575+
// setRuleSets
510576
config.setRuleSets(ruleSets_.stream().toList());
577+
578+
// setShowSuppressedViolations
511579
config.setShowSuppressedViolations(showSuppressed_);
580+
// setSourceEncoding
512581
config.setSourceEncoding(encoding_);
582+
// setSuppressMarker
513583
config.setSuppressMarker(suppressedMarker_);
584+
585+
// setThreads
514586
config.setThreads(threads_);
515587

516588
return config;
@@ -710,11 +782,36 @@ public int performPmdAnalysis(String commandName, PMDConfiguration config) throw
710782
return numViolations;
711783
}
712784

785+
/**
786+
* Prepend the specified classpath like string to the current ClassLoader of the configuration. If no ClassLoader
787+
* is currently configured, the ClassLoader used to load the PMDConfiguration class will be used as the parent
788+
* ClassLoader of the created ClassLoader.
789+
* <p>
790+
* If the classpath String looks like a URL to a file (i.e. starts with {@code file://}) the file will be read with
791+
* each line representing an entry on the classpath.
792+
*
793+
* @param classpath one or more classpath
794+
* @return this operation
795+
*/
796+
public PmdOperation prependAuxClasspath(String... classpath) {
797+
prependClasspath_ = String.join(File.pathSeparator, classpath);
798+
return this;
799+
}
800+
801+
/**
802+
* Returns the prepended classpath.
803+
*
804+
* @return the classpath
805+
*/
806+
public String prependAuxClasspath() {
807+
return prependClasspath_;
808+
}
809+
713810
/**
714811
* Adds several paths to shorten paths that are output in the report.
715812
*
716813
* @param relativeRoot one or more relative root paths
717-
* @return this operations
814+
* @return this operation
718815
* @see #relativizeRoots(Collection)
719816
*/
720817
public PmdOperation relativizeRoots(Path... relativeRoot) {
@@ -725,7 +822,7 @@ public PmdOperation relativizeRoots(Path... relativeRoot) {
725822
* Adds several paths to shorten paths that are output in the report.
726823
*
727824
* @param relativeRoot one or more relative root paths
728-
* @return this operations
825+
* @return this operation
729826
* @see #relativizeRootsFiles(Collection)
730827
*/
731828
public PmdOperation relativizeRoots(File... relativeRoot) {
@@ -736,7 +833,7 @@ public PmdOperation relativizeRoots(File... relativeRoot) {
736833
* Adds several paths to shorten paths that are output in the report.
737834
*
738835
* @param relativeRoot one or more relative root paths
739-
* @return this operations
836+
* @return this operation
740837
* @see #relativizeRootsStrings(Collection)
741838
*/
742839
public PmdOperation relativizeRoots(String... relativeRoot) {
@@ -747,7 +844,7 @@ public PmdOperation relativizeRoots(String... relativeRoot) {
747844
* Adds several paths to shorten paths that are output in the report.
748845
*
749846
* @param relativeRoot a collection of relative root paths
750-
* @return this operations
847+
* @return this operation
751848
* @see #relativizeRoots(Path...)
752849
*/
753850
public PmdOperation relativizeRoots(Collection<Path> relativeRoot) {
@@ -768,7 +865,7 @@ public Collection<Path> relativizeRoots() {
768865
* Adds several paths to shorten paths that are output in the report.
769866
*
770867
* @param relativeRoot a collection of relative root paths
771-
* @return this operations
868+
* @return this operation
772869
* @see #relativizeRoots(File...)
773870
*/
774871
public PmdOperation relativizeRootsFiles(Collection<File> relativeRoot) {
@@ -779,7 +876,7 @@ public PmdOperation relativizeRootsFiles(Collection<File> relativeRoot) {
779876
* Adds several paths to shorten paths that are output in the report.
780877
*
781878
* @param relativeRoot a collection of relative root paths
782-
* @return this operations
879+
* @return this operation
783880
* @see #relativizeRoots(String...)
784881
*/
785882
public PmdOperation relativizeRootsStrings(Collection<String> relativeRoot) {

‎src/test/java/rife/bld/extension/PmdOperationTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ void testEncoding() {
166166
assertThat(config.getSourceEncoding()).as("ISO_8859").isEqualTo(StandardCharsets.ISO_8859_1);
167167
}
168168

169+
@Test
170+
void testExcludes() {
171+
var foo = Path.of("foo/bar");
172+
var bar = Path.of("bar/foo");
173+
var foz = Path.of("foz/baz");
174+
var baz = Path.of("baz/foz");
175+
176+
var excludes = List.of(foo, bar);
177+
var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).excludes(excludes);
178+
var config = pmd.initConfiguration(COMMAND_NAME);
179+
assertThat(config.getExcludes()).containsExactly(excludes.toArray(new Path[0]));
180+
181+
pmd = pmd.excludes(baz, foz);
182+
assertThat(pmd.excludes()).hasSize(4);
183+
config = pmd.initConfiguration(COMMAND_NAME);
184+
assertThat(config.getExcludes()).hasSize(4).contains(bar, foz);
185+
}
186+
169187
@Test
170188
void testExecute() throws ExitStatusException {
171189
var pmd = new PmdOperation().fromProject(new BaseProject());
@@ -368,6 +386,12 @@ void testMainOperation() throws ExitStatusException {
368386
assertThat(pmd).isEqualTo(0);
369387
}
370388

389+
@Test
390+
void testPrependAuxClasspath() {
391+
var pmd = newPmdOperation().ruleSets(CATEGORY_FOO).prependAuxClasspath("foo", "bar");
392+
assertThat(pmd.prependAuxClasspath()).isEqualTo("foo" + File.pathSeparator + "bar");
393+
}
394+
371395
@Test
372396
void testPriority() throws ExitStatusException {
373397
var pmd = newPmdOperation().inputPaths(CODE_STYLE_SAMPLE).minimumPriority(RulePriority.HIGH);

0 commit comments

Comments
 (0)