Skip to content

Commit

Permalink
[MPLUGIN-511] add versions history requirements detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Mar 16, 2024
1 parent d9f8d89 commit cd74761
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 31 deletions.
Expand Up @@ -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;
@@ -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
@@ -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>
@@ -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
}
}
@@ -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<')
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<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);
Expand Down Expand Up @@ -257,19 +291,21 @@ private void generateMojosDocumentation(PluginDescriptor pluginDescriptor, Local
}
}

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());
Expand All @@ -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();
}
}

0 comments on commit cd74761

Please sign in to comment.