[MNG-5222] Maven 3 no longer logs warnings about deprecated plugin parameters

- Added warning when setting deprecated parameter with value different
than the default.
- Changed Logger to SLF4J.
This commit is contained in:
Gabriel Belingueres 2019-09-09 00:36:13 -03:00 committed by Slawomir Jaranowski
parent bdb35f2deb
commit c99028b9fb
2 changed files with 115 additions and 2 deletions

View File

@ -636,7 +636,7 @@ public class DefaultMavenPluginManager
ConfigurationListener listener = new DebugConfigurationListener( logger ); ConfigurationListener listener = new DebugConfigurationListener( logger );
ValidatingConfigurationListener validator = ValidatingConfigurationListener validator =
new ValidatingConfigurationListener( mojo, mojoDescriptor, listener ); new ValidatingConfigurationListener( mojo, mojoDescriptor, listener, expressionEvaluator );
logger.debug( logger.debug(
"Configuring mojo '" + mojoDescriptor.getId() + "' with " + configuratorId + " configurator -->" ); "Configuring mojo '" + mojoDescriptor.getId() + "' with " + configuratorId + " configurator -->" );

View File

@ -19,13 +19,22 @@ package org.apache.maven.plugin.internal;
* under the License. * under the License.
*/ */
import java.lang.reflect.Array;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
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.MessageUtils;
import org.codehaus.plexus.component.configurator.ConfigurationListener; import org.codehaus.plexus.component.configurator.ConfigurationListener;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* A configuration listener to help validate the plugin configuration. For instance, check for required but missing * A configuration listener to help validate the plugin configuration. For instance, check for required but missing
@ -36,17 +45,25 @@ import org.codehaus.plexus.component.configurator.ConfigurationListener;
class ValidatingConfigurationListener class ValidatingConfigurationListener
implements ConfigurationListener implements ConfigurationListener
{ {
private static final Logger LOGGER = LoggerFactory.getLogger( ValidatingConfigurationListener.class );
private final Object mojo; private final Object mojo;
private final ConfigurationListener delegate; private final ConfigurationListener delegate;
private final MojoDescriptor mojoDescriptor;
private final ExpressionEvaluator expressionEvaluator;
private final Map<String, Parameter> missingParameters; private final Map<String, Parameter> missingParameters;
ValidatingConfigurationListener( Object mojo, MojoDescriptor mojoDescriptor, ConfigurationListener delegate ) ValidatingConfigurationListener( Object mojo, MojoDescriptor mojoDescriptor, ConfigurationListener delegate,
ExpressionEvaluator expressionEvaluator )
{ {
this.mojo = mojo; this.mojo = mojo;
this.delegate = delegate; this.delegate = delegate;
this.mojoDescriptor = mojoDescriptor;
this.expressionEvaluator = expressionEvaluator;
this.missingParameters = new HashMap<>(); this.missingParameters = new HashMap<>();
if ( mojoDescriptor.getParameters() != null ) if ( mojoDescriptor.getParameters() != null )
@ -92,6 +109,102 @@ class ValidatingConfigurationListener
{ {
missingParameters.remove( fieldName ); missingParameters.remove( fieldName );
} }
if ( LOGGER.isWarnEnabled() )
{
warnDeprecated( fieldName, value );
}
} }
private void warnDeprecated( String fieldName, Object value )
{
Parameter parameter = mojoDescriptor.getParameterMap().get( fieldName );
String deprecated = parameter.getDeprecated();
if ( deprecated != null )
{
Object defaultValue = evaluateValue( parameter.getDefaultValue() );
if ( !Objects.equals( toString( value ), defaultValue ) )
{
StringBuilder sb = new StringBuilder();
sb.append( " Parameter '" );
sb.append( fieldName );
sb.append( '\'' );
if ( parameter.getExpression() != null )
{
String userProperty = parameter.getExpression().replace( "${", "'" ).replace( '}', '\'' );
sb.append( " (user property " );
sb.append( userProperty );
sb.append( ")" );
}
sb.append( " is deprecated: " );
sb.append( deprecated );
LOGGER.warn( MessageUtils.buffer().warning( sb.toString() ).toString() );
}
}
}
private Object evaluateValue( String value )
{
try
{
return expressionEvaluator.evaluate( value );
}
catch ( ExpressionEvaluationException e )
{
// should not happen here
}
return value;
}
/**
* Creates a string representation of the specified object for comparing with default values.
*
* @param obj The object to create a string representation for, may be <code>null</code>.
* @return The string representation, null for empty arrays / collections.
*/
private static String toString( Object obj )
{
String str;
if ( obj != null && obj.getClass().isArray() )
{
int n = Array.getLength( obj );
if ( n == 0 )
{
str = null;
}
else
{
StringJoiner sj = new StringJoiner( "," );
for ( int i = 0; i < n; i++ )
{
sj.add( String.valueOf( Array.get( obj, i ) ) );
}
str = sj.toString();
}
}
else if ( obj instanceof Collection )
{
Collection<?> collection = (Collection<?>) obj;
if ( collection.isEmpty() )
{
str = null;
}
else
{
Iterator<?> it = collection.iterator();
StringJoiner sj = new StringJoiner( "," );
while ( it.hasNext() )
{
sj.add( String.valueOf( it.next() ) );
}
str = sj.toString();
}
}
else
{
str = String.valueOf( obj );
}
return str;
}
} }