From cd747611b768031b57fda87bb8e19845d2dc69fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Wed, 6 Mar 2024 02:18:47 +0100 Subject: [PATCH] [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 | 29 ++++++ .../plugin/plugin/report/PluginReport.java | 56 ++++++++++-- .../plugin/report/RequirementsHistory.java | 49 +++++----- 7 files changed, 278 insertions(+), 31 deletions(-) create mode 100644 maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/invoker.properties create mode 100644 maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/pom.xml create mode 100644 maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/src/main/java/org/MyMojo.java create mode 100644 maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy 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 5afea34bf..9520fec9e 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( '1.3' ) assert new File( basedir, 'propertyRelease/target/site/plugin-info.html' ).text.contains( '8' ) assert new File( basedir, 'pluginManagement/target/site/plugin-info.html' ).text.contains( '1.4' ) -assert new File( basedir, 'plugin/target/site/plugin-info.html' ).text.contains( '1.5' ) +assert new File( basedir, 'plugin/target/site/plugin-info.html' ).text.contains( '5' ) 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 000000000..a3d335ef8 --- /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 000000000..a032447e3 --- /dev/null +++ b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/pom.xml @@ -0,0 +1,90 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins + maven-plugin-report-plugin + 1.0-SNAPSHOT + maven-plugin + + MPLUGIN-511 + + Test discovery of plugin requirements history. + + + + UTF-8 + + + + + org.apache.maven + maven-plugin-api + @mavenVersion@ + provided + + + org.apache.maven.plugin-tools + maven-plugin-annotations + @project.version@ + provided + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + @compilerPluginVersion@ + + + org.apache.maven.plugins + maven-site-plugin + @sitePluginVersion@ + + + org.apache.maven.plugins + maven-plugin-plugin + @project.version@ + + prefix + + + + + + + + true + + + org.apache.maven.plugins + maven-plugin-report-plugin + @project.version@ + + + + 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 000000000..d2f2e77f9 --- /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}/") + private String string; + + public void execute() { + // intentional do nothing + } +} diff --git a/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy new file mode 100644 index 000000000..bdf7c0328 --- /dev/null +++ b/maven-plugin-report-plugin/src/it/plugin-report-detect-requirements-history/verify.groovy @@ -0,0 +1,29 @@ +/* + * 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. + */ + +assert new File( basedir, 'target/site/noop-mojo.html' ).isFile() + +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 3c47a3cff..9f1178ff3 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.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.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 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 @@ protected void executeReport(Locale locale) throws MavenReportException { // Generate the mojos' documentation generateMojosDocumentation(pluginDescriptor, locale); + if (requirementsHistories.isEmpty()) { + // detect requirements history + String v = null; + try { + List 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 @@ private void generateMojosDocumentation(PluginDescriptor pluginDescriptor, Local } } - private List discoverVersions() throws VersionRangeResolutionException { + private List 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 @@ private ProjectBuildingResult buildMavenProject(String version) throws ProjectBu 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 9f62910b0..53446d44c 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 String toString() { 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 static String discoverMavenRequirement(MavenProject project, PluginDescri * * * @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 static String discoverJdkRequirement(MavenProject project, PluginDescript } 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 pluginsAsMap) {