Skip to content

Commit facedc9

Browse files
gnodetslawekjaranowski
authored andcommitted
Add support for Maven 4 PluginDescriptor.getRequiredJavaVersion() method
* 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 (cherry picked from commit b939390)
1 parent 0e2322a commit facedc9

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
@@ -41,7 +41,7 @@
4141
import org.apache.maven.plugins.annotations.ResolutionScope;
4242
import org.apache.maven.project.MavenProject;
4343
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
44-
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
44+
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
4545
import org.apache.maven.tools.plugin.PluginToolsRequest;
4646
import org.apache.maven.tools.plugin.extractor.ExtractionException;
4747
import org.apache.maven.tools.plugin.generator.GeneratorException;
@@ -376,10 +376,9 @@ public void generate() throws MojoExecutionException {
376376
}
377377

378378
private PluginDescriptor extendPluginDescriptor(PluginToolsRequest request) {
379-
ExtendedPluginDescriptor extendedPluginDescriptor = new ExtendedPluginDescriptor(request.getPluginDescriptor());
380-
extendedPluginDescriptor.setRequiredJavaVersion(getRequiredJavaVersion(request));
381-
extendedPluginDescriptor.setRequiredMavenVersion(getRequiredMavenVersion(request));
382-
return extendedPluginDescriptor;
379+
PluginDescriptor pluginDescriptor = request.getPluginDescriptor();
380+
pluginDescriptor.setRequiredMavenVersion(getRequiredMavenVersion(request));
381+
return PluginDescriptorHelper.setRequiredJavaVersion(pluginDescriptor, getRequiredJavaVersion(request));
383382
}
384383

385384
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);
@@ -147,11 +146,9 @@ public void writeDescriptor(File destinationFile, PluginToolsRequest request, De
147146
GeneratorUtils.element(
148147
w, "inheritedByDefault", String.valueOf(pluginDescriptor.isInheritedByDefault()));
149148

150-
if (pluginDescriptor instanceof ExtendedPluginDescriptor) {
151-
ExtendedPluginDescriptor extPluginDescriptor = (ExtendedPluginDescriptor) pluginDescriptor;
152-
if (StringUtils.isNotBlank(extPluginDescriptor.getRequiredJavaVersion())) {
153-
GeneratorUtils.element(w, "requiredJavaVersion", extPluginDescriptor.getRequiredJavaVersion());
154-
}
149+
if (StringUtils.isNotBlank(PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor))) {
150+
GeneratorUtils.element(
151+
w, "requiredJavaVersion", PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor));
155152
}
156153
if (StringUtils.isNotBlank(pluginDescriptor.getRequiredMavenVersion())) {
157154
GeneratorUtils.element(w, "requiredMavenVersion", pluginDescriptor.getRequiredMavenVersion());
@@ -195,7 +192,6 @@ public void writeDescriptor(File destinationFile, PluginToolsRequest request, De
195192
}
196193

197194
/**
198-
*
199195
* @param type
200196
* @param containsXhtmlValue
201197
* @param text
@@ -585,6 +581,7 @@ else if (type != DescriptorType.LIMITED_FOR_HELP_MOJO || parameter.isEditable())
585581

586582
/**
587583
* Writes parameter type information and potentially also the related javadoc URL.
584+
*
588585
* @param w
589586
* @param type
590587
* @param javadocLinkGenerator

0 commit comments

Comments
 (0)