[MNG-7457] Warn about deprecated plugin Mojo

This commit is contained in:
Slawomir Jaranowski 2022-04-23 21:55:29 +02:00
parent 9ac2d08dc7
commit 702f52d0ea
1 changed files with 38 additions and 16 deletions

View File

@ -24,6 +24,7 @@ import javax.inject.Singleton;
import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.shared.utils.logging.MessageBuilder;
import org.apache.maven.shared.utils.logging.MessageUtils; import org.apache.maven.shared.utils.logging.MessageUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
@ -32,7 +33,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Print warnings if deprecated params of plugin are used in configuration. * Print warnings if deprecated mojo or parameters of plugin are used in configuration.
* *
* @author Slawomir Jaranowski * @author Slawomir Jaranowski
*/ */
@ -52,6 +53,11 @@ class DeprecatedPluginValidator implements MavenPluginConfigurationValidator
return; return;
} }
if ( mojoDescriptor.getDeprecated() != null )
{
logDeprecatedMojo( mojoDescriptor );
}
if ( mojoDescriptor.getParameters() == null ) if ( mojoDescriptor.getParameters() == null )
{ {
return; return;
@ -64,19 +70,19 @@ class DeprecatedPluginValidator implements MavenPluginConfigurationValidator
} }
private static void checkParameter( Parameter parameter, private static void checkParameter( Parameter parameter,
PlexusConfiguration pomConfiguration, PlexusConfiguration pomConfiguration,
ExpressionEvaluator expressionEvaluator ) ExpressionEvaluator expressionEvaluator )
{ {
PlexusConfiguration config = pomConfiguration.getChild( parameter.getName(), false ); PlexusConfiguration config = pomConfiguration.getChild( parameter.getName(), false );
if ( isValueSet( config, expressionEvaluator ) ) if ( isValueSet( config, expressionEvaluator ) )
{ {
logDeprecateWarn( parameter ); logDeprecatedParameter( parameter );
} }
} }
private static boolean isValueSet( PlexusConfiguration config, private static boolean isValueSet( PlexusConfiguration config,
ExpressionEvaluator expressionEvaluator ) ExpressionEvaluator expressionEvaluator )
{ {
if ( config == null ) if ( config == null )
{ {
@ -112,22 +118,38 @@ class DeprecatedPluginValidator implements MavenPluginConfigurationValidator
return false; return false;
} }
private static void logDeprecateWarn( Parameter parameter ) private void logDeprecatedMojo( MojoDescriptor mojoDescriptor )
{ {
StringBuilder sb = new StringBuilder(); String message = MessageUtils.buffer()
sb.append( "Parameter '" ); .warning( "Goal '" )
sb.append( parameter.getName() ); .warning( mojoDescriptor.getGoal() )
sb.append( '\'' ); .warning( "' is deprecated: " )
.warning( mojoDescriptor.getDeprecated() )
.toString();
LOGGER.warn( message );
}
private static void logDeprecatedParameter( Parameter parameter )
{
MessageBuilder messageBuilder = MessageUtils.buffer()
.warning( "Parameter '" )
.warning( parameter.getName() )
.warning( '\'' );
if ( parameter.getExpression() != null ) if ( parameter.getExpression() != null )
{ {
String userProperty = parameter.getExpression().replace( "${", "'" ).replace( '}', '\'' ); String userProperty = parameter.getExpression().replace( "${", "'" ).replace( '}', '\'' );
sb.append( " (user property " ); messageBuilder
sb.append( userProperty ); .warning( " (user property " )
sb.append( ")" ); .warning( userProperty )
.warning( ")" );
} }
sb.append( " is deprecated: " );
sb.append( parameter.getDeprecated() );
LOGGER.warn( MessageUtils.buffer().warning( sb.toString() ).toString() ); messageBuilder
.warning( " is deprecated: " )
.warning( parameter.getDeprecated() );
LOGGER.warn( messageBuilder.toString() );
} }
} }