Skip to content

Commit

Permalink
[MNG-5222] - Maven 3 no longer logs warnings about deprecated plugin
Browse files Browse the repository at this point in the history
parameters

- Added warning when setting deprecated parameter with value different
than the default.
- Stop using deprecated DebugConfigurationListener class.
  • Loading branch information
belingueres committed Sep 9, 2019
1 parent 71eafc4 commit 2a25e06
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.maven.model.Plugin;
import org.apache.maven.monitor.logging.DefaultLog;
import org.apache.maven.plugin.ContextEnabled;
import org.apache.maven.plugin.DebugConfigurationListener;
import org.apache.maven.plugin.ExtensionRealmCache;
import org.apache.maven.plugin.InvalidPluginDescriptorException;
import org.apache.maven.plugin.MavenPluginManager;
Expand Down Expand Up @@ -67,7 +66,6 @@
import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.ComponentConfigurator;
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.codehaus.plexus.component.repository.ComponentDescriptor;
Expand Down Expand Up @@ -624,10 +622,8 @@ private void populatePluginFields( Object mojo, MojoDescriptor mojoDescriptor, C
// so that this method could entirely be handled by a plexus lookup?
configurator = container.lookup( ComponentConfigurator.class, configuratorId );

ConfigurationListener listener = new DebugConfigurationListener( logger );

ValidatingConfigurationListener validator =
new ValidatingConfigurationListener( mojo, mojoDescriptor, listener );
new ValidatingConfigurationListener( mojo, mojoDescriptor, logger );

logger.debug(
"Configuring mojo '" + mojoDescriptor.getId() + "' with " + configuratorId + " configurator -->" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
* under the License.
*/

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.maven.plugin.descriptor.MojoDescriptor;
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.logging.Logger;

/**
* A configuration listener to help validate the plugin configuration. For instance, check for required but missing
Expand All @@ -39,14 +42,17 @@ class ValidatingConfigurationListener

private final Object mojo;

private final ConfigurationListener delegate;
private final MojoDescriptor mojoDescriptor;

private final Logger logger;

private final Map<String, Parameter> missingParameters;

ValidatingConfigurationListener( Object mojo, MojoDescriptor mojoDescriptor, ConfigurationListener delegate )
ValidatingConfigurationListener( Object mojo, MojoDescriptor mojoDescriptor, Logger logger )
{
this.mojo = mojo;
this.delegate = delegate;
this.mojoDescriptor = mojoDescriptor;
this.logger = logger;
this.missingParameters = new HashMap<>();

if ( mojoDescriptor.getParameters() != null )
Expand All @@ -68,21 +74,31 @@ public Collection<Parameter> getMissingParameters()

public void notifyFieldChangeUsingSetter( String fieldName, Object value, Object target )
{
delegate.notifyFieldChangeUsingSetter( fieldName, value, target );
if ( logger.isDebugEnabled() )
{
logger.debug( " (s) " + fieldName + " = " + toString( value ) );
}

if ( mojo == target )
{
notify( fieldName, value );

warnDeprecated( fieldName, value );
}
}

public void notifyFieldChangeUsingReflection( String fieldName, Object value, Object target )
{
delegate.notifyFieldChangeUsingReflection( fieldName, value, target );
if ( logger.isDebugEnabled() )
{
logger.debug( " (f) " + fieldName + " = " + toString( value ) );
}

if ( mojo == target )
{
notify( fieldName, value );

warnDeprecated( fieldName, value );
}
}

Expand All @@ -94,4 +110,57 @@ private void notify( String fieldName, Object value )
}
}

private void warnDeprecated( String fieldName, Object value )
{
Parameter parameter = mojoDescriptor.getParameterMap().get( fieldName );
String deprecated = parameter.getDeprecated();
if ( deprecated != null && !deprecated.isEmpty() )
{
if ( !toString( value ).equals( toString( parameter.getDefaultValue() ) ) )
{
StringBuilder sb = new StringBuilder( " Parameter '" );
sb.append( fieldName ).append( '\'' );
if ( parameter.getExpression() != null )
{
String userParam = parameter.getExpression().replace( "${", "'" ).replace( '}', '\'' );
sb.append( " (User Parameter " ).append( userParam ).append( ")" );
}
sb.append( " is deprecated. " ).append( deprecated );

logger.warn( MessageUtils.buffer().warning( sb.toString() ).toString() );
}
}
}

/**
* Creates a human-friendly string representation of the specified object.
*
* @param obj The object to create a string representation for, may be <code>null</code>.
* @return The string representation, never <code>null</code>.
*/
private String toString( Object obj )
{
String str;
if ( obj != null && obj.getClass().isArray() )
{
int n = Array.getLength( obj );
StringBuilder buf = new StringBuilder( 256 );
buf.append( '[' );
for ( int i = 0; i < n; i++ )
{
if ( i > 0 )
{
buf.append( ", " );
}
buf.append( String.valueOf( Array.get( obj, i ) ) );
}
buf.append( ']' );
str = buf.toString();
}
else
{
str = String.valueOf( obj );
}
return str;
}
}

0 comments on commit 2a25e06

Please sign in to comment.