Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPLUGIN-511 add plugin requirements history detection #269

Merged
merged 2 commits into from
Mar 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<')
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@
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;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
Expand All @@ -36,12 +42,23 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.plugin.descriptor.EnhancedPluginDescriptorBuilder;
import org.apache.maven.project.DefaultProjectBuildingRequest;
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.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.eclipse.aether.version.Version;

/**
* Generates the Plugin's documentation report: <code>plugin-info.html</code> plugin overview page,
Expand Down Expand Up @@ -93,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 @@ -123,6 +148,15 @@ public class PluginReport extends AbstractMavenReport {
@Parameter(property = "maven.plugin.report.disableInternalJavadocLinkValidation")
private boolean disableInternalJavadocLinkValidation;

@Component
private MavenSession mavenSession;

@Component
private RepositorySystem repositorySystem;

@Component
private ProjectBuilder projectBuilder;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -150,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 @@ -232,4 +290,37 @@ private void generateMojosDocumentation(PluginDescriptor pluginDescriptor, Local
}
}
}

private List<Version> discoverVersions(String range) throws VersionRangeResolutionException {
MavenProject currentProject = mavenSession.getCurrentProject();
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(
new DefaultArtifact(currentProject.getGroupId() + ":" + currentProject.getArtifactId() + ":" + range));
rangeRequest.setRepositories(
RepositoryUtils.toRepos(mavenSession.getCurrentProject().getRemoteArtifactRepositories()));
VersionRangeResult rangeResult =
repositorySystem.resolveVersionRange(mavenSession.getRepositorySession(), rangeRequest);
return rangeResult.getVersions().stream()
.filter(version -> !ArtifactUtils.isSnapshot(version.toString()))
.collect(Collectors.toList());
}

private MavenProject buildMavenProject(String version) throws ProjectBuildingException {
MavenProject currentProject = mavenSession.getCurrentProject();
ProjectBuildingRequest buildRequest = new DefaultProjectBuildingRequest();
buildRequest.setLocalRepository(mavenSession.getLocalRepository());
buildRequest.setRemoteRepositories(mavenSession.getCurrentProject().getRemoteArtifactRepositories());
buildRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
buildRequest.setProcessPlugins(false);
buildRequest.setRepositoryMerging(ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT);
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)
.getProject();
}
}