Skip to content

Commit

Permalink
[MENFORCER-388] Extends RequirePluginVersions with banMavenDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Jul 22, 2021
1 parent ca73329 commit 1b8ca8f
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils;
import org.apache.maven.plugins.enforcer.utils.PluginWrapper;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
Expand Down Expand Up @@ -111,6 +112,11 @@ public class RequirePluginVersions
*/
private boolean banTimestamps = true;

/**
* @since 3.0.0
*/
private boolean banMavenDefaults = true;

/**
* The comma separated list of phases that should be used to find lifecycle plugin bindings. The default value is
* "clean,deploy,site".
Expand Down Expand Up @@ -176,6 +182,8 @@ public class RequirePluginVersions
/** The utils. */
private EnforcerRuleUtils utils;

private RuntimeInformation runtimeInformation;

@Override
public void execute( EnforcerRuleHelper helper )
throws EnforcerRuleException
Expand All @@ -190,6 +198,8 @@ public void execute( EnforcerRuleHelper helper )

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

runtimeInformation = helper.getComponent( RuntimeInformation.class );

DefaultLifecycles defaultLifeCycles = helper.getComponent( DefaultLifecycles.class );
lifecycles = defaultLifeCycles.getLifeCycles();

Expand Down Expand Up @@ -266,7 +276,8 @@ private void handleMessagesToTheUser( MavenProject project, List<Plugin> failure
throws EnforcerRuleException
{
StringBuilder newMsg = new StringBuilder();
newMsg.append( "Some plugins are missing valid versions:" );
newMsg.append( "Some plugins are missing valid versions or depend on Maven "
+ runtimeInformation.getMavenVersion() + " defaults:" );
handleBanMessages( newMsg );
newMsg.append( "\n" );
for ( Plugin plugin : failures )
Expand All @@ -281,13 +292,27 @@ private void handleMessagesToTheUser( MavenProject project, List<Plugin> failure

Plugin currentPlugin = findCurrentPlugin( plugin, project );

if ( currentPlugin != null )
if ( currentPlugin == null )
{
newMsg.append( currentPlugin.getVersion() );
newMsg.append( "unknown" );
}
else
{
newMsg.append( "unknown" );
newMsg.append( currentPlugin.getVersion() );

if ( PluginWrapper.isVersionFromDefaultLifecycleBindings( currentPlugin ).orElse( false ) )
{
newMsg.append( " via default lifecycle bindings" );
}
else
{
String msg = PluginWrapper.isVersionFromSuperpom( currentPlugin )
.filter( b -> b )
.map( t -> " via super POM" )
// for Maven 3.6.0 or before (MNG-6593 / MNG-6600)
.orElse( " via super POM or default lifecycle bindings" );
newMsg.append( msg );
}
}
}
catch ( Exception e )
Expand Down Expand Up @@ -966,7 +991,7 @@ private void getProfilePluginManagementPlugins( List<PluginWrapper> plugins, Mod
try
{
List<Plugin> modelPlugins = profile.getBuild().getPluginManagement().getPlugins();
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
}
catch ( NullPointerException e )
{
Expand All @@ -980,7 +1005,8 @@ private void getProfileReportingPlugins( List<PluginWrapper> plugins, Model mode
{
List<ReportPlugin> modelReportPlugins = profile.getReporting().getPlugins();
// add the reporting plugins
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ) ) );
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
banMavenDefaults ) );
}
catch ( NullPointerException e )
{
Expand All @@ -993,7 +1019,7 @@ private void getProfilePlugins( List<PluginWrapper> plugins, Model model, Profil
try
{
List<Plugin> modelPlugins = profile.getBuild().getPlugins();
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
}
catch ( NullPointerException e )
{
Expand All @@ -1006,7 +1032,7 @@ private void getPlugins( List<PluginWrapper> plugins, Model model )
try
{
List<Plugin> modelPlugins = model.getBuild().getPlugins();
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
}
catch ( NullPointerException e )
{
Expand All @@ -1019,7 +1045,7 @@ private void getPluginManagementPlugins( List<PluginWrapper> plugins, Model mode
try
{
List<Plugin> modelPlugins = model.getBuild().getPluginManagement().getPlugins();
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ) ) );
plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), banMavenDefaults ) );
}
catch ( NullPointerException e )
{
Expand All @@ -1033,7 +1059,8 @@ private void getReportingPlugins( List<PluginWrapper> plugins, Model model )
{
List<ReportPlugin> modelReportPlugins = model.getReporting().getPlugins();
// add the reporting plugins
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ) ) );
plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
banMavenDefaults ) );
}
catch ( NullPointerException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

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

private final InputLocationTracker locationTracker;

public static List<PluginWrapper> addAll( List<?> plugins )
public static List<PluginWrapper> addAll( List<? extends InputLocationTracker> plugins, boolean banMavenDefaults )
{
List<PluginWrapper> results = null;

if ( !plugins.isEmpty() )
{
results = new ArrayList<>( plugins.size() );
for ( Object o : plugins )
for ( InputLocationTracker o : plugins )
{
// null or true means it is most assumed a Maven default
if ( banMavenDefaults && ( isVersionFromDefaultLifecycleBindings( o ).orElse( true )
|| isVersionFromSuperpom( o ).orElse( true ) ) )
{
continue;
}

if ( o instanceof Plugin )
{
results.add( new PluginWrapper( (Plugin) o ) );
Expand All @@ -61,11 +69,46 @@ public static List<PluginWrapper> addAll( List<?> plugins )
results.add( new PluginWrapper( (ReportPlugin) o ) );
}
}

}
}
return results;
}

/**
*
* @param o either Plugin or ReportPlugin
* @return
*/
public static Optional<Boolean> isVersionFromDefaultLifecycleBindings( InputLocationTracker o )
{
InputLocation versionLocation = o.getLocation( "version" );
if ( versionLocation == null )
{
return Optional.empty();
}

String modelId = versionLocation.getSource().getModelId();
return Optional.of( modelId.startsWith( "org.apache.maven:maven-core:" )
&& modelId.endsWith( ":default-lifecycle-bindings" ) );
}

/**
*
* @param o either Plugin or ReportPlugin
* @return null if untraceable, otherwise its matching value
*/
public static Optional<Boolean> isVersionFromSuperpom( InputLocationTracker o )
{
InputLocation versionLocation = o.getLocation( "version" );
if ( versionLocation == null )
{
return Optional.empty();
}

String modelId = versionLocation.getSource().getModelId();
return Optional.of( modelId.startsWith( "org.apache.maven:maven-model-builder:" )
&& modelId.endsWith( ":super-pom" ) );
}

private PluginWrapper( Plugin plugin )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
Expand Down Expand Up @@ -189,10 +191,14 @@ public static EnforcerRuleHelper getHelper( MavenProject project, ExpressionEval
*/
public static Plugin newPlugin( String groupId, String artifactId, String version )
{
InputSource inputSource = new InputSource();
inputSource.setModelId( "unit" );

Plugin plugin = new Plugin();
plugin.setArtifactId( artifactId );
plugin.setGroupId( groupId );
plugin.setVersion( version );
plugin.setLocation( "version", new InputLocation( 0, 0, inputSource ) );
return plugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void testHasVersionSpecified()
plugins.add( EnforcerTestUtils.newPlugin( "group", "g-artifact", "1.0-12345678.123456-1" ) );


List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins );
List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, false );

RequirePluginVersions rule = new RequirePluginVersions();
rule.setBanLatest( false );
Expand Down Expand Up @@ -145,7 +145,7 @@ public void testHasVersionSpecifiedWithProperties()
plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "${}" ) );
plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "${ }" ) );

List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins );
List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, false );

RequirePluginVersions rule = new RequirePluginVersions();
rule.setBanLatest( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer359</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<build>
<pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
*/
File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequirePluginVersions failed with message:' )
assert buildLog.text.contains( 'Some plugins are missing valid versions: (LATEST RELEASE SNAPSHOT are not allowed)' )
assert buildLog.text.contains( "Some plugins are missing valid versions or depend on Maven ${mavenVersion} defaults: (LATEST RELEASE SNAPSHOT are not allowed)" )
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.buildResult=failure
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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>
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.enforcer</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<description>
</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requirePluginVersions>
<banSnapshots>false</banSnapshots>
</requirePluginVersions>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ under the License.
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer359</artifactId>
<version>1.0</version>
</parent>

<groupId>org.apache.maven.its.enforcer</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
Expand Down

0 comments on commit 1b8ca8f

Please sign in to comment.