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

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git

commit cd747611b768031b57fda87bb8e19845d2dc69fa
Author: Hervé Boutemy <hbout...@apache.org>
AuthorDate: Wed Mar 6 02:18:47 2024 +0100

    [MPLUGIN-511] add versions history requirements detection
---
 .../src/it/plugin-info-jdk/verify.groovy           |  2 +-
 .../invoker.properties                             | 18 +++++
 .../pom.xml                                        | 90 ++++++++++++++++++++++
 .../src/main/java/org/MyMojo.java                  | 65 ++++++++++++++++
 .../verify.groovy                                  | 14 ++--
 .../maven/plugin/plugin/report/PluginReport.java   | 56 +++++++++++---
 .../plugin/plugin/report/RequirementsHistory.java  | 49 +++++++-----
 7 files changed, 258 insertions(+), 36 deletions(-)

diff --git a/maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy 
b/maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy
index 5afea34b..9520fec9 100644
--- a/maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy
+++ b/maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy
@@ -20,6 +20,6 @@
 assert new File( basedir, 'property/target/site/plugin-info.html' 
).text.contains( '<td>1.3</td>' )
 assert new File( basedir, 'propertyRelease/target/site/plugin-info.html' 
).text.contains( '<td>8</td>' )
 assert new File( basedir, 'pluginManagement/target/site/plugin-info.html' 
).text.contains( '<td>1.4</td>' )
-assert new File( basedir, 'plugin/target/site/plugin-info.html' 
).text.contains( '<td>1.5</td>' )
+assert new File( basedir, 'plugin/target/site/plugin-info.html' 
).text.contains( '<td>5</td>' )
 
 return true;
diff --git 
a/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/invoker.properties
 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/invoker.properties
new file mode 100644
index 00000000..a3d335ef
--- /dev/null
+++ 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/invoker.properties
@@ -0,0 +1,18 @@
+# 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.
+
+invoker.goals = site
diff --git 
a/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/pom.xml
 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/pom.xml
new file mode 100644
index 00000000..a032447e
--- /dev/null
+++ 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/pom.xml
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins</groupId>
+  <artifactId>maven-plugin-report-plugin</artifactId><!-- intentionally reuse 
plugin's groupId:artifactId to build its releases history -->
+  <version>1.0-SNAPSHOT</version>
+  <packaging>maven-plugin</packaging>
+
+  <name>MPLUGIN-511</name>
+  <description>
+    Test discovery of plugin requirements history.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <version>@mavenVersion@</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.plugin-tools</groupId>
+      <artifactId>maven-plugin-annotations</artifactId>
+      <version>@project.version@</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-compiler-plugin</artifactId>
+          <version>@compilerPluginVersion@</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>@sitePluginVersion@</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-plugin-plugin</artifactId>
+          <version>@project.version@</version>
+          <configuration>
+            <goalPrefix>prefix</goalPrefix>
+          </configuration>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+
+  <reporting>
+    <excludeDefaults>true</excludeDefaults>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-plugin-report-plugin</artifactId>
+        <version>@project.version@</version>
+      </plugin>
+    </plugins>
+  </reporting>
+</project>
diff --git 
a/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/src/main/java/org/MyMojo.java
 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/src/main/java/org/MyMojo.java
new file mode 100644
index 00000000..d2f2e77f
--- /dev/null
+++ 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/src/main/java/org/MyMojo.java
@@ -0,0 +1,65 @@
+/*
+ * 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;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugins.annotations.Execute;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+
+/**
+ * Does nothing.
+ *
+ * @since 1.0
+ * @deprecated You don't use test goals, do you?
+ */
+@Mojo(
+        name = "noop",
+        defaultPhase = LifecyclePhase.PROCESS_SOURCES,
+        requiresDependencyResolution = ResolutionScope.TEST,
+        requiresDirectInvocation = true,
+        requiresOnline = true,
+        inheritByDefault = false,
+        aggregator = true)
+@Execute(phase = LifecyclePhase.COMPILE)
+public class MyMojo extends AbstractMojo {
+
+    /**
+     * This is a test.
+     */
+    @SuppressWarnings("unused")
+    @Parameter(required = true)
+    private String required;
+
+    /**
+     * This is a test.
+     *
+     * @since 1.1
+     * @deprecated Just testing.
+     */
+    @SuppressWarnings("unused")
+    @Parameter(property = "string", defaultValue = 
"${project.version}/</markup-must-be-escaped>")
+    private String string;
+
+    public void execute() {
+        // intentional do nothing
+    }
+}
diff --git a/maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy
similarity index 65%
copy from maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy
copy to 
maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy
index 5afea34b..bdf7c032 100644
--- a/maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy
+++ 
b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy
@@ -17,9 +17,13 @@
  * under the License.
  */
 
-assert new File( basedir, 'property/target/site/plugin-info.html' 
).text.contains( '<td>1.3</td>' )
-assert new File( basedir, 'propertyRelease/target/site/plugin-info.html' 
).text.contains( '<td>8</td>' )
-assert new File( basedir, 'pluginManagement/target/site/plugin-info.html' 
).text.contains( '<td>1.4</td>' )
-assert new File( basedir, 'plugin/target/site/plugin-info.html' 
).text.contains( '<td>1.5</td>' )
+assert new File( basedir, 'target/site/noop-mojo.html' ).isFile()
 
-return true;
+def pluginInfo = new File( basedir, 'target/site/plugin-info.html' )
+assert pluginInfo.isFile()
+
+assert pluginInfo.text.contains('3.11.0')
+assert pluginInfo.text.contains('3.10.2')
+assert pluginInfo.text.contains('3.9.0')
+assert pluginInfo.text.contains('3.7.0')
+assert pluginInfo.text.contains('>8<')
diff --git 
a/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/PluginReport.java
 
b/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/PluginReport.java
index 3c47a3cf..9f1178ff 100644
--- 
a/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/PluginReport.java
+++ 
b/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/PluginReport.java
@@ -23,10 +23,13 @@ import java.io.IOException;
 import java.io.Reader;
 import java.nio.file.Files;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
+import java.util.stream.Collectors;
 
 import org.apache.maven.RepositoryUtils;
+import org.apache.maven.artifact.ArtifactUtils;
 import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.building.ModelBuildingRequest;
@@ -44,7 +47,6 @@ import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuilder;
 import org.apache.maven.project.ProjectBuildingException;
 import org.apache.maven.project.ProjectBuildingRequest;
-import org.apache.maven.project.ProjectBuildingResult;
 import org.apache.maven.reporting.AbstractMavenReport;
 import org.apache.maven.reporting.MavenReportException;
 import org.apache.maven.rtinfo.RuntimeInformation;
@@ -108,6 +110,14 @@ public class PluginReport extends AbstractMavenReport {
     @Parameter
     private List<RequirementsHistory> requirementsHistories = new 
ArrayList<>();
 
+    /**
+     * Plugin's version range for automatic detection of requirements history.
+     *
+     * @since 3.12.0
+     */
+    @Parameter(defaultValue = "[0,)")
+    private String requirementsHistoryDetectionRange;
+
     @Component
     private RuntimeInformation rtInfo;
 
@@ -174,6 +184,30 @@ public class PluginReport extends AbstractMavenReport {
         // Generate the mojos' documentation
         generateMojosDocumentation(pluginDescriptor, locale);
 
+        if (requirementsHistories.isEmpty()) {
+            // detect requirements history
+            String v = null;
+            try {
+                List<Version> versions = 
discoverVersions(requirementsHistoryDetectionRange);
+                getLog().info("Detecting requirements history for " + 
requirementsHistoryDetectionRange + ": "
+                        + versions.size());
+
+                Collections.reverse(versions);
+                for (Version version : versions) {
+                    v = version.toString();
+                    MavenProject versionProject = buildMavenProject(v);
+                    RequirementsHistory requirements = 
RequirementsHistory.discoverRequirements(versionProject);
+                    requirementsHistories.add(requirements);
+                    getLog().info("  - " + requirements);
+                }
+            } catch (VersionRangeResolutionException vrre) {
+                throw new MavenReportException(
+                        "Cannot resolve past versions " + 
requirementsHistoryDetectionRange, vrre);
+            } catch (ProjectBuildingException pbe) {
+                throw new MavenReportException("Cannot resolve MavenProject 
for version " + v, pbe);
+            }
+        }
+
         // Write the overview
         PluginOverviewRenderer r = new PluginOverviewRenderer(
                 getSink(), i18n, locale, getProject(), requirementsHistories, 
pluginDescriptor, hasExtensionsToLoad);
@@ -257,19 +291,21 @@ public class PluginReport extends AbstractMavenReport {
         }
     }
 
-    private List<Version> discoverVersions() throws 
VersionRangeResolutionException {
+    private List<Version> discoverVersions(String range) throws 
VersionRangeResolutionException {
         MavenProject currentProject = mavenSession.getCurrentProject();
         VersionRangeRequest rangeRequest = new VersionRangeRequest();
         rangeRequest.setArtifact(
-                new DefaultArtifact(currentProject.getGroupId() + ":" + 
currentProject.getArtifactId() + ":[0,)"));
+                new DefaultArtifact(currentProject.getGroupId() + ":" + 
currentProject.getArtifactId() + ":" + range));
         rangeRequest.setRepositories(
                 
RepositoryUtils.toRepos(mavenSession.getCurrentProject().getRemoteArtifactRepositories()));
         VersionRangeResult rangeResult =
                 
repositorySystem.resolveVersionRange(mavenSession.getRepositorySession(), 
rangeRequest);
-        return rangeResult.getVersions();
+        return rangeResult.getVersions().stream()
+                .filter(version -> 
!ArtifactUtils.isSnapshot(version.toString()))
+                .collect(Collectors.toList());
     }
 
-    private ProjectBuildingResult buildMavenProject(String version) throws 
ProjectBuildingException {
+    private MavenProject buildMavenProject(String version) throws 
ProjectBuildingException {
         MavenProject currentProject = mavenSession.getCurrentProject();
         ProjectBuildingRequest buildRequest = new 
DefaultProjectBuildingRequest();
         buildRequest.setLocalRepository(mavenSession.getLocalRepository());
@@ -280,9 +316,11 @@ public class PluginReport extends AbstractMavenReport {
         buildRequest.setSystemProperties(mavenSession.getSystemProperties());
         buildRequest.setUserProperties(mavenSession.getUserProperties());
         buildRequest.setRepositorySession(mavenSession.getRepositorySession());
-        return projectBuilder.build(
-                RepositoryUtils.toArtifact(new DefaultArtifact(
-                        currentProject.getGroupId() + ":" + 
currentProject.getArtifactId() + ":pom:" + version)),
-                buildRequest);
+        return projectBuilder
+                .build(
+                        RepositoryUtils.toArtifact(new 
DefaultArtifact(currentProject.getGroupId() + ":"
+                                + currentProject.getArtifactId() + ":pom:" + 
version)),
+                        buildRequest)
+                .getProject();
     }
 }
diff --git 
a/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/RequirementsHistory.java
 
b/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/RequirementsHistory.java
index 9f62910b..53446d44 100644
--- 
a/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/RequirementsHistory.java
+++ 
b/maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/RequirementsHistory.java
@@ -18,6 +18,7 @@
  */
 package org.apache.maven.plugin.plugin.report;
 
+import java.util.Arrays;
 import java.util.Map;
 import java.util.Optional;
 
@@ -72,16 +73,23 @@ public class RequirementsHistory {
         return sb.toString();
     }
 
+    public static RequirementsHistory discoverRequirements(MavenProject 
project) {
+        RequirementsHistory req = new RequirementsHistory();
+        req.version = project.getVersion();
+        req.jdk = discoverJdkRequirement(project, null);
+        req.maven = discoverMavenRequirement(project, null);
+        return req;
+    }
     /**
      * Tries to determine the Maven requirement from either the plugin 
descriptor or (if not set) from the
      * Maven prerequisites element in the POM.
      *
      * @param project      not null
-     * @param pluginDescriptor the plugin descriptor (not null)
+     * @param pluginDescriptor the plugin descriptor (can be null)
      * @return the Maven version or null if not specified
      */
     public static String discoverMavenRequirement(MavenProject project, 
PluginDescriptor pluginDescriptor) {
-        if 
(StringUtils.isNotBlank(pluginDescriptor.getRequiredMavenVersion())) {
+        if (pluginDescriptor != null && 
StringUtils.isNotBlank(pluginDescriptor.getRequiredMavenVersion())) {
             return pluginDescriptor.getRequiredMavenVersion();
         }
         return Optional.ofNullable(project.getPrerequisites())
@@ -100,7 +108,7 @@ public class RequirementsHistory {
      * </ol>
      *
      * @param project      not null
-     * @param pluginDescriptor the plugin descriptor (not null)
+     * @param pluginDescriptor the plugin descriptor (can be null)
      * @return the JDK version
      */
     public static String discoverJdkRequirement(MavenProject project, 
PluginDescriptor pluginDescriptor) {
@@ -118,33 +126,32 @@ public class RequirementsHistory {
         }
 
         jdk = getPluginParameter(compiler, "release");
-        if (jdk != null) {
-            return jdk;
+        if (jdk == null) {
+            jdk = 
project.getProperties().getProperty("maven.compiler.release");
         }
 
-        jdk = project.getProperties().getProperty("maven.compiler.release");
-        if (jdk != null) {
-            return jdk;
+        if (jdk == null) {
+            jdk = getPluginParameter(compiler, "target");
         }
 
-        jdk = getPluginParameter(compiler, "target");
-        if (jdk != null) {
-            return jdk;
+        if (jdk == null) {
+            // default value
+            jdk = project.getProperties().getProperty("maven.compiler.target");
         }
 
-        // default value
-        jdk = project.getProperties().getProperty("maven.compiler.target");
-        if (jdk != null) {
-            return jdk;
-        }
+        if (jdk == null) {
+            String version = (compiler == null) ? null : compiler.getVersion();
 
-        String version = (compiler == null) ? null : compiler.getVersion();
-
-        if (version != null) {
-            return "Default target for maven-compiler-plugin version " + 
version;
+            if (version != null) {
+                return "Default target for maven-compiler-plugin version " + 
version;
+            }
+        } else {
+            if (Arrays.asList("1.5", "1.6", "1.7", "1.8").contains(jdk)) {
+                jdk = jdk.substring(2);
+            }
         }
 
-        return null;
+        return jdk;
     }
 
     private static Plugin getCompilerPlugin(Map<String, Plugin> pluginsAsMap) {

Reply via email to