It could be used as a starter project to build with Java 25, maven, pmd, log4j2. You can re-use it in other projects.
Feel free to contribute and improve it.
It uses my custom formatting intellij-java-yan-google-style-v2_1.xml, based on the original Google Style formatting (local copy: intellij-java-google-style.xml).
My custom formatter changes: yan v2.1 is Google Style with 120-column lines, compact one-line Javadoc and empty classes; everything else is as in the original Google Style.
The custom ruleset (custom-ruleset.xml) combines several PMD categories (performance, security, codestyle, errorprone, multithreading, bestpractices) with some rules excluded or customized. All categories: https://github.com/pmd/pmd/tree/main/pmd-java/src/main/resources/category/java
To run it, build with maven and then run the main class DemoApp.
mvn clean install
mvn exec:java
You can play with the java code, for example uncomment different sections and run PMD and see the errors. PMD check is the part of the validate phase.
If you rename the method and its argument into
public final class NumberUtils {
// try to rename it to f(n)
public static String f(int n) {
return "[" + n + "]";
}
}Note: the custom ruleset lowers the ShortMethodName minimum to 3 characters, so a 2-character name like fo would
fail. The PMD result will be
[WARNING] PMD Failure: com.yk.utils.pmdstarter.app.utils.NumberUtils:10 Rule:ShortMethodName Priority:3 Avoid using short method names.
[WARNING] PMD Failure: com.yk.utils.pmdstarter.app.utils.NumberUtils:10 Rule:ShortVariable Priority:3 Avoid variables with short names like fo.
There was a warning when I tried to use a specific rule. I found this comment:
<!-- Rules, that have been moved into a category -->
in the source code So instead of using a specific rule, I would use a category.
<ruleset>category/java/bestpractices.xml</ruleset>instead of
<ruleset>rulesets/java/imports.xml</ruleset>The warning text:
[WARNING] Use Rule name category/java/bestpractices.xml/UnusedImports instead of the deprecated Rule name C:\Dev\workspaces\utils\java-17-pmd\target\pmd\rulesets\imports.xml/UnusedImports. PMD 7.0.0 will remove support for this deprecated Rule name usage.
Added the execution configuration and explicitly use goal check in the package phase.
<executions>
<execution>
<id>check pmd and fail</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>I added maven-surefire-plugin. https://stackoverflow.com/questions/53433663/maven-not-running-junit-5-tests
To fix it, I checked the phase from package to validate. https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
- https://github.com/yan-khonski-it/pmd-java-21
- https://github.com/yan-khonski-it/java-17-pmd/tree/main
- https://github.com/yan-khonski-it/pmd-java-14
- https://github.com/yan-khonski-it/mvn-pmd-java11
- https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#shortvariable
- https://pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html
- https://www.baeldung.com/pmd
- https://maven.apache.org/plugins/maven-pmd-plugin/usage.html
- https://github.com/google/styleguide
- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/imports.xml
- https://github.com/apache/maven-surefire