Skip to content

Commit cd74761

Browse files
committed
[MPLUGIN-511] add versions history requirements detection
1 parent d9f8d89 commit cd74761

File tree

7 files changed

+278
-31
lines changed

7 files changed

+278
-31
lines changed

maven-plugin-report-plugin/src/it/plugin-info-jdk/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
assert new File( basedir, 'property/target/site/plugin-info.html' ).text.contains( '<td>1.3</td>' )
2121
assert new File( basedir, 'propertyRelease/target/site/plugin-info.html' ).text.contains( '<td>8</td>' )
2222
assert new File( basedir, 'pluginManagement/target/site/plugin-info.html' ).text.contains( '<td>1.4</td>' )
23-
assert new File( basedir, 'plugin/target/site/plugin-info.html' ).text.contains( '<td>1.5</td>' )
23+
assert new File( basedir, 'plugin/target/site/plugin-info.html' ).text.contains( '<td>5</td>' )
2424

2525
return true;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = site
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-plugin-report-plugin</artifactId><!-- intentionally reuse plugin's groupId:artifactId to build its releases history -->
28+
<version>1.0-SNAPSHOT</version>
29+
<packaging>maven-plugin</packaging>
30+
31+
<name>MPLUGIN-511</name>
32+
<description>
33+
Test discovery of plugin requirements history.
34+
</description>
35+
36+
<properties>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.apache.maven</groupId>
43+
<artifactId>maven-plugin-api</artifactId>
44+
<version>@mavenVersion@</version>
45+
<scope>provided</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.apache.maven.plugin-tools</groupId>
49+
<artifactId>maven-plugin-annotations</artifactId>
50+
<version>@project.version@</version>
51+
<scope>provided</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<pluginManagement>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>@compilerPluginVersion@</version>
62+
</plugin>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-site-plugin</artifactId>
66+
<version>@sitePluginVersion@</version>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-plugin-plugin</artifactId>
71+
<version>@project.version@</version>
72+
<configuration>
73+
<goalPrefix>prefix</goalPrefix>
74+
</configuration>
75+
</plugin>
76+
</plugins>
77+
</pluginManagement>
78+
</build>
79+
80+
<reporting>
81+
<excludeDefaults>true</excludeDefaults>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-plugin-report-plugin</artifactId>
86+
<version>@project.version@</version>
87+
</plugin>
88+
</plugins>
89+
</reporting>
90+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org;
20+
21+
import org.apache.maven.plugin.AbstractMojo;
22+
import org.apache.maven.plugins.annotations.Execute;
23+
import org.apache.maven.plugins.annotations.LifecyclePhase;
24+
import org.apache.maven.plugins.annotations.Mojo;
25+
import org.apache.maven.plugins.annotations.Parameter;
26+
import org.apache.maven.plugins.annotations.ResolutionScope;
27+
28+
/**
29+
* Does nothing.
30+
*
31+
* @since 1.0
32+
* @deprecated You don't use test goals, do you?
33+
*/
34+
@Mojo(
35+
name = "noop",
36+
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
37+
requiresDependencyResolution = ResolutionScope.TEST,
38+
requiresDirectInvocation = true,
39+
requiresOnline = true,
40+
inheritByDefault = false,
41+
aggregator = true)
42+
@Execute(phase = LifecyclePhase.COMPILE)
43+
public class MyMojo extends AbstractMojo {
44+
45+
/**
46+
* This is a test.
47+
*/
48+
@SuppressWarnings("unused")
49+
@Parameter(required = true)
50+
private String required;
51+
52+
/**
53+
* This is a test.
54+
*
55+
* @since 1.1
56+
* @deprecated Just testing.
57+
*/
58+
@SuppressWarnings("unused")
59+
@Parameter(property = "string", defaultValue = "${project.version}/</markup-must-be-escaped>")
60+
private String string;
61+
62+
public void execute() {
63+
// intentional do nothing
64+
}
65+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
assert new File( basedir, 'target/site/noop-mojo.html' ).isFile()
21+
22+
def pluginInfo = new File( basedir, 'target/site/plugin-info.html' )
23+
assert pluginInfo.isFile()
24+
25+
assert pluginInfo.text.contains('3.11.0')
26+
assert pluginInfo.text.contains('3.10.2')
27+
assert pluginInfo.text.contains('3.9.0')
28+
assert pluginInfo.text.contains('3.7.0')
29+
assert pluginInfo.text.contains('>8<')

maven-plugin-report-plugin/src/main/java/org/apache/maven/plugin/plugin/report/PluginReport.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import java.io.Reader;
2424
import java.nio.file.Files;
2525
import java.util.ArrayList;
26+
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.Locale;
29+
import java.util.stream.Collectors;
2830

2931
import org.apache.maven.RepositoryUtils;
32+
import org.apache.maven.artifact.ArtifactUtils;
3033
import org.apache.maven.doxia.sink.Sink;
3134
import org.apache.maven.execution.MavenSession;
3235
import org.apache.maven.model.building.ModelBuildingRequest;
@@ -44,7 +47,6 @@
4447
import org.apache.maven.project.ProjectBuilder;
4548
import org.apache.maven.project.ProjectBuildingException;
4649
import org.apache.maven.project.ProjectBuildingRequest;
47-
import org.apache.maven.project.ProjectBuildingResult;
4850
import org.apache.maven.reporting.AbstractMavenReport;
4951
import org.apache.maven.reporting.MavenReportException;
5052
import org.apache.maven.rtinfo.RuntimeInformation;
@@ -108,6 +110,14 @@ public class PluginReport extends AbstractMavenReport {
108110
@Parameter
109111
private List<RequirementsHistory> requirementsHistories = new ArrayList<>();
110112

113+
/**
114+
* Plugin's version range for automatic detection of requirements history.
115+
*
116+
* @since 3.12.0
117+
*/
118+
@Parameter(defaultValue = "[0,)")
119+
private String requirementsHistoryDetectionRange;
120+
111121
@Component
112122
private RuntimeInformation rtInfo;
113123

@@ -174,6 +184,30 @@ protected void executeReport(Locale locale) throws MavenReportException {
174184
// Generate the mojos' documentation
175185
generateMojosDocumentation(pluginDescriptor, locale);
176186

187+
if (requirementsHistories.isEmpty()) {
188+
// detect requirements history
189+
String v = null;
190+
try {
191+
List<Version> versions = discoverVersions(requirementsHistoryDetectionRange);
192+
getLog().info("Detecting requirements history for " + requirementsHistoryDetectionRange + ": "
193+
+ versions.size());
194+
195+
Collections.reverse(versions);
196+
for (Version version : versions) {
197+
v = version.toString();
198+
MavenProject versionProject = buildMavenProject(v);
199+
RequirementsHistory requirements = RequirementsHistory.discoverRequirements(versionProject);
200+
requirementsHistories.add(requirements);
201+
getLog().info(" - " + requirements);
202+
}
203+
} catch (VersionRangeResolutionException vrre) {
204+
throw new MavenReportException(
205+
"Cannot resolve past versions " + requirementsHistoryDetectionRange, vrre);
206+
} catch (ProjectBuildingException pbe) {
207+
throw new MavenReportException("Cannot resolve MavenProject for version " + v, pbe);
208+
}
209+
}
210+
177211
// Write the overview
178212
PluginOverviewRenderer r = new PluginOverviewRenderer(
179213
getSink(), i18n, locale, getProject(), requirementsHistories, pluginDescriptor, hasExtensionsToLoad);
@@ -257,19 +291,21 @@ private void generateMojosDocumentation(PluginDescriptor pluginDescriptor, Local
257291
}
258292
}
259293

260-
private List<Version> discoverVersions() throws VersionRangeResolutionException {
294+
private List<Version> discoverVersions(String range) throws VersionRangeResolutionException {
261295
MavenProject currentProject = mavenSession.getCurrentProject();
262296
VersionRangeRequest rangeRequest = new VersionRangeRequest();
263297
rangeRequest.setArtifact(
264-
new DefaultArtifact(currentProject.getGroupId() + ":" + currentProject.getArtifactId() + ":[0,)"));
298+
new DefaultArtifact(currentProject.getGroupId() + ":" + currentProject.getArtifactId() + ":" + range));
265299
rangeRequest.setRepositories(
266300
RepositoryUtils.toRepos(mavenSession.getCurrentProject().getRemoteArtifactRepositories()));
267301
VersionRangeResult rangeResult =
268302
repositorySystem.resolveVersionRange(mavenSession.getRepositorySession(), rangeRequest);
269-
return rangeResult.getVersions();
303+
return rangeResult.getVersions().stream()
304+
.filter(version -> !ArtifactUtils.isSnapshot(version.toString()))
305+
.collect(Collectors.toList());
270306
}
271307

272-
private ProjectBuildingResult buildMavenProject(String version) throws ProjectBuildingException {
308+
private MavenProject buildMavenProject(String version) throws ProjectBuildingException {
273309
MavenProject currentProject = mavenSession.getCurrentProject();
274310
ProjectBuildingRequest buildRequest = new DefaultProjectBuildingRequest();
275311
buildRequest.setLocalRepository(mavenSession.getLocalRepository());
@@ -280,9 +316,11 @@ private ProjectBuildingResult buildMavenProject(String version) throws ProjectBu
280316
buildRequest.setSystemProperties(mavenSession.getSystemProperties());
281317
buildRequest.setUserProperties(mavenSession.getUserProperties());
282318
buildRequest.setRepositorySession(mavenSession.getRepositorySession());
283-
return projectBuilder.build(
284-
RepositoryUtils.toArtifact(new DefaultArtifact(
285-
currentProject.getGroupId() + ":" + currentProject.getArtifactId() + ":pom:" + version)),
286-
buildRequest);
319+
return projectBuilder
320+
.build(
321+
RepositoryUtils.toArtifact(new DefaultArtifact(currentProject.getGroupId() + ":"
322+
+ currentProject.getArtifactId() + ":pom:" + version)),
323+
buildRequest)
324+
.getProject();
287325
}
288326
}

0 commit comments

Comments
 (0)