Skip to content

Commit

Permalink
MSHADE-307 dropping useDefaultConfiguration to make the behavior by d…
Browse files Browse the repository at this point in the history
…efault + supporting SPI loading for Filter and ResourceTransformer to enable 'add and enable' behavior/auto configuration
  • Loading branch information
rmannibucau committed Dec 16, 2018
1 parent f96c2e2 commit ad66d55
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
87 changes: 46 additions & 41 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;

/**
Expand Down Expand Up @@ -367,15 +368,6 @@ public class ShadeMojo
@Parameter( defaultValue = "false" )
private boolean shadeTestJar;

/**
* Auto-configure the mojo to use common transformers (ServicesResourceTransformer, ManifestTransformer)
* and filters (META-INF/*.[SF,DSA,RSA]).
*
* @since 3.2.2
*/
@Parameter( defaultValue = "false" )
private boolean useDefaultConfiguration;

/**
* @since 1.6
*/
Expand Down Expand Up @@ -785,11 +777,6 @@ private List<ResourceTransformer> getResourceTransformers()

private List<ResourceTransformer> getDefaultResourceTransformers()
{
if ( !useDefaultConfiguration )
{
return Collections.emptyList();
}

final List<ResourceTransformer> transformers = new LinkedList<>();
if ( missTransformer( ServicesResourceTransformer.class ) )
{
Expand All @@ -801,6 +788,15 @@ private List<ResourceTransformer> getDefaultResourceTransformers()
getLog().debug( "Adding ManifestResourceTransformer transformer" );
transformers.add( new ManifestResourceTransformer() );
}
for ( final ResourceTransformer transformer : ServiceLoader.load( ResourceTransformer.class ) )
{
if ( !missTransformer( transformer.getClass() ) )
{
continue;
}
getLog().debug( "Adding " + transformer.getClass().getName() + " transformer" );
transformers.add( transformer );
}
return transformers;
}

Expand All @@ -823,8 +819,8 @@ private boolean missTransformer( final Class<?> type )
private List<Filter> getFilters()
throws MojoExecutionException
{
List<Filter> filters = new ArrayList<Filter>();
List<SimpleFilter> simpleFilters = new ArrayList<SimpleFilter>();
List<Filter> filters = new LinkedList<Filter>();
List<SimpleFilter> simpleFilters = new LinkedList<SimpleFilter>();
Map<Artifact, ArtifactId> artifacts = null;

if ( this.filters != null && this.filters.length > 0 )
Expand All @@ -848,39 +844,48 @@ private List<Filter> getFilters()
}
}

if ( useDefaultConfiguration )
// first check for backward compatibility this is not already configured explicitly
boolean addExclusion = true;
if ( this.filters != null )
{
// first check for backward compatibility this is not already configured explicitly
boolean addExclusion = true;
if ( this.filters != null )
for ( final ArchiveFilter filter : this.filters )
{
for ( final ArchiveFilter filter : this.filters )
if ( filter.getExcludes() != null
&& filter.getExcludes().contains( "META-INF/*.SF" )
&& filter.getExcludes().contains( "META-INF/*.DSA" )
&& filter.getExcludes().contains( "META-INF/*.RSA" )
&& "*:*".equals( filter.getArtifact() ) )
{
if ( filter.getExcludes() != null
&& filter.getExcludes().contains( "META-INF/*.SF" )
&& filter.getExcludes().contains( "META-INF/*.DSA" )
&& filter.getExcludes().contains( "META-INF/*.RSA" )
&& "*:*".equals( filter.getArtifact() ) )
{
addExclusion = false;
break;
}
addExclusion = false;
break;
}
}
if ( addExclusion )
{
getLog().debug( "Adding META-INF/*.SF, META-INF/*.DSA and META-INF/*.RSA exclusion" );
}
if ( addExclusion )
{
getLog().debug( "Adding META-INF/*.SF, META-INF/*.DSA and META-INF/*.RSA exclusion" );

if ( artifacts == null )
{
artifacts = getArtifactIds();
}
simpleFilters.add( new SimpleFilter(
getMatchingJars( artifacts , new ArtifactId( "*:*" ) ),
Collections.<String>emptySet(),
new HashSet<String>( Arrays.asList( "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA" ) ) ) );
if ( artifacts == null )
{
artifacts = getArtifactIds();
}
simpleFilters.add( new SimpleFilter(
getMatchingJars( artifacts , new ArtifactId( "*:*" ) ),
Collections.<String>emptySet(),
new HashSet<String>( Arrays.asList( "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA" ) ) ) );
}

for ( final Filter filter : ServiceLoader.load( Filter.class ) )
{
getLog().debug( "Adding " + filter.getClass().getName() + " filter" );
if ( SimpleFilter.class.isInstance( filter ) )
{
simpleFilters.add( SimpleFilter.class.cast( filter ) );
}
else
{
filters.add( filter );
}
}

filters.addAll( simpleFilters );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public void testDefaultConfiguration() throws Exception
{
final ShadeMojo shadeMojo = new ShadeMojo();
setProject(shadeMojo);
final Field useDefaultConfiguration = ShadeMojo.class.getDeclaredField("useDefaultConfiguration");
useDefaultConfiguration.setAccessible(true);
useDefaultConfiguration.set(shadeMojo, true);

// default transformers are present
final Method getResourceTransformers = ShadeMojo.class.getDeclaredMethod("getResourceTransformers");
Expand Down Expand Up @@ -198,10 +195,10 @@ public void testShadeWithFilter()
List<Filter> filters = (List<Filter>) getFilters.invoke( mojo);

// assertions - there must be one filter
assertEquals( 1, filters.size() );
assertEquals( 2, filters.size() );

// the filter must be able to filter the binary and the sources jar
Filter filter = filters.get( 0 );
Filter filter = filters.get( 1 ); // 0 is the built-in META-INF/*
assertTrue( filter.canFilter( new File( "myfaces-impl-2.0.1-SNAPSHOT.jar" ) ) ); // binary jar
assertTrue( filter.canFilter( new File( "myfaces-impl-2.0.1-SNAPSHOT-sources.jar" ) ) ); // sources jar
}
Expand Down

0 comments on commit ad66d55

Please sign in to comment.