Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPMD-379] Upgrade to use PMD 7.0.0 by default #144

Merged
merged 13 commits into from Apr 18, 2024
10 changes: 2 additions & 8 deletions pom.xml
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-pmd-plugin</artifactId>
<version>3.21.3-SNAPSHOT</version>
<version>3.22.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven PMD Plugin</name>
Expand Down Expand Up @@ -83,7 +83,7 @@ under the License.
<properties>
<mavenVersion>3.2.5</mavenVersion>
<javaVersion>8</javaVersion>
<pmdVersion>6.55.0</pmdVersion>
<pmdVersion>7.0.0</pmdVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion>
<doxiaVersion>1.12.0</doxiaVersion>
Expand Down Expand Up @@ -187,12 +187,6 @@ under the License.
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<!-- Support logging of PMD through slf4j. See also MPMD-244 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4jVersion}</version>
</dependency>

<!-- doxia -->
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/it/MPMD-219-pmd-processing-error/verify.groovy
Expand Up @@ -20,5 +20,5 @@
File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "PMD processing errors" )
assert buildLog.text.contains( "Error while parsing" )
assert buildLog.text.contains( "ParseException: Parse exception in file" )
assert buildLog.text.contains( "BrokenFile.java" )
2 changes: 1 addition & 1 deletion src/it/MPMD-243-excludeFromFailureFile/config/ruleset.xml
Expand Up @@ -26,5 +26,5 @@ under the License.
MPMD-243
</description>

<rule ref="rulesets/java/imports.xml/TooManyStaticImports"/>
<rule ref="category/java/codestyle.xml/TooManyStaticImports"/>
</ruleset>
61 changes: 0 additions & 61 deletions src/it/MPMD-244-logging/logging-disabled/pom.xml

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/it/MPMD-244-logging/logging-enabled/pom.xml
Expand Up @@ -45,7 +45,6 @@ under the License.
<version>@project.version@</version>
<configuration>
<skipPmdError>true</skipPmdError>
<showPmdLog>true</showPmdLog>
</configuration>
<executions>
<execution>
Expand Down
1 change: 0 additions & 1 deletion src/it/MPMD-244-logging/pom.xml
Expand Up @@ -36,7 +36,6 @@ under the License.
</properties>

<modules>
<module>logging-disabled</module>
<module>logging-enabled</module>
</modules>
</project>
15 changes: 9 additions & 6 deletions src/it/MPMD-244-logging/verify.groovy
Expand Up @@ -20,12 +20,15 @@
File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "PMD processing errors" )
assert buildLog.text.contains( "Error while parsing" )
assert buildLog.text.contains( "ParseException: Parse exception" )
assert buildLog.text.contains( "at line 24, column 5: Encountered" )

String disabledPath = new File( basedir, 'logging-disabled/src/main/java/BrokenFile.java' ).getCanonicalPath()
String enabledPath = new File( basedir, 'logging-enabled/src/main/java/BrokenFile.java' ).getCanonicalPath()

// logging disabled: the pmd exception is only output through the processing error reporting (since MPMD-246)
assert 1 == buildLog.text.count( "net.sourceforge.pmd.PMDException: Error while parsing ${disabledPath}" )
// logging enabled: the pmd exception is output twice: through the processing error reporting (since MPMD-246) and through PMD's own logging
assert 2 == buildLog.text.count( "net.sourceforge.pmd.PMDException: Error while parsing ${enabledPath}" )
// logging enabled: the pmd exception is still output only once: through the processing error reporting (since MPMD-246) - PMD 7 doesn't log the processing error additionally
assert 1 == buildLog.text.count( "${enabledPath}: ParseException: Parse exception in" )

// build.log contains the logging from the two PMD executions
// only one execution has logging enabled, so we expect only one log output
assert 1 == buildLog.text.count( "[DEBUG] Rules loaded from" )
// with --debug switch or -X the logging is always enabled and can't be disabled , because PMD 7 switched to slf4j
1 change: 1 addition & 0 deletions src/it/MPMD-258-multiple-executions/invoker.properties
Expand Up @@ -15,5 +15,6 @@
# specific language governing permissions and limitations
# under the License.

invoker.debug = true
mkolesnikov marked this conversation as resolved.
Show resolved Hide resolved
invoker.goals = clean compile pmd:pmd
invoker.maven.version = 3.1.0+
2 changes: 2 additions & 0 deletions src/it/MPMD-258-multiple-executions/verify.groovy
Expand Up @@ -21,7 +21,9 @@ File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

// we have 2 modules and for each module this should be output once
// note: this is only logged in debug mode by net.sourceforge.pmd.cache.FileAnalysisCache
assert 2 == buildLog.text.count( "Analysis cache created" )

// since we are running clean pmd:pmd, the cache is always created, never updated
// note: this is only logged in debug mode by net.sourceforge.pmd.cache.FileAnalysisCache
assert 0 == buildLog.text.count( "Analysis cache updated" )
2 changes: 1 addition & 1 deletion src/it/MPMD-268-deprecated-rules/verify.groovy
Expand Up @@ -19,4 +19,4 @@

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "[WARNING] Use Rule name category/java/codestyle.xml/ControlStatementBraces instead of the deprecated" )
assert buildLog.text.contains( "Use Rule name category/java/codestyle.xml/ControlStatementBraces instead of the deprecated Rule name" )
4 changes: 2 additions & 2 deletions src/it/MPMD-304-toolchain-support/verify.groovy
Expand Up @@ -29,8 +29,8 @@ assert buildLog.text.contains( '[INFO] You have 1 CPD duplication' )

File pmdReport = new File( basedir, 'target/pmd.xml' )
assert pmdReport.exists()
assert pmdReport.text.contains( '<violation beginline="24" endline="24" begincolumn="29" endcolumn="34" rule="ExtendsObject"' )
assert pmdReport.text.contains( '<violation beginline="36" endline="36" begincolumn="9" endcolumn="31" rule="DontCallThreadRun"' )
assert pmdReport.text.contains( '<violation beginline="24" endline="24" begincolumn="29" endcolumn="35" rule="ExtendsObject"' )
assert pmdReport.text.contains( '<violation beginline="36" endline="36" begincolumn="9" endcolumn="32" rule="DontCallThreadRun"' )

File pmdSite = new File( basedir, 'target/site/pmd.html' )
assert pmdSite.exists()
Expand Down
2 changes: 1 addition & 1 deletion src/it/MPMD-89-232-typeresolution/src/main/pmd/ruleset.xml
Expand Up @@ -24,5 +24,5 @@ under the License.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>Custom Ruleset for test case MPMD-89 and MPMP-232</description>
<rule ref="rulesets/java/junit.xml/TestClassWithoutTestCases"/>
<rule ref="category/java/errorprone.xml/TestClassWithoutTestCases"/>
</ruleset>
4 changes: 2 additions & 2 deletions src/it/mpmd-138/verify.groovy
Expand Up @@ -23,7 +23,7 @@ assert buildLog.exists()

// Module 1
assert 1 == buildLog.getText().count('[INFO] PMD Failure: test.MyClass:27 Rule:UnnecessarySemicolon Priority:3 Unnecessary semicolon')
assert 1 == buildLog.getText().count('[INFO] PMD Failure: test.MyClass:28 Rule:UnnecessaryReturn Priority:3 Avoid unnecessary return statements')
assert 1 == buildLog.getText().count('[INFO] PMD Failure: test.MyClass:28 Rule:UnnecessaryReturn Priority:3 Unnecessary return statement')
assert 1 == buildLog.getText().count('[INFO] You have 2 PMD violations. For more details see:')

// Module 2
Expand All @@ -34,4 +34,4 @@ assert 1 == buildLog.getText().count('[INFO] You have 1 PMD violation. For more
assert 1 == buildLog.getText().count('[INFO] You have 1 CPD duplication. For more details see:')

// Module 4
assert 1 == buildLog.getText().count('[INFO] You have 2 CPD duplications. For more details see:')
assert 1 == buildLog.getText().count('[INFO] You have 2 CPD duplications. For more details see:')
2 changes: 1 addition & 1 deletion src/it/multi-module/mod-1/src/main/config/pmd/utf-8.xml
Expand Up @@ -23,5 +23,5 @@ under the License.
<description>
This ruleset is encoded with UTF-8 to check proper encoding handling.
</description>
<rule ref="category/java/errorprone.xml/EmptyStatementNotInLoop" message="UTF-8-CHARS: ÄÖÜäöüß¼½¾¤"/>
<rule ref="category/java/codestyle.xml/UnnecessarySemicolon" message="UTF-8-CHARS: ÄÖÜäöüß¼½¾¤"/>
</ruleset>
2 changes: 1 addition & 1 deletion src/it/multi-module/mod-2/rulesets/java/basic.xml
Expand Up @@ -23,5 +23,5 @@ under the License.
<description>
The relative path of this ruleset matches the built-in ruleset "basic".
</description>
<rule ref="category/java/errorprone.xml/EmptyStatementNotInLoop" message="TEST: LOCAL-FILE-RULESET"/>
<rule ref="category/java/codestyle.xml/UnnecessarySemicolon" message="TEST: LOCAL-FILE-RULESET"/>
</ruleset>
2 changes: 1 addition & 1 deletion src/it/multi-module/mod-3/src/main/config/pmd/rel.xml
Expand Up @@ -23,5 +23,5 @@ under the License.
<description>
This ruleset is specified via a relative filesystem path.
</description>
<rule ref="category/java/errorprone.xml/EmptyStatementNotInLoop" message="TEST: RELATIVE-PATH"/>
<rule ref="category/java/codestyle.xml/UnnecessarySemicolon" message="TEST: RELATIVE-PATH"/>
</ruleset>
20 changes: 5 additions & 15 deletions src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
Expand Up @@ -21,10 +21,8 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Properties;

import net.sourceforge.pmd.cpd.JavaTokenizer;
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
import net.sourceforge.pmd.cpd.CPDReportRenderer;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -177,23 +175,15 @@ private void executeCpd() throws MavenReportException {
return;
}

Properties languageProperties = new Properties();
if (ignoreLiterals) {
languageProperties.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
}
if (ignoreIdentifiers) {
languageProperties.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
}
if (ignoreAnnotations) {
languageProperties.setProperty(JavaTokenizer.IGNORE_ANNOTATIONS, "true");
}
try {
filesToProcess = getFilesToProcess();

CpdRequest request = new CpdRequest();
request.setMinimumTokens(minimumTokens);
request.setLanguage(language);
request.setLanguageProperties(languageProperties);
request.setIgnoreAnnotations(ignoreAnnotations);
request.setIgnoreIdentifiers(ignoreIdentifiers);
request.setIgnoreLiterals(ignoreLiterals);
request.setSourceEncoding(getInputEncoding());
request.addFiles(filesToProcess.keySet());

Expand Down Expand Up @@ -238,7 +228,7 @@ public String getOutputName() {
* @deprecated Use {@link CpdExecutor#createRenderer(String, String)} instead.
*/
@Deprecated
public CPDRenderer createRenderer() throws MavenReportException {
public CPDReportRenderer createRenderer() throws MavenReportException {
adangel marked this conversation as resolved.
Show resolved Hide resolved
return CpdExecutor.createRenderer(format, getOutputEncoding());
}
}
Expand Up @@ -64,7 +64,7 @@ public boolean isExcludedFromFailure(final Duplication errorDetail) {
public boolean isExcludedFromFailure(final Match errorDetail) {
final Set<String> uniquePaths = new HashSet<>();
for (Mark mark : errorDetail.getMarkSet()) {
uniquePaths.add(mark.getFilename());
uniquePaths.add(mark.getLocation().getFileId().getAbsolutePath());
}
return isExcludedFromFailure(uniquePaths);
}
Expand Down
Expand Up @@ -28,7 +28,7 @@
import java.util.Properties;
import java.util.Set;

import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.reporting.RuleViolation;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.pmd.model.Violation;

Expand Down Expand Up @@ -84,8 +84,11 @@ public boolean isExcludedFromFailure(final Violation errorDetail) {
* @return <code>true</code> if the violation should be excluded, <code>false</code> otherwise.
*/
public boolean isExcludedFromFailure(final RuleViolation errorDetail) {
final String className =
extractClassName(errorDetail.getPackageName(), errorDetail.getClassName(), errorDetail.getFilename());
final Map<String, String> additionalInfo = errorDetail.getAdditionalInfo();
final String className = extractClassName(
additionalInfo.get(RuleViolation.PACKAGE_NAME),
additionalInfo.get(RuleViolation.CLASS_NAME),
errorDetail.getFileId().getAbsolutePath());
return isExcludedFromFailure(className, errorDetail.getRule().getName());
}

Expand Down
Expand Up @@ -30,7 +30,7 @@
import java.util.Locale;
import java.util.Map;

import net.sourceforge.pmd.RulePriority;
import net.sourceforge.pmd.lang.rule.RulePriority;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.pmd.model.ProcessingError;
Expand Down