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

gnodet pushed a commit to branch maven4-plugin-descriptor-java-version
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git

commit 161a847da9c01eeaea04344d98a1f8f75f29e394
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Wed May 14 11:59:19 2025 +0200

    Add support for Maven 4 PluginDescriptor.getRequiredJavaVersion() method
    
    This commit enhances the PluginDescriptorHelper class to support the 
getRequiredJavaVersion()
    and setRequiredJavaVersion() methods directly on the PluginDescriptor class 
in Maven 4.
    
    The implementation:
    1. Uses reflection to check if the methods exist in the current Maven 
version
    2. Tries to use the direct methods first if available (Maven 4)
    3. Falls back to the existing wrapper approach if needed (Maven 3)
    
    This approach ensures backward compatibility while taking advantage of the 
new methods in Maven 4.
---
 .../maven/tools/plugin/PluginDescriptorHelper.java | 100 +++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git 
a/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/PluginDescriptorHelper.java
 
b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/PluginDescriptorHelper.java
new file mode 100644
index 00000000..a35baaba
--- /dev/null
+++ 
b/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/PluginDescriptorHelper.java
@@ -0,0 +1,100 @@
+/*
+ * 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.tools.plugin;
+
+import java.lang.reflect.Method;
+
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+
+public class PluginDescriptorHelper {
+
+    private static final Method GET_REQUIRED_JAVA_VERSION_METHOD;
+    private static final Method SET_REQUIRED_JAVA_VERSION_METHOD;
+
+    static {
+        Method getMethod = null;
+        Method setMethod = null;
+        try {
+            getMethod = 
PluginDescriptor.class.getMethod("getRequiredJavaVersion");
+            setMethod = 
PluginDescriptor.class.getMethod("setRequiredJavaVersion", String.class);
+        } catch (NoSuchMethodException e) {
+            // Methods don't exist in this version of Maven
+        }
+        GET_REQUIRED_JAVA_VERSION_METHOD = getMethod;
+        SET_REQUIRED_JAVA_VERSION_METHOD = setMethod;
+    }
+
+    public static String getRequiredJavaVersion(PluginDescriptor descriptor) {
+        if (descriptor == null) {
+            return null;
+        }
+
+        // First try to use the direct method if available in Maven 4
+        if (GET_REQUIRED_JAVA_VERSION_METHOD != null) {
+            try {
+                return (String) 
GET_REQUIRED_JAVA_VERSION_METHOD.invoke(descriptor);
+            } catch (Exception e) {
+                // Fall back to the wrapper approach
+            }
+        }
+
+        // Fall back to the wrapper approach for Maven 3
+        if (descriptor instanceof ExtendedPluginDescriptor) {
+            return ((ExtendedPluginDescriptor) 
descriptor).getRequiredJavaVersion();
+        }
+
+        return null;
+    }
+
+    /**
+     * Sets the required Java version on a plugin descriptor.
+     * <p>
+     * This method works with both Maven 3 and Maven 4:
+     * <ul>
+     *   <li>In Maven 4, it uses the direct method on the PluginDescriptor 
class</li>
+     *   <li>In Maven 3, it uses the ExtendedPluginDescriptor wrapper</li>
+     * </ul>
+     *
+     * @param descriptor the plugin descriptor
+     * @param requiredJavaVersion the required Java version to set
+     * @return the modified plugin descriptor, or null if the input descriptor 
was null
+     */
+    public static PluginDescriptor setRequiredJavaVersion(PluginDescriptor 
descriptor, String requiredJavaVersion) {
+        if (descriptor == null) {
+            return null;
+        }
+
+        // First try to use the direct method if available in Maven 4
+        if (SET_REQUIRED_JAVA_VERSION_METHOD != null) {
+            try {
+                SET_REQUIRED_JAVA_VERSION_METHOD.invoke(descriptor, 
requiredJavaVersion);
+                return descriptor;
+            } catch (Exception e) {
+                // Fall back to the wrapper approach
+            }
+        }
+
+        // Fall back to the wrapper approach for Maven 3
+        if (!(descriptor instanceof ExtendedPluginDescriptor)) {
+            descriptor = new ExtendedPluginDescriptor(descriptor);
+        }
+        ((ExtendedPluginDescriptor) 
descriptor).setRequiredJavaVersion(requiredJavaVersion);
+        return descriptor;
+    }
+}

Reply via email to