1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.tools.plugin;
20
21 import java.lang.reflect.Method;
22
23 import org.apache.maven.plugin.descriptor.PluginDescriptor;
24
25 public class PluginDescriptorHelper {
26
27 private static final Method GET_REQUIRED_JAVA_VERSION_METHOD;
28 private static final Method SET_REQUIRED_JAVA_VERSION_METHOD;
29
30 static {
31 Method getMethod = null;
32 Method setMethod = null;
33 try {
34 getMethod = PluginDescriptor.class.getMethod("getRequiredJavaVersion");
35 setMethod = PluginDescriptor.class.getMethod("setRequiredJavaVersion", String.class);
36 } catch (NoSuchMethodException e) {
37 // Methods don't exist in this version of Maven
38 }
39 GET_REQUIRED_JAVA_VERSION_METHOD = getMethod;
40 SET_REQUIRED_JAVA_VERSION_METHOD = setMethod;
41 }
42
43 public static String getRequiredJavaVersion(PluginDescriptor descriptor) {
44 if (descriptor == null) {
45 return null;
46 }
47
48 // First try to use the direct method if available in Maven 4
49 if (GET_REQUIRED_JAVA_VERSION_METHOD != null) {
50 try {
51 return (String) GET_REQUIRED_JAVA_VERSION_METHOD.invoke(descriptor);
52 } catch (Exception e) {
53 // Fall back to the wrapper approach
54 }
55 }
56
57 // Fall back to the wrapper approach for Maven 3
58 if (descriptor instanceof ExtendedPluginDescriptor) {
59 return ((ExtendedPluginDescriptor) descriptor).getRequiredJavaVersion();
60 }
61
62 return null;
63 }
64
65 /**
66 * Sets the required Java version on a plugin descriptor.
67 * <p>
68 * This method works with both Maven 3 and Maven 4:
69 * <ul>
70 * <li>In Maven 4, it uses the direct method on the PluginDescriptor class</li>
71 * <li>In Maven 3, it uses the ExtendedPluginDescriptor wrapper</li>
72 * </ul>
73 *
74 * @param descriptor the plugin descriptor
75 * @param requiredJavaVersion the required Java version to set
76 * @return the modified plugin descriptor, or null if the input descriptor was null
77 */
78 public static PluginDescriptor setRequiredJavaVersion(PluginDescriptor descriptor, String requiredJavaVersion) {
79 if (descriptor == null) {
80 return null;
81 }
82
83 // First try to use the direct method if available in Maven 4
84 if (SET_REQUIRED_JAVA_VERSION_METHOD != null) {
85 try {
86 SET_REQUIRED_JAVA_VERSION_METHOD.invoke(descriptor, requiredJavaVersion);
87 return descriptor;
88 } catch (Exception e) {
89 // Fall back to the wrapper approach
90 }
91 }
92
93 // Fall back to the wrapper approach for Maven 3
94 if (!(descriptor instanceof ExtendedPluginDescriptor)) {
95 descriptor = new ExtendedPluginDescriptor(descriptor);
96 }
97 ((ExtendedPluginDescriptor) descriptor).setRequiredJavaVersion(requiredJavaVersion);
98 return descriptor;
99 }
100 }