Skip to content

Commit b939390

Browse files
authored
Add support for Maven 4 PluginDescriptor.getRequiredJavaVersion() method (#401)
* 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. * Add missing files
1 parent 5364f90 commit b939390

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.apache.maven.project.MavenProject;
4949
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
5050
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
51-
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
51+
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
5252
import org.apache.maven.tools.plugin.PluginToolsRequest;
5353
import org.apache.maven.tools.plugin.extractor.ExtractionException;
5454
import org.apache.maven.tools.plugin.generator.GeneratorException;
@@ -521,10 +521,9 @@ static byte[] computeGeneratorClassBytes(
521521
}
522522

523523
private PluginDescriptor extendPluginDescriptor(PluginToolsRequest request) {
524-
ExtendedPluginDescriptor extendedPluginDescriptor = new ExtendedPluginDescriptor(request.getPluginDescriptor());
525-
extendedPluginDescriptor.setRequiredJavaVersion(getRequiredJavaVersion(request));
526-
extendedPluginDescriptor.setRequiredMavenVersion(getRequiredMavenVersion(request));
527-
return extendedPluginDescriptor;
524+
PluginDescriptor pluginDescriptor = request.getPluginDescriptor();
525+
pluginDescriptor.setRequiredMavenVersion(getRequiredMavenVersion(request));
526+
return PluginDescriptorHelper.setRequiredJavaVersion(pluginDescriptor, getRequiredJavaVersion(request));
528527
}
529528

530529
private String getRequiredMavenVersion(PluginToolsRequest request) {

maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/RequirementsHistory.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.apache.maven.model.Prerequisites;
3030
import org.apache.maven.plugin.descriptor.PluginDescriptor;
3131
import org.apache.maven.project.MavenProject;
32-
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
32+
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
3333
import org.codehaus.plexus.util.xml.Xpp3Dom;
3434

3535
/**
@@ -114,11 +114,7 @@ public static String discoverMavenRequirement(MavenProject project, PluginDescri
114114
* @return the JDK version
115115
*/
116116
public static String discoverJdkRequirement(MavenProject project, PluginDescriptor pluginDescriptor) {
117-
String jdk = null;
118-
if (pluginDescriptor instanceof ExtendedPluginDescriptor) {
119-
ExtendedPluginDescriptor extPluginDescriptor = (ExtendedPluginDescriptor) pluginDescriptor;
120-
jdk = extPluginDescriptor.getRequiredJavaVersion();
121-
}
117+
String jdk = PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor);
122118
if (jdk != null) {
123119
return jdk;
124120
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorFilesGenerator.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.apache.maven.plugin.descriptor.Requirement;
3636
import org.apache.maven.project.MavenProject;
3737
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
38-
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
38+
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
3939
import org.apache.maven.tools.plugin.PluginToolsRequest;
4040
import org.apache.maven.tools.plugin.javadoc.JavadocLinkGenerator;
4141
import org.apache.maven.tools.plugin.util.PluginUtils;
@@ -58,7 +58,6 @@
5858
* </ol>
5959
* from a given in-memory descriptor. The in-memory descriptor acting as source is supposed to contain XHTML values
6060
* for description elements.
61-
*
6261
*/
6362
public class PluginDescriptorFilesGenerator implements Generator {
6463
private static final Logger LOG = LoggerFactory.getLogger(PluginDescriptorFilesGenerator.class);
@@ -157,11 +156,9 @@ public void writeDescriptor(File destinationFile, PluginToolsRequest request, De
157156
GeneratorUtils.element(
158157
w, "inheritedByDefault", String.valueOf(pluginDescriptor.isInheritedByDefault()));
159158

160-
if (pluginDescriptor instanceof ExtendedPluginDescriptor) {
161-
ExtendedPluginDescriptor extPluginDescriptor = (ExtendedPluginDescriptor) pluginDescriptor;
162-
if (StringUtils.isNotBlank(extPluginDescriptor.getRequiredJavaVersion())) {
163-
GeneratorUtils.element(w, "requiredJavaVersion", extPluginDescriptor.getRequiredJavaVersion());
164-
}
159+
if (StringUtils.isNotBlank(PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor))) {
160+
GeneratorUtils.element(
161+
w, "requiredJavaVersion", PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor));
165162
}
166163
if (StringUtils.isNotBlank(pluginDescriptor.getRequiredMavenVersion())) {
167164
GeneratorUtils.element(w, "requiredMavenVersion", pluginDescriptor.getRequiredMavenVersion());
@@ -205,7 +202,6 @@ public void writeDescriptor(File destinationFile, PluginToolsRequest request, De
205202
}
206203

207204
/**
208-
*
209205
* @param type
210206
* @param containsXhtmlValue
211207
* @param text
@@ -618,6 +614,7 @@ else if (type != DescriptorType.LIMITED_FOR_HELP_MOJO || parameter.isEditable())
618614

619615
/**
620616
* Writes parameter type information and potentially also the related javadoc URL.
617+
*
621618
* @param w
622619
* @param type
623620
* @param javadocLinkGenerator

0 commit comments

Comments
 (0)