From b5ea31d53f6c5995c87403c336eb8aa717c6048c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 16 Feb 2023 16:40:24 +0100 Subject: [PATCH] [core] Support environment variable CLASSPATH with pmd.bat under Windows --- docs/pages/pmd/userdocs/cli_reference.md | 18 ++++++++++++++++-- docs/pages/release_notes.md | 2 ++ pmd-dist/src/main/resources/scripts/pmd.bat | 7 ++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/pages/pmd/userdocs/cli_reference.md b/docs/pages/pmd/userdocs/cli_reference.md index 2201d4ab53..cbb6fba4d5 100644 --- a/docs/pages/pmd/userdocs/cli_reference.md +++ b/docs/pages/pmd/userdocs/cli_reference.md @@ -189,8 +189,22 @@ if you want to analyze a project, that uses one of OpenJDK's [Preview Language F Just set the environment variable `PMD_JAVA_OPTS` before executing PMD, e.g. - export PMD_JAVA_OPTS="--enable-preview" - ./run.sh pmd -d ../../../src/main/java/ -f text -R rulesets/java/quickstart.xml +```shell +export PMD_JAVA_OPTS="--enable-preview" +./run.sh pmd -d ../../../src/main/java/ -f text -R rulesets/java/quickstart.xml +``` + +## Additional runtime classpath + +If you develop custom rules and package them as a jar file, you need to add it to PMD's runtime classpath. +You can either copy the jar file into the `lib/` subfolder alongside the other jar files, that are in PMD's +standard distribution. + +Or you can set the environment variable `CLASSPATH` before starting PMD, e.g. + +```shell +CLASSPATH=custom-rule-example.jar ./run.sh pmd -d ../../../src/main/java/ -f text -R myrule.xml +``` ## Exit Status diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 75d5480d5d..c302ba42ea 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -15,6 +15,8 @@ This is a {{ site.pmd.release_type }} release. ### New and noteworthy ### Fixed Issues +* core + * [#4395](https://github.com/pmd/pmd/issues/4395): \[core] Support environment variable CLASSPATH with pmd.bat under Windows * java-errorprone * [#4393](https://github.com/pmd/pmd/issues/4393): \[java] MissingStaticMethodInNonInstantiatableClass false-positive for Lombok's @UtilityClass for classes with non-private fields diff --git a/pmd-dist/src/main/resources/scripts/pmd.bat b/pmd-dist/src/main/resources/scripts/pmd.bat index b3b2c78699..4c2023b8c8 100755 --- a/pmd-dist/src/main/resources/scripts/pmd.bat +++ b/pmd-dist/src/main/resources/scripts/pmd.bat @@ -2,5 +2,10 @@ set TOPDIR="%~dp0.." set OPTS= set MAIN_CLASS=net.sourceforge.pmd.PMD +set PMD_CLASSPATH=%TOPDIR%\lib\* -java %PMD_JAVA_OPTS% -classpath %TOPDIR%\lib\* %OPTS% %MAIN_CLASS% %* +if [%CLASSPATH%] NEQ [] ( + set PMD_CLASSPATH=%CLASSPATH%;%PMD_CLASSPATH% +) + +java %PMD_JAVA_OPTS% -classpath %PMD_CLASSPATH% %OPTS% %MAIN_CLASS% %*