* Set to: {@code rulesets/java/quickstart.xml}
*/
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 input paths (source) list.
*/
private final Collection
* The built-in rule set paths are:
*
* The built-in rule set paths are:
* Specifies the character set encoding of the source code files. The default is {@code UTF-8}. The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}. Specifies the character set encoding of the source code files. The default is
* {@link StandardCharsets#UTF_8 UTF-8}. The valid values are the standard character sets of {@link java.nio.charset.Charset Charset}.
* Default is: {@code true}
*
* Note: If only violations are found, see {@link #failOnViolation(boolean) failOnViolation}
*
* @param failOnError whether to exit and fail the build if recoverable errors occurred
* @return this operation
* @see #failOnViolation(boolean)
*/
public PmdOperation failOnError(boolean failOnError) {
failOnError_ = failOnError;
return this;
}
/**
* Sets whether the build will continue on violations.
*
* Note: If additionally recoverable errors occurred, see {@link #failOnError(boolean) failOnError}
*
* @param failOnViolation whether to exit and fail the build if violations are found
* @return this operation
*/
public PmdOperation failOnViolation(boolean failOnViolation) {
failOnViolation_ = failOnViolation;
return this;
}
/**
* Forces a language to be used for all input files, irrespective of file names.
*
* @param languageVersion the language version
* @return this operation
*/
public PmdOperation forceLanguageVersion(LanguageVersion languageVersion) {
forcedLanguageVersion_ = languageVersion;
return this;
}
/**
* Configures a PMD operation from a {@link BaseProject}.
*
*
* The defaults are:
*
* While clicking on the URI works in IntelliJ IDEA, Visual Studio Code, etc.; it might not in terminal emulators.
*
* Default: {@code TRUE}
*/
public PmdOperation includeLineNumber(boolean includeLineNumber) {
includeLineNumber_ = includeLineNumber;
return this;
}
/**
* Enables or disables incremental analysis.
*/
public PmdOperation incrementalAnalysis(boolean incrementalAnalysis) {
incrementalAnalysis_ = incrementalAnalysis;
return this;
}
/**
* Creates a new initialized configuration.
*
* @param commandName the command name
* @return this operation
*/
public PMDConfiguration initConfiguration(String commandName) {
PMDConfiguration config = new PMDConfiguration();
if (cache_ == null && project_ != null && incrementalAnalysis_) {
config.setAnalysisCacheLocation(
Paths.get(project_.buildDirectory().getPath(), PMD_DIR, PMD_DIR + "-cache").toFile().getAbsolutePath());
} else if (cache_ != null) {
config.setAnalysisCacheLocation(cache_.toFile().getAbsolutePath());
}
config.setFailOnError(failOnError_);
config.setFailOnViolation(failOnViolation_);
if (languageVersions_ != null) {
config.setDefaultLanguageVersions(languageVersions_.stream().toList());
}
if (forcedLanguageVersion_ != null) {
config.setForceLanguageVersion(forcedLanguageVersion_);
}
if (ignoreFile_ != null) {
config.setIgnoreFilePath(ignoreFile_);
}
config.setIgnoreIncrementalAnalysis(!incrementalAnalysis_);
if (inputPaths_.isEmpty()) {
throw new IllegalArgumentException(commandName + ": InputPaths required.");
} else {
config.setInputPathList(inputPaths_.stream().toList());
}
if (reportProperties_ != null) {
config.setReportProperties(reportProperties_);
}
if (inputUri_ != null) {
config.setInputUri(inputUri_);
}
config.setMinimumPriority(rulePriority_);
if (project_ != null) {
config.setReportFile(Objects.requireNonNullElseGet(reportFile_,
() -> Paths.get(project_.buildDirectory().getPath(),
PMD_DIR, PMD_DIR + "-report." + reportFormat_)));
} else {
config.setReportFile(reportFile_);
}
config.addRelativizeRoots(relativizeRoots_.stream().toList());
config.setReportFormat(reportFormat_);
config.setRuleSets(ruleSets_.stream().toList());
config.setShowSuppressedViolations(showSuppressed_);
config.setSourceEncoding(encoding_);
config.setSuppressMarker(suppressedMarker_);
config.setThreads(threads_);
return config;
}
/**
* Sets paths to source files, or directories containing source files to analyze.
*
* @param inputPath one or more paths
* @return this operation
* @see #addInputPaths(Path...)
*/
public PmdOperation inputPaths(Path... inputPath) {
return inputPaths(List.of(inputPath));
}
/**
* Sets paths to source files, or directories containing source files to analyze.
*
* Previous entries are disregarded.
*
* @param inputPath one or more paths
* @return this operation
* @see #addInputPaths(File...)
*/
public PmdOperation inputPaths(File... inputPath) {
return inputPathsFiles(List.of(inputPath));
}
/**
* Sets paths to source files, or directories containing source files to analyze.
*
* Previous entries are disregarded.
*
* @param inputPath one or more paths
* @return this operation
* @see #addInputPaths(String...)
*/
public PmdOperation inputPaths(String... inputPath) {
return inputPathsStrings(List.of(inputPath));
}
/**
* Sets paths to source files, or directories containing source files to analyze.
*
* Previous entries are disregarded.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #addInputPaths(Path...)
*/
public PmdOperation inputPaths(Collection
* Previous entries are disregarded.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #addInputPaths(File...)
*/
public PmdOperation inputPathsFiles(Collection
* Previous entries are disregarded.
*
* @param inputPath a collection of input paths
* @return this operation
* @see #addInputPaths(String...)
*/
public PmdOperation inputPathsStrings(Collection
* The built-in rule set paths are:
*
* The built-in rule set paths are:
*
*
*
* @param ruleSet one or more rule set
* @return this operation
* @see #ruleSets(String...)
*/
public PmdOperation addRuleSet(String... ruleSet) {
return addRuleSet(List.of(ruleSet));
}
/**
* Adds new rule set paths.
*
*
*
* @param ruleSet a collection of rule set paths
* @return this operation
* @see #ruleSets(Collection)
*/
public PmdOperation addRuleSet(Collection
*
*
* @param project the project
* @return this operation
*/
public PmdOperation fromProject(BaseProject project) {
project_ = project;
inputPaths(project.srcMainDirectory(), project.srcTestDirectory());
ruleSets_.add(RULE_SET_DEFAULT);
return this;
}
/**
* Sets the path to the file containing a list of files to ignore, one path per line.
*
* @param ignoreFile the ignore file path
* @return this operation
*/
public PmdOperation ignoreFile(Path ignoreFile) {
ignoreFile_ = ignoreFile;
return this;
}
/**
* Sets the path to the file containing a list of files to ignore, one path per line.
*
* @param ignoreFile the ignore file path
* @return this operation
*/
public PmdOperation ignoreFile(File ignoreFile) {
return ignoreFile(ignoreFile.toPath());
}
/**
* Sets the path to the file containing a list of files to ignore, one path per line.
*
* @param ignoreFile the ignore file path
* @return this operation
*/
public PmdOperation ignoreFile(String ignoreFile) {
return ignoreFile(Path.of(ignoreFile));
}
/**
* Enables or disables including the line number for the beginning of the violation in the analyzed source file URI.
*
*
*
* @param ruleSet one or more rule set
* @return this operation
* @see #addRuleSet(String...)
*/
public PmdOperation ruleSets(String... ruleSet) {
ruleSets_.clear();
ruleSets_.addAll(List.of(ruleSet));
return this;
}
/**
* Sets new rule set paths, disregarding any previous entries.
*
*
*
* @param ruleSet a collection of rule set paths
* @return this operation
* @see #addRuleSet(Collection)
*/
public PmdOperation ruleSets(Collection