Skip to content

Commit 1b8ca8f

Browse files
committed
[MENFORCER-388] Extends RequirePluginVersions with banMavenDefaults
1 parent ca73329 commit 1b8ca8f

File tree

9 files changed

+174
-16
lines changed

9 files changed

+174
-16
lines changed

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils;
6363
import org.apache.maven.plugins.enforcer.utils.PluginWrapper;
6464
import org.apache.maven.project.MavenProject;
65+
import org.apache.maven.rtinfo.RuntimeInformation;
6566
import org.apache.maven.settings.Settings;
6667
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
6768
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
@@ -111,6 +112,11 @@ public class RequirePluginVersions
111112
*/
112113
private boolean banTimestamps = true;
113114

115+
/**
116+
* @since 3.0.0
117+
*/
118+
private boolean banMavenDefaults = true;
119+
114120
/**
115121
* The comma separated list of phases that should be used to find lifecycle plugin bindings. The default value is
116122
* "clean,deploy,site".
@@ -176,6 +182,8 @@ public class RequirePluginVersions
176182
/** The utils. */
177183
private EnforcerRuleUtils utils;
178184

185+
private RuntimeInformation runtimeInformation;
186+
179187
@Override
180188
public void execute( EnforcerRuleHelper helper )
181189
throws EnforcerRuleException
@@ -190,6 +198,8 @@ public void execute( EnforcerRuleHelper helper )
190198

191199
project = (MavenProject) helper.evaluate( "${project}" );
192200

201+
runtimeInformation = helper.getComponent( RuntimeInformation.class );
202+
193203
DefaultLifecycles defaultLifeCycles = helper.getComponent( DefaultLifecycles.class );
194204
lifecycles = defaultLifeCycles.getLifeCycles();
195205

@@ -266,7 +276,8 @@ private void handleMessagesToTheUser( MavenProject project, List<Plugin> failure
266276
throws EnforcerRuleException
267277
{
268278
StringBuilder newMsg = new StringBuilder();
269-
newMsg.append( "Some plugins are missing valid versions:" );
279+
newMsg.append( "Some plugins are missing valid versions or depend on Maven "
280+
+ runtimeInformation.getMavenVersion() + " defaults:" );
270281
handleBanMessages( newMsg );
271282
newMsg.append( "\n" );
272283
for ( Plugin plugin : failures )
@@ -281,13 +292,27 @@ private void handleMessagesToTheUser( MavenProject project, List<Plugin> failure
281292

282293
Plugin currentPlugin = findCurrentPlugin( plugin, project );
283294

284-
if ( currentPlugin != null )
295+
if ( currentPlugin == null )
285296
{
286-
newMsg.append( currentPlugin.getVersion() );
297+
newMsg.append( "unknown" );
287298
}
288299
else
289300
{
290-
newMsg.append( "unknown" );
301+
newMsg.append( currentPlugin.getVersion() );
302+
303+
if ( PluginWrapper.isVersionFromDefaultLifecycleBindings( currentPlugin ).orElse( false ) )
304+
{
305+
newMsg.append( " via default lifecycle bindings" );
306+
}
307+
else
308+
{
309+
String msg = PluginWrapper.isVersionFromSuperpom( currentPlugin )
310+
.filter( b -> b )
311+
.map( t -> " via super POM" )
312+
// for Maven 3.6.0 or before (MNG-6593 / MNG-6600)
313+
.orElse( " via super POM or default lifecycle bindings" );
314+
newMsg.append( msg );
315+
}
291316
}
292317
}
293318
catch ( Exception e )
@@ -966,7 +991,7 @@ private void getProfilePluginManagementPlugins( List<PluginWrapper> plugins, Mod
966991
try
967992
{
968993
List<Plugin> modelPlugins = profile.getBuild().getPluginManagement().getPlugins();
969-
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
994+
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
970995
}
971996
catch ( NullPointerException e )
972997
{
@@ -980,7 +1005,8 @@ private void getProfileReportingPlugins( List<PluginWrapper> plugins, Model mode
9801005
{
9811006
List<ReportPlugin> modelReportPlugins = profile.getReporting().getPlugins();
9821007
// add the reporting plugins
983-
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ) ) );
1008+
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
1009+
banMavenDefaults ) );
9841010
}
9851011
catch ( NullPointerException e )
9861012
{
@@ -993,7 +1019,7 @@ private void getProfilePlugins( List<PluginWrapper> plugins, Model model, Profil
9931019
try
9941020
{
9951021
List<Plugin> modelPlugins = profile.getBuild().getPlugins();
996-
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
1022+
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
9971023
}
9981024
catch ( NullPointerException e )
9991025
{
@@ -1006,7 +1032,7 @@ private void getPlugins( List<PluginWrapper> plugins, Model model )
10061032
try
10071033
{
10081034
List<Plugin> modelPlugins = model.getBuild().getPlugins();
1009-
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
1035+
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
10101036
}
10111037
catch ( NullPointerException e )
10121038
{
@@ -1019,7 +1045,7 @@ private void getPluginManagementPlugins( List<PluginWrapper> plugins, Model mode
10191045
try
10201046
{
10211047
List<Plugin> modelPlugins = model.getBuild().getPluginManagement().getPlugins();
1022-
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
1048+
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
10231049
}
10241050
catch ( NullPointerException e )
10251051
{
@@ -1033,7 +1059,8 @@ private void getReportingPlugins( List<PluginWrapper> plugins, Model model )
10331059
{
10341060
List<ReportPlugin> modelReportPlugins = model.getReporting().getPlugins();
10351061
// add the reporting plugins
1036-
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ) ) );
1062+
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
1063+
banMavenDefaults ) );
10371064
}
10381065
catch ( NullPointerException e )
10391066
{

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.Optional;
2425

2526
import org.apache.maven.model.InputLocation;
2627
import org.apache.maven.model.InputLocationTracker;
@@ -41,15 +42,22 @@ public class PluginWrapper
4142

4243
private final InputLocationTracker locationTracker;
4344

44-
public static List<PluginWrapper> addAll( List<?> plugins )
45+
public static List<PluginWrapper> addAll( List<? extends InputLocationTracker> plugins, boolean banMavenDefaults )
4546
{
4647
List<PluginWrapper> results = null;
4748

4849
if ( !plugins.isEmpty() )
4950
{
5051
results = new ArrayList<>( plugins.size() );
51-
for ( Object o : plugins )
52+
for ( InputLocationTracker o : plugins )
5253
{
54+
// null or true means it is most assumed a Maven default
55+
if ( banMavenDefaults && ( isVersionFromDefaultLifecycleBindings( o ).orElse( true )
56+
|| isVersionFromSuperpom( o ).orElse( true ) ) )
57+
{
58+
continue;
59+
}
60+
5361
if ( o instanceof Plugin )
5462
{
5563
results.add( new PluginWrapper( (Plugin) o ) );
@@ -61,11 +69,46 @@ public static List<PluginWrapper> addAll( List<?> plugins )
6169
results.add( new PluginWrapper( (ReportPlugin) o ) );
6270
}
6371
}
64-
6572
}
6673
}
6774
return results;
6875
}
76+
77+
/**
78+
*
79+
* @param o either Plugin or ReportPlugin
80+
* @return
81+
*/
82+
public static Optional<Boolean> isVersionFromDefaultLifecycleBindings( InputLocationTracker o )
83+
{
84+
InputLocation versionLocation = o.getLocation( "version" );
85+
if ( versionLocation == null )
86+
{
87+
return Optional.empty();
88+
}
89+
90+
String modelId = versionLocation.getSource().getModelId();
91+
return Optional.of( modelId.startsWith( "org.apache.maven:maven-core:" )
92+
&& modelId.endsWith( ":default-lifecycle-bindings" ) );
93+
}
94+
95+
/**
96+
*
97+
* @param o either Plugin or ReportPlugin
98+
* @return null if untraceable, otherwise its matching value
99+
*/
100+
public static Optional<Boolean> isVersionFromSuperpom( InputLocationTracker o )
101+
{
102+
InputLocation versionLocation = o.getLocation( "version" );
103+
if ( versionLocation == null )
104+
{
105+
return Optional.empty();
106+
}
107+
108+
String modelId = versionLocation.getSource().getModelId();
109+
return Optional.of( modelId.startsWith( "org.apache.maven:maven-model-builder:" )
110+
&& modelId.endsWith( ":super-pom" ) );
111+
}
69112

70113
private PluginWrapper( Plugin plugin )
71114
{

enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.maven.execution.MavenExecutionRequest;
3535
import org.apache.maven.execution.MavenExecutionResult;
3636
import org.apache.maven.execution.MavenSession;
37+
import org.apache.maven.model.InputLocation;
38+
import org.apache.maven.model.InputSource;
3739
import org.apache.maven.model.Plugin;
3840
import org.apache.maven.plugin.MojoExecution;
3941
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
@@ -189,10 +191,14 @@ public static EnforcerRuleHelper getHelper( MavenProject project, ExpressionEval
189191
*/
190192
public static Plugin newPlugin( String groupId, String artifactId, String version )
191193
{
194+
InputSource inputSource = new InputSource();
195+
inputSource.setModelId( "unit" );
196+
192197
Plugin plugin = new Plugin();
193198
plugin.setArtifactId( artifactId );
194199
plugin.setGroupId( groupId );
195200
plugin.setVersion( version );
201+
plugin.setLocation( "version", new InputLocation( 0, 0, inputSource ) );
196202
return plugin;
197203
}
198204
}

enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void testHasVersionSpecified()
8080
plugins.add( EnforcerTestUtils.newPlugin( "group", "g-artifact", "1.0-12345678.123456-1" ) );
8181

8282

83-
List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins );
83+
List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, false );
8484

8585
RequirePluginVersions rule = new RequirePluginVersions();
8686
rule.setBanLatest( false );
@@ -145,7 +145,7 @@ public void testHasVersionSpecifiedWithProperties()
145145
plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "${}" ) );
146146
plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "${ }" ) );
147147

148-
List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins );
148+
List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, false );
149149

150150
RequirePluginVersions rule = new RequirePluginVersions();
151151
rule.setBanLatest( false );

maven-enforcer-plugin/src/it/mrm/repository/menforcer359-1.0.pom

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
2424
<artifactId>menforcer359</artifactId>
2525
<version>1.0</version>
26+
<packaging>pom</packaging>
2627

2728
<build>
2829
<pluginManagement>

maven-enforcer-plugin/src/it/projects/require-plugin-versions-ci/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
*/
1919
File buildLog = new File( basedir, 'build.log' )
2020
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequirePluginVersions failed with message:' )
21-
assert buildLog.text.contains( 'Some plugins are missing valid versions: (LATEST RELEASE SNAPSHOT are not allowed)' )
21+
assert buildLog.text.contains( "Some plugins are missing valid versions or depend on Maven ${mavenVersion} defaults: (LATEST RELEASE SNAPSHOT are not allowed)" )
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.buildResult=failure
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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>
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.its.enforcer</groupId>
26+
<artifactId>test</artifactId>
27+
<version>1.0</version>
28+
<packaging>pom</packaging>
29+
30+
<description>
31+
</description>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-enforcer-plugin</artifactId>
38+
<version>@project.version@</version>
39+
<executions>
40+
<execution>
41+
<id>test</id>
42+
<goals>
43+
<goal>enforce</goal>
44+
</goals>
45+
<configuration>
46+
<rules>
47+
<requirePluginVersions>
48+
<banSnapshots>false</banSnapshots>
49+
</requirePluginVersions>
50+
</rules>
51+
</configuration>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>

maven-enforcer-plugin/src/it/projects/require-plugin-versions_inherit/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ under the License.
2222
<project>
2323
<modelVersion>4.0.0</modelVersion>
2424

25+
<parent>
26+
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
27+
<artifactId>menforcer359</artifactId>
28+
<version>1.0</version>
29+
</parent>
30+
2531
<groupId>org.apache.maven.its.enforcer</groupId>
2632
<artifactId>test</artifactId>
2733
<version>1.0</version>

0 commit comments

Comments
 (0)