Skip to content

yan-khonski-it/java-pmd-starter

Repository files navigation

java-pmd-starter

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.

Example

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.

Problems I faced.

Warning about deprecated rules

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.

PMD was not run

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>

Unit test was not run

I added maven-surefire-plugin. https://stackoverflow.com/questions/53433663/maven-not-running-junit-5-tests

PMD was run after unit tests.

To fix it, I checked the phase from package to validate. https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Old versions reference (archived).

References

About

Use it as a template to start a Java project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages