This is an automated email from the ASF dual-hosted git repository.

adangel pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 102a8af  Default targetJdk to maven.compiler.release, then 
maven.compiler.source (#788)
102a8af is described below

commit 102a8af13f9c66f33e8407bb23285639248a8e1d
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Thu Nov 19 16:24:49 2026 +0100

    Default targetJdk to maven.compiler.release, then maven.compiler.source 
(#788)
    
    `targetJdk` defaulted to `${maven.compiler.source}` only. A project that 
sets just `maven.compiler.release`, the usual setup since JDK 9, got no 
`targetJdk`: PMD parsed the code as the latest Java version and the plugin 
looked up no JDK toolchain for platform classes. `targetJdk` now falls back to 
`maven.compiler.release`, then to `maven.compiler.source`; an explicit 
`targetJdk` still wins. Release takes precedence over source, as in the Maven 
Compiler Plugin, so a project that sets b [...]
    
    The Target JDK page now describes the new default.
---
 .../org/apache/maven/plugins/pmd/PmdReport.java    | 43 +++++++++++++++++--
 src/site/markdown/examples/targetJdk.md.vm         |  2 +-
 .../maven/plugins/pmd/TargetJdkResolutionTest.java | 48 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java 
b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
index d60dcf7..d365672 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -83,10 +83,25 @@ public class PmdReport extends AbstractPmdReport {
      * <p>
      *   <b>Note:</b> this parameter is only used if the language parameter is 
set to <code>java</code>.
      * </p>
+     * <p>If not set, the value of {@code maven.compiler.release} is used, or 
{@code maven.compiler.source} when no
+     * release is set, as the Maven Compiler Plugin does.</p>
      */
-    @Parameter(property = "targetJdk", defaultValue = 
"${maven.compiler.source}")
+    @Parameter(property = "targetJdk")
     private String targetJdk;
 
+    /**
+     * The {@code maven.compiler.release} property, used when {@link 
#targetJdk} is not set.
+     */
+    @Parameter(defaultValue = "${maven.compiler.release}", readonly = true)
+    private String compilerRelease;
+
+    /**
+     * The {@code maven.compiler.source} property, used when neither {@link 
#targetJdk} nor
+     * {@code maven.compiler.release} is set.
+     */
+    @Parameter(defaultValue = "${maven.compiler.source}", readonly = true)
+    private String compilerSource;
+
     /**
      * The programming language to be analyzed by PMD. Valid values are 
currently <code>java</code>,
      * <code>javascript</code> and <code>jsp</code>.
@@ -389,7 +404,7 @@ public class PmdReport extends AbstractPmdReport {
         }
 
         PmdRequest request = new PmdRequest();
-        request.setLanguageAndVersion(language, targetJdk);
+        request.setLanguageAndVersion(language, getTargetJdk());
         request.setRulesets(resolveRulesets());
         request.setAuxClasspath(typeResolution ? determineAuxClasspath() : 
null);
         request.setSourceEncoding(getInputEncoding());
@@ -626,10 +641,32 @@ public class PmdReport extends AbstractPmdReport {
         return false;
     }
 
+    /**
+     * @return the explicit {@code targetJdk}, else {@code 
maven.compiler.release}, else {@code maven.compiler.source}
+     */
+    private String getTargetJdk() {
+        return resolveTargetJdk(targetJdk, compilerRelease, compilerSource);
+    }
+
+    static String resolveTargetJdk(String targetJdk, String release, String 
source) {
+        if (isNotBlank(targetJdk)) {
+            return targetJdk;
+        }
+        if (isNotBlank(release)) {
+            return release;
+        }
+        return isNotBlank(source) ? source : null;
+    }
+
+    private static boolean isNotBlank(String value) {
+        return value != null && !value.trim().isEmpty();
+    }
+
     private List<String> determineJavaRuntimeClasses() {
         if (!"java".equals(language) && null != language) {
             return Collections.emptyList();
         }
+        String targetJdk = getTargetJdk();
         if (targetJdk == null) {
             return Collections.emptyList();
         }
@@ -660,7 +697,7 @@ public class PmdReport extends AbstractPmdReport {
                 getLog().warn("Couldn't find java runtime classes in " + 
javaHome
                         + ". Please make sure the java installation exists.");
             } else {
-                getLog().warn("Could't find java executable for " + toolchain
+                getLog().warn("Couldn't find java executable for " + toolchain
                         + ". Please double check your toolchains.xml for 
outdated entries.");
             }
         }
diff --git a/src/site/markdown/examples/targetJdk.md.vm 
b/src/site/markdown/examples/targetJdk.md.vm
index bd03d0b..1645aec 100644
--- a/src/site/markdown/examples/targetJdk.md.vm
+++ b/src/site/markdown/examples/targetJdk.md.vm
@@ -27,7 +27,7 @@ under the License.
 
 # Target JDK
 
-There is a configuration element in the PMD plugin that lets you set which 
[target JDK](../pmd-mojo.html#targetJdk) your Java code will be compiled for. 
Often you want this to be in sync with the configuration for 
maven-compiler-plugin. `targetJdk` defaults to the property 
`maven.compiler.source`, so a project that sets that property needs no further 
configuration. A project that sets only `maven.compiler.release` must pass it 
on explicitly, for example `<targetJdk>${esc.d}{maven.compile [...]
+There is a configuration element in the PMD plugin that lets you set which 
[target JDK](../pmd-mojo.html#targetJdk) your Java code will be compiled for. 
Often you want this to be in sync with the configuration for 
maven-compiler-plugin. `targetJdk` defaults to the property 
`maven.compiler.release`, or to `maven.compiler.source` when no release is set, 
as the Maven Compiler Plugin does, so a project that sets either property needs 
no further configuration. Alternatively, you can set the v [...]
 
 ```xml
 <project>
diff --git 
a/src/test/java/org/apache/maven/plugins/pmd/TargetJdkResolutionTest.java 
b/src/test/java/org/apache/maven/plugins/pmd/TargetJdkResolutionTest.java
new file mode 100644
index 0000000..4cb07e7
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/pmd/TargetJdkResolutionTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.pmd;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class TargetJdkResolutionTest {
+
+    @Test
+    void explicitTargetJdkWins() {
+        assertEquals("11", PmdReport.resolveTargetJdk("11", "17", "1.8"));
+    }
+
+    @Test
+    void releaseIsUsedBeforeSource() {
+        assertEquals("17", PmdReport.resolveTargetJdk(null, "17", "1.8"));
+    }
+
+    @Test
+    void sourceIsUsedWithoutRelease() {
+        assertEquals("1.8", PmdReport.resolveTargetJdk(null, null, "1.8"));
+        assertEquals("1.8", PmdReport.resolveTargetJdk(" ", "", "1.8"));
+    }
+
+    @Test
+    void nothingSetGivesNull() {
+        assertNull(PmdReport.resolveTargetJdk(null, null, null));
+    }
+}

Reply via email to