Class PmdOperation


public class PmdOperation extends AbstractOperation<PmdOperation>
Performs static code analysis with PMD.
Since:
1.0
Author:
Erik C. Thauvin
  • Field Details

    • RULE_SET_DEFAULT

      public static final String RULE_SET_DEFAULT
      The default rule set.

      Set to: rulesets/java/quickstart.xml

      See Also:
  • Constructor Details

    • PmdOperation

      public PmdOperation()
  • Method Details

    • addInputPaths

      public PmdOperation addInputPaths(Path... inputPath)
      Adds paths to source files, or directories containing source files to analyze.\
      Parameters:
      inputPath - one or more paths
      Returns:
      this operation
      See Also:
    • addInputPaths

      public PmdOperation addInputPaths(File... inputPath)
      Adds paths to source files, or directories containing source files to analyze.
      Parameters:
      inputPath - one or more paths
      Returns:
      this operation
      See Also:
    • addInputPaths

      public PmdOperation addInputPaths(String... inputPath)
      Adds paths to source files, or directories containing source files to analyze.
      Parameters:
      inputPath - one or more paths
      Returns:
      this operation
      See Also:
    • addInputPaths

      public PmdOperation addInputPaths(Collection<Path> inputPath)
      Adds paths to source files, or directories containing source files to analyze.
      Parameters:
      inputPath - a collection of input paths
      Returns:
      this operation
      See Also:
    • addInputPathsFiles

      public PmdOperation addInputPathsFiles(Collection<File> inputPath)
      Adds paths to source files, or directories containing source files to analyze.
      Parameters:
      inputPath - a collection of input paths
      Returns:
      this operation
      See Also:
    • addInputPathsStrings

      public PmdOperation addInputPathsStrings(Collection<String> inputPath)
      Adds paths to source files, or directories containing source files to analyze.
      Parameters:
      inputPath - a collection of input paths
      Returns:
      this operation
      See Also:
    • addRuleSet

      public PmdOperation addRuleSet(String... ruleSet)
      Adds new rule set paths.

      The built-in rule set paths are:

      • rulesets/java/quickstart.xml
      • category/java/bestpractices.xml
      • category/java/codestyle.xml
      • category/java/design.xml
      • category/java/documentation.xml
      • category/java/errorprone.xml
      • category/java/multithreading.xml
      • category/java/performance.xml
      • category/java/security.xml
      Parameters:
      ruleSet - one or more rule set
      Returns:
      this operation
      See Also:
    • addRuleSet

      public PmdOperation addRuleSet(Collection<String> ruleSet)
      Adds new rule set paths.

      The built-in rule set paths are:

      • rulesets/java/quickstart.xml
      • category/java/bestpractices.xml
      • category/java/codestyle.xml
      • category/java/design.xml
      • category/java/documentation.xml
      • category/java/errorprone.xml
      • category/java/multithreading.xml
      • category/java/performance.xml
      • category/java/security.xml
      Parameters:
      ruleSet - a collection of rule set paths
      Returns:
      this operation
      See Also:
    • cache

      public PmdOperation cache(Path cache)
      Sets the location of the cache file for incremental analysis.
    • cache

      public PmdOperation cache(File cache)
      Sets the location of the cache file for incremental analysis.
    • cache

      public PmdOperation cache(String cache)
      Sets the location of the cache file for incremental analysis.
    • defaultLanguageVersions

      public PmdOperation defaultLanguageVersions(LanguageVersion... languageVersion)
      Sets the default language version to be used for all input files.
      Parameters:
      languageVersion - one or more language version
      Returns:
      this operation
    • defaultLanguageVersions

      public PmdOperation defaultLanguageVersions(Collection<LanguageVersion> languageVersion)
      Sets the default language version to be used for all input files.
      Parameters:
      languageVersion - a collection language versions
      Returns:
      this operation
    • encoding

      public PmdOperation encoding(String encoding)

      Specifies the character set encoding of the source code files. The default is UTF-8.

      The valid values are the standard character sets of Charset.

    • encoding

      public PmdOperation encoding(Charset encoding)

      Specifies the character set encoding of the source code files. The default is UTF-8.

      The valid values are the standard character sets of Charset.

    • execute

      public void execute() throws Exception
      Performs the PMD code analysis operation.
      Specified by:
      execute in class AbstractOperation<PmdOperation>
      Throws:
      Exception
    • failOnError

      public PmdOperation failOnError(boolean failOnError)
      Sets whether the build will exit on recoverable errors.

      Default is: true

      Note: If only violations are found, see failOnViolation

      Parameters:
      failOnError - whether to exit and fail the build if recoverable errors occurred
      Returns:
      this operation
      See Also:
    • failOnViolation

      public PmdOperation failOnViolation(boolean failOnViolation)
      Sets whether the build will continue on violations.

      Note: If additionally recoverable errors occurred, see failOnError

      Parameters:
      failOnViolation - whether to exit and fail the build if violations are found
      Returns:
      this operation
    • forceLanguageVersion

      public PmdOperation forceLanguageVersion(LanguageVersion languageVersion)
      Forces a language to be used for all input files, irrespective of file names.
      Parameters:
      languageVersion - the language version
      Returns:
      this operation
    • fromProject

      public PmdOperation fromProject(BaseProject project)
      Configures a PMD operation from a BaseProject.

      The defaults are:

      • cache=build/pmd/pmd-cache
      • encoding=UTF-9
      • incrementAnalysis=true
      • inputPaths=[src/main, src/test]
      • reportFile=build/pmd/pmd-report-txt
      • reportFormat=text
      • rulePriority=LOW
      • ruleSets=[rulesets/java/quickstart.xml]
      • suppressedMarker=NOPMD
      Parameters:
      project - the project
      Returns:
      this operation
    • ignoreFile

      public PmdOperation ignoreFile(Path ignoreFile)
      Sets the path to the file containing a list of files to ignore, one path per line.
      Parameters:
      ignoreFile - the ignore file path
      Returns:
      this operation
    • ignoreFile

      public PmdOperation ignoreFile(File ignoreFile)
      Sets the path to the file containing a list of files to ignore, one path per line.
      Parameters:
      ignoreFile - the ignore file path
      Returns:
      this operation
    • ignoreFile

      public PmdOperation ignoreFile(String ignoreFile)
      Sets the path to the file containing a list of files to ignore, one path per line.
      Parameters:
      ignoreFile - the ignore file path
      Returns:
      this operation
    • includeLineNumber

      public PmdOperation includeLineNumber(boolean includeLineNumber)
      Enables or disables including the line number for the beginning of the violation in the analyzed source file URI.

      While clicking on the URI works in IntelliJ IDEA, Visual Studio Code, etc.; it might not in terminal emulators.

      Default: TRUE

    • incrementalAnalysis

      public PmdOperation incrementalAnalysis(boolean incrementalAnalysis)
      Enables or disables incremental analysis.
    • initConfiguration

      public PMDConfiguration initConfiguration(String commandName)
      Creates a new initialized configuration.
      Parameters:
      commandName - the command name
      Returns:
      this operation
    • inputPaths

      public PmdOperation inputPaths(Path... inputPath)
      Sets paths to source files, or directories containing source files to analyze.
      Parameters:
      inputPath - one or more paths
      Returns:
      this operation
      See Also:
    • inputPaths

      public PmdOperation inputPaths(File... inputPath)
      Sets paths to source files, or directories containing source files to analyze.

      Previous entries are disregarded.

      Parameters:
      inputPath - one or more paths
      Returns:
      this operation
      See Also:
    • inputPaths

      public PmdOperation inputPaths(String... inputPath)
      Sets paths to source files, or directories containing source files to analyze.

      Previous entries are disregarded.

      Parameters:
      inputPath - one or more paths
      Returns:
      this operation
      See Also:
    • inputPaths

      public PmdOperation inputPaths(Collection<Path> inputPath)
      Sets paths to source files, or directories containing source files to analyze.

      Previous entries are disregarded.

      Parameters:
      inputPath - a collection of input paths
      Returns:
      this operation
      See Also:
    • inputPaths

      public Collection<Path> inputPaths()
      Returns paths to source files, or directories containing source files to analyze.
      Returns:
      the input paths
    • inputPathsFiles

      public PmdOperation inputPathsFiles(Collection<File> inputPath)
      Sets paths to source files, or directories containing source files to analyze.

      Previous entries are disregarded.

      Parameters:
      inputPath - a collection of input paths
      Returns:
      this operation
      See Also:
    • inputPathsStrings

      public PmdOperation inputPathsStrings(Collection<String> inputPath)
      Sets paths to source files, or directories containing source files to analyze.

      Previous entries are disregarded.

      Parameters:
      inputPath - a collection of input paths
      Returns:
      this operation
      See Also:
    • languageVersions

      public PmdOperation languageVersions(LanguageVersion... languageVersion)
      Sets the default language versions.
      Parameters:
      languageVersion - one or more language versions
      Returns:
      this operation
    • languageVersions

      public PmdOperation languageVersions(Collection<LanguageVersion> languageVersions)
      Sets the default language versions.
      Parameters:
      languageVersions - a collection language versions
      Returns:
      this operation
    • languageVersions

      public Collection<LanguageVersion> languageVersions()
      Returns the default language versions.
      Returns:
      the language versions
    • minimumPriority

      public PmdOperation minimumPriority(RulePriority priority)
      Sets the minimum priority threshold when loading Rules from RuleSets.
      Returns:
      this operation
    • performPmdAnalysis

      public int performPmdAnalysis(String commandName, PMDConfiguration config) throws ExitStatusException
      Performs the PMD analysis with the given config.
      Parameters:
      commandName - the command name
      config - the configuration
      Returns:
      the number of violations
      Throws:
      ExitStatusException - if an error occurs
    • relativizeRoots

      public PmdOperation relativizeRoots(Path... relativeRoot)
      Adds several paths to shorten paths that are output in the report.
      Parameters:
      relativeRoot - one or more relative root paths
      Returns:
      this operations
      See Also:
    • relativizeRoots

      public PmdOperation relativizeRoots(File... relativeRoot)
      Adds several paths to shorten paths that are output in the report.
      Parameters:
      relativeRoot - one or more relative root paths
      Returns:
      this operations
      See Also:
    • relativizeRoots

      public PmdOperation relativizeRoots(String... relativeRoot)
      Adds several paths to shorten paths that are output in the report.
      Parameters:
      relativeRoot - one or more relative root paths
      Returns:
      this operations
      See Also:
    • relativizeRoots

      public PmdOperation relativizeRoots(Collection<Path> relativeRoot)
      Adds several paths to shorten paths that are output in the report.
      Parameters:
      relativeRoot - a collection of relative root paths
      Returns:
      this operations
      See Also:
    • relativizeRoots

      public Collection<Path> relativizeRoots()
      Returns paths to shorten paths that are output in the report.
      Returns:
      the relative root paths
    • relativizeRootsFiles

      public PmdOperation relativizeRootsFiles(Collection<File> relativeRoot)
      Adds several paths to shorten paths that are output in the report.
      Parameters:
      relativeRoot - a collection of relative root paths
      Returns:
      this operations
      See Also:
    • relativizeRootsStrings

      public PmdOperation relativizeRootsStrings(Collection<String> relativeRoot)
      Adds several paths to shorten paths that are output in the report.
      Parameters:
      relativeRoot - a collection of relative root paths
      Returns:
      this operations
      See Also:
    • reportFile

      public PmdOperation reportFile(Path reportFile)
      Sets the path to the report page.
      Parameters:
      reportFile - the report file path
      Returns:
      this operation
    • reportFile

      public PmdOperation reportFile(File reportFile)
      Sets the path to the report page.
      Parameters:
      reportFile - the report file path
      Returns:
      this operation
    • reportFile

      public PmdOperation reportFile(String reportFile)
      Sets the path to the report page.
      Parameters:
      reportFile - the report file path
      Returns:
      this operation
    • reportFile

      public Path reportFile()
      Returns the path to the report page.
      Returns:
      the path
    • reportFormat

      public PmdOperation reportFormat(String reportFormat)
      Sets the output format of the analysis report. The default is text.
      Parameters:
      reportFormat - the report format
      Returns:
      this operation
    • reportProperties

      public PmdOperation reportProperties(Properties reportProperties)
      Sets the Report properties. These are used to create the Renderer.
      Parameters:
      reportProperties - the report properties
      Returns:
      this operation
    • ruleSets

      public PmdOperation ruleSets(String... ruleSet)
      Sets new rule set paths, disregarding any previous entries.

      The built-in rule set paths are:

      • rulesets/java/quickstart.xml
      • category/java/bestpractices.xml
      • category/java/codestyle.xml
      • category/java/design.xml
      • category/java/documentation.xml
      • category/java/errorprone.xml
      • category/java/multithreading.xml
      • category/java/performance.xml
      • category/java/security.xml
      Parameters:
      ruleSet - one or more rule set
      Returns:
      this operation
      See Also:
    • ruleSets

      public PmdOperation ruleSets(Collection<String> ruleSet)
      Sets new rule set paths, disregarding any previous entries.

      The built-in rule set paths are:

      • rulesets/java/quickstart.xml
      • category/java/bestpractices.xml
      • category/java/codestyle.xml
      • category/java/design.xml
      • category/java/documentation.xml
      • category/java/errorprone.xml
      • category/java/multithreading.xml
      • category/java/performance.xml
      • category/java/security.xml
      Parameters:
      ruleSet - a collection of rule set paths
      Returns:
      this operation
      See Also:
    • ruleSets

      public Collection<String> ruleSets()
      Returns the rule set paths.
      Returns:
      the rule sets
    • showSuppressed

      public PmdOperation showSuppressed(boolean showSuppressed)
      Enables or disables adding the suppressed rule violations to the report.
      Parameters:
      showSuppressed - true or false
      Returns:
      this operation
    • suppressedMarker

      public PmdOperation suppressedMarker(String suppressedMarker)
      Specifies the comment token that marks lines which should be ignored. The default is NOPMD.
      Parameters:
      suppressedMarker - the suppressed marker
      Returns:
      this operation
    • threads

      public PmdOperation threads(int threads)
      Sets the number of threads to be used. The default is 1.
      Parameters:
      threads - the number of threads
      Returns:
      this operation
    • uri

      public PmdOperation uri(URI inputUri)
      Sets the input URI to process for source code objects.
      Parameters:
      inputUri - the input URI
      Returns:
      this operation