@@ -49,6 +49,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
49
49
public static final String RULE_SET_DEFAULT = "rulesets/java/quickstart.xml" ;
50
50
private static final Logger LOGGER = Logger .getLogger (PmdOperation .class .getName ());
51
51
private static final String PMD_DIR = "pmd" ;
52
+ /**
53
+ * The list of paths to exclude.
54
+ */
55
+ private final List <Path > excludes_ = new ArrayList <>();
52
56
/**
53
57
* The input paths (source) list.
54
58
*/
@@ -101,6 +105,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
101
105
* The default language version(s).
102
106
*/
103
107
private Collection <LanguageVersion > languageVersions_ = new ArrayList <>();
108
+ /**
109
+ * The classpath to prepend.
110
+ */
111
+ private String prependClasspath_ ;
104
112
/**
105
113
* The project reference.
106
114
*/
@@ -272,7 +280,6 @@ public PmdOperation cache(String cache) {
272
280
return cache (Path .of (cache ));
273
281
}
274
282
275
-
276
283
/**
277
284
* Sets the default language version to be used for all input files.
278
285
*
@@ -314,6 +321,37 @@ public PmdOperation encoding(Charset encoding) {
314
321
return this ;
315
322
}
316
323
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
+
317
355
/**
318
356
* Performs the PMD code analysis operation.
319
357
*/
@@ -456,47 +494,68 @@ public PmdOperation incrementalAnalysis(boolean incrementalAnalysis) {
456
494
* @return this operation
457
495
*/
458
496
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
+ }
460
506
507
+ // setAnalysisCacheLocation
461
508
if (cache_ == null && project_ != null && incrementalAnalysis_ ) {
462
509
config .setAnalysisCacheLocation (
463
510
Paths .get (project_ .buildDirectory ().getPath (), PMD_DIR , PMD_DIR + "-cache" ).toFile ().getAbsolutePath ());
464
511
} else if (cache_ != null ) {
465
512
config .setAnalysisCacheLocation (cache_ .toFile ().getAbsolutePath ());
466
513
}
467
514
468
- config .setFailOnError (failOnError_ );
469
- config .setFailOnViolation (failOnViolation_ );
470
-
515
+ // setDefaultLanguageVersions
471
516
if (languageVersions_ != null ) {
472
517
config .setDefaultLanguageVersions (languageVersions_ .stream ().toList ());
473
518
}
474
519
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
475
531
if (forcedLanguageVersion_ != null ) {
476
532
config .setForceLanguageVersion (forcedLanguageVersion_ );
477
533
}
478
534
535
+ // setIgnoreFilePath
479
536
if (ignoreFile_ != null ) {
480
537
config .setIgnoreFilePath (ignoreFile_ );
481
538
}
482
539
540
+ // setIgnoreIncrementalAnalysis
483
541
config .setIgnoreIncrementalAnalysis (!incrementalAnalysis_ );
484
542
543
+ // setInputPathList
485
544
if (inputPaths_ .isEmpty ()) {
486
545
throw new IllegalArgumentException (commandName + ": InputPaths required." );
487
546
} else {
488
547
config .setInputPathList (inputPaths_ .stream ().toList ());
489
548
}
490
- if (reportProperties_ != null ) {
491
- config .setReportProperties (reportProperties_ );
492
- }
493
549
550
+ // setInputUri
494
551
if (inputUri_ != null ) {
495
552
config .setInputUri (inputUri_ );
496
553
}
497
554
555
+ // setMinimumPriority
498
556
config .setMinimumPriority (rulePriority_ );
499
557
558
+ // setReportFile
500
559
if (project_ != null ) {
501
560
config .setReportFile (Objects .requireNonNullElseGet (reportFile_ ,
502
561
() -> Paths .get (project_ .buildDirectory ().getPath (),
@@ -505,12 +564,25 @@ public PMDConfiguration initConfiguration(String commandName) {
505
564
config .setReportFile (reportFile_ );
506
565
}
507
566
508
- config . addRelativizeRoots ( relativizeRoots_ . stream (). toList ());
567
+ // setReportFormat
509
568
config .setReportFormat (reportFormat_ );
569
+
570
+ // setReportProperties
571
+ if (reportProperties_ != null ) {
572
+ config .setReportProperties (reportProperties_ );
573
+ }
574
+
575
+ // setRuleSets
510
576
config .setRuleSets (ruleSets_ .stream ().toList ());
577
+
578
+ // setShowSuppressedViolations
511
579
config .setShowSuppressedViolations (showSuppressed_ );
580
+ // setSourceEncoding
512
581
config .setSourceEncoding (encoding_ );
582
+ // setSuppressMarker
513
583
config .setSuppressMarker (suppressedMarker_ );
584
+
585
+ // setThreads
514
586
config .setThreads (threads_ );
515
587
516
588
return config ;
@@ -710,11 +782,36 @@ public int performPmdAnalysis(String commandName, PMDConfiguration config) throw
710
782
return numViolations ;
711
783
}
712
784
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
+
713
810
/**
714
811
* Adds several paths to shorten paths that are output in the report.
715
812
*
716
813
* @param relativeRoot one or more relative root paths
717
- * @return this operations
814
+ * @return this operation
718
815
* @see #relativizeRoots(Collection)
719
816
*/
720
817
public PmdOperation relativizeRoots (Path ... relativeRoot ) {
@@ -725,7 +822,7 @@ public PmdOperation relativizeRoots(Path... relativeRoot) {
725
822
* Adds several paths to shorten paths that are output in the report.
726
823
*
727
824
* @param relativeRoot one or more relative root paths
728
- * @return this operations
825
+ * @return this operation
729
826
* @see #relativizeRootsFiles(Collection)
730
827
*/
731
828
public PmdOperation relativizeRoots (File ... relativeRoot ) {
@@ -736,7 +833,7 @@ public PmdOperation relativizeRoots(File... relativeRoot) {
736
833
* Adds several paths to shorten paths that are output in the report.
737
834
*
738
835
* @param relativeRoot one or more relative root paths
739
- * @return this operations
836
+ * @return this operation
740
837
* @see #relativizeRootsStrings(Collection)
741
838
*/
742
839
public PmdOperation relativizeRoots (String ... relativeRoot ) {
@@ -747,7 +844,7 @@ public PmdOperation relativizeRoots(String... relativeRoot) {
747
844
* Adds several paths to shorten paths that are output in the report.
748
845
*
749
846
* @param relativeRoot a collection of relative root paths
750
- * @return this operations
847
+ * @return this operation
751
848
* @see #relativizeRoots(Path...)
752
849
*/
753
850
public PmdOperation relativizeRoots (Collection <Path > relativeRoot ) {
@@ -768,7 +865,7 @@ public Collection<Path> relativizeRoots() {
768
865
* Adds several paths to shorten paths that are output in the report.
769
866
*
770
867
* @param relativeRoot a collection of relative root paths
771
- * @return this operations
868
+ * @return this operation
772
869
* @see #relativizeRoots(File...)
773
870
*/
774
871
public PmdOperation relativizeRootsFiles (Collection <File > relativeRoot ) {
@@ -779,7 +876,7 @@ public PmdOperation relativizeRootsFiles(Collection<File> relativeRoot) {
779
876
* Adds several paths to shorten paths that are output in the report.
780
877
*
781
878
* @param relativeRoot a collection of relative root paths
782
- * @return this operations
879
+ * @return this operation
783
880
* @see #relativizeRoots(String...)
784
881
*/
785
882
public PmdOperation relativizeRootsStrings (Collection <String > relativeRoot ) {
0 commit comments