diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java index 7acf7450..996a8572 100644 --- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java +++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java @@ -81,6 +81,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.ServiceLoader; import java.util.Set; /** @@ -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 */ @@ -785,11 +777,6 @@ private List getResourceTransformers() private List getDefaultResourceTransformers() { - if ( !useDefaultConfiguration ) - { - return Collections.emptyList(); - } - final List transformers = new LinkedList<>(); if ( missTransformer( ServicesResourceTransformer.class ) ) { @@ -801,6 +788,15 @@ private List 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; } @@ -823,8 +819,8 @@ private boolean missTransformer( final Class type ) private List getFilters() throws MojoExecutionException { - List filters = new ArrayList(); - List simpleFilters = new ArrayList(); + List filters = new LinkedList(); + List simpleFilters = new LinkedList(); Map artifacts = null; if ( this.filters != null && this.filters.length > 0 ) @@ -848,39 +844,48 @@ private List 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.emptySet(), - new HashSet( 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.emptySet(), + new HashSet( 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 ); diff --git a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java index cdb5ea2e..42f372fe 100644 --- a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java @@ -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"); @@ -198,10 +195,10 @@ public void testShadeWithFilter() List filters = (List) 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 }