From 54f9d91ebb05ff8a051272c4e91dca4ee4984d80 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 21 May 2022 16:11:35 +0200 Subject: [PATCH] [MNG-7468] Check unsupported plugins parameters in configuration --- .../DefaultMojoExecutionConfigurator.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java index 89db25bc64..298562ded3 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java @@ -20,6 +20,9 @@ package org.apache.maven.lifecycle.internal; */ import java.util.Collection; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.inject.Named; import javax.inject.Singleton; @@ -28,9 +31,17 @@ import org.apache.maven.lifecycle.MojoExecutionConfigurator; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.project.MavenProject; +import org.apache.maven.shared.utils.logging.MessageBuilder; +import org.apache.maven.shared.utils.logging.MessageUtils; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static java.util.Arrays.stream; /** * @since 3.3.1, MNG-5753 @@ -40,6 +51,7 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; public class DefaultMojoExecutionConfigurator implements MojoExecutionConfigurator { + private final Logger logger = LoggerFactory.getLogger( getClass() ); @Override public void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig ) @@ -76,6 +88,8 @@ public class DefaultMojoExecutionConfigurator mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration ); mojoExecution.setConfiguration( mojoConfiguration ); + + checkUnknownMojoConfigurationParameters( mojoExecution ); } } @@ -108,4 +122,77 @@ public class DefaultMojoExecutionConfigurator return null; } + private void checkUnknownMojoConfigurationParameters( MojoExecution mojoExecution ) + { + if ( mojoExecution.getConfiguration() == null || mojoExecution.getConfiguration().getChildCount() == 0 ) + { + return; + } + + MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); + + // in first step get parameter names of current goal + Set parametersNamesGoal = mojoDescriptor.getParameters().stream() + .flatMap( this::getParameterNames ) + .collect( Collectors.toSet() ); + + Set unknownParameters = getUnknownParameters( mojoExecution, parametersNamesGoal ); + + if ( unknownParameters.isEmpty() ) + { + return; + } + + // second step get parameter names of all plugin goals + Set parametersNamesAll = mojoDescriptor.getPluginDescriptor().getMojos().stream() + .flatMap( m -> m.getParameters().stream() ) + .flatMap( this::getParameterNames ) + .collect( Collectors.toSet() ); + + unknownParameters = getUnknownParameters( mojoExecution, parametersNamesAll ); + + unknownParameters.forEach( + name -> + { + MessageBuilder messageBuilder = MessageUtils.buffer() + .warning( "Parameter '" ) + .warning( name ) + .warning( "' is unknown for plugin '" ) + .warning( mojoExecution.getArtifactId() ).warning( ":" ) + .warning( mojoExecution.getVersion() ).warning( ":" ) + .warning( mojoExecution.getGoal() ); + + if ( mojoExecution.getExecutionId() != null ) + { + messageBuilder.warning( " (" ); + messageBuilder.warning( mojoExecution.getExecutionId() ); + messageBuilder.warning( ")" ); + } + + messageBuilder.warning( "'" ); + + logger.warn( messageBuilder.toString() ); + } ); + } + + private Stream getParameterNames( Parameter parameter ) + { + if ( parameter.getAlias() != null ) + { + return Stream.of( parameter.getName(), parameter.getAlias() ); + } + else + { + return Stream.of( parameter.getName() ); + } + } + + private Set getUnknownParameters( MojoExecution mojoExecution, Set parameters ) + { + return stream( mojoExecution.getConfiguration().getChildren() ) + .map( Xpp3Dom::getName ) + .filter( name -> !parameters.contains( name ) ) + .collect( Collectors.toSet() ); + } + }