Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
import org.apache.maven.tools.plugin.PluginToolsRequest;
import org.apache.maven.tools.plugin.extractor.ExtractionException;
import org.apache.maven.tools.plugin.generator.GeneratorException;
Expand Down Expand Up @@ -521,10 +521,9 @@ static byte[] computeGeneratorClassBytes(
}

private PluginDescriptor extendPluginDescriptor(PluginToolsRequest request) {
ExtendedPluginDescriptor extendedPluginDescriptor = new ExtendedPluginDescriptor(request.getPluginDescriptor());
extendedPluginDescriptor.setRequiredJavaVersion(getRequiredJavaVersion(request));
extendedPluginDescriptor.setRequiredMavenVersion(getRequiredMavenVersion(request));
return extendedPluginDescriptor;
PluginDescriptor pluginDescriptor = request.getPluginDescriptor();
pluginDescriptor.setRequiredMavenVersion(getRequiredMavenVersion(request));
return PluginDescriptorHelper.setRequiredJavaVersion(pluginDescriptor, getRequiredJavaVersion(request));
}

private String getRequiredMavenVersion(PluginToolsRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.maven.model.Prerequisites;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
import org.codehaus.plexus.util.xml.Xpp3Dom;

/**
Expand Down Expand Up @@ -114,11 +114,7 @@ public static String discoverMavenRequirement(MavenProject project, PluginDescri
* @return the JDK version
*/
public static String discoverJdkRequirement(MavenProject project, PluginDescriptor pluginDescriptor) {
String jdk = null;
if (pluginDescriptor instanceof ExtendedPluginDescriptor) {
ExtendedPluginDescriptor extPluginDescriptor = (ExtendedPluginDescriptor) pluginDescriptor;
jdk = extPluginDescriptor.getRequiredJavaVersion();
}
String jdk = PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor);
if (jdk != null) {
return jdk;
}
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How it will be used?

Why not direct use class from Maven core ... such master branch will be dedicated to Maven 4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I still think being able to build a maximum of projects targeting Maven 3 with Maven 4 is a good idea. We do want maximum compatibility when possible.
Also, if we target Maven 4, we should move to the new API, but that's more work imho.
So I'd rather to a micro release, and then start migrating this plugin to target Maven 4.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should we do it in branch: maven-plugin-tools-3.x ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should we do it in branch: maven-plugin-tools-3.x ?

Yes, I suppose. Not sure we need to do it now, unless someone wants to start the migration asap though.

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.maven.plugin.descriptor.Requirement;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
import org.apache.maven.tools.plugin.ExtendedPluginDescriptor;
import org.apache.maven.tools.plugin.PluginDescriptorHelper;
import org.apache.maven.tools.plugin.PluginToolsRequest;
import org.apache.maven.tools.plugin.javadoc.JavadocLinkGenerator;
import org.apache.maven.tools.plugin.util.PluginUtils;
Expand All @@ -58,7 +58,6 @@
* </ol>
* from a given in-memory descriptor. The in-memory descriptor acting as source is supposed to contain XHTML values
* for description elements.
*
*/
public class PluginDescriptorFilesGenerator implements Generator {
private static final Logger LOG = LoggerFactory.getLogger(PluginDescriptorFilesGenerator.class);
Expand Down Expand Up @@ -157,11 +156,9 @@ public void writeDescriptor(File destinationFile, PluginToolsRequest request, De
GeneratorUtils.element(
w, "inheritedByDefault", String.valueOf(pluginDescriptor.isInheritedByDefault()));

if (pluginDescriptor instanceof ExtendedPluginDescriptor) {
ExtendedPluginDescriptor extPluginDescriptor = (ExtendedPluginDescriptor) pluginDescriptor;
if (StringUtils.isNotBlank(extPluginDescriptor.getRequiredJavaVersion())) {
GeneratorUtils.element(w, "requiredJavaVersion", extPluginDescriptor.getRequiredJavaVersion());
}
if (StringUtils.isNotBlank(PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor))) {
GeneratorUtils.element(
w, "requiredJavaVersion", PluginDescriptorHelper.getRequiredJavaVersion(pluginDescriptor));
}
if (StringUtils.isNotBlank(pluginDescriptor.getRequiredMavenVersion())) {
GeneratorUtils.element(w, "requiredMavenVersion", pluginDescriptor.getRequiredMavenVersion());
Expand Down Expand Up @@ -205,7 +202,6 @@ public void writeDescriptor(File destinationFile, PluginToolsRequest request, De
}

/**
*
* @param type
* @param containsXhtmlValue
* @param text
Expand Down Expand Up @@ -618,6 +614,7 @@ else if (type != DescriptorType.LIMITED_FOR_HELP_MOJO || parameter.isEditable())

/**
* Writes parameter type information and potentially also the related javadoc URL.
*
* @param w
* @param type
* @param javadocLinkGenerator
Expand Down