From e90394b93101e80fa5994637127b05d1d3e33f4a Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Wed, 19 Dec 2018 10:18:10 +0100 Subject: [PATCH 1/2] MSHADE-307 defaults for transformers and filters --- .../maven/plugins/shade/mojo/ShadeMojo.java | 115 +++++++++--- .../plugins/shade/mojo/ShadeMojoTest.java | 174 ++++++++++++------ 2 files changed, 199 insertions(+), 90 deletions(-) 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 6b8d24a6..69cad48e 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 @@ -40,7 +40,9 @@ import org.apache.maven.plugins.shade.pom.PomWriter; import org.apache.maven.plugins.shade.relocation.Relocator; import org.apache.maven.plugins.shade.relocation.SimpleRelocator; +import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer; import org.apache.maven.plugins.shade.resource.ResourceTransformer; +import org.apache.maven.plugins.shade.resource.ServicesResourceTransformer; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; @@ -76,6 +78,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -764,12 +767,44 @@ private List getResourceTransformers() { if ( transformers == null ) { - return Collections.emptyList(); + return getDefaultResourceTransformers(); } return Arrays.asList( transformers ); } + private List getDefaultResourceTransformers() + { + final List transformers = new LinkedList<>(); + if ( missTransformer( ServicesResourceTransformer.class ) ) + { + getLog().debug( "Adding ServicesResourceTransformer transformer" ); + transformers.add( new ServicesResourceTransformer() ); + } + if ( missTransformer( ManifestResourceTransformer.class ) ) + { + getLog().debug( "Adding ManifestResourceTransformer transformer" ); + transformers.add( new ManifestResourceTransformer() ); + } + return transformers; + } + + private boolean missTransformer( final Class type ) + { + if ( transformers == null ) + { + return true; + } + for ( final ResourceTransformer transformer : transformers ) + { + if ( type.isInstance( transformer ) ) + { + return false; + } + } + return true; + } + private List getFilters() throws MojoExecutionException { @@ -778,39 +813,13 @@ private List getFilters() if ( this.filters != null && this.filters.length > 0 ) { - Map artifacts = new HashMap(); - - artifacts.put( project.getArtifact(), new ArtifactId( project.getArtifact() ) ); - - for ( Artifact artifact : project.getArtifacts() ) - { - artifacts.put( artifact, new ArtifactId( artifact ) ); - } + Map artifacts = getArtifactIds(); for ( ArchiveFilter filter : this.filters ) { ArtifactId pattern = new ArtifactId( filter.getArtifact() ); - Set jars = new HashSet(); - - for ( Map.Entry entry : artifacts.entrySet() ) - { - if ( entry.getValue().matches( pattern ) ) - { - Artifact artifact = entry.getKey(); - - jars.add( artifact.getFile() ); - - if ( createSourcesJar ) - { - File file = resolveArtifactSources( artifact ); - if ( file != null ) - { - jars.add( file ); - } - } - } - } + Set jars = getMatchingJars( artifacts, pattern ); if ( jars.isEmpty() ) { @@ -822,6 +831,16 @@ private List getFilters() simpleFilters.add( new SimpleFilter( jars, filter.getIncludes(), filter.getExcludes() ) ); } } + else if ( this.filters == null ) + { + getLog().debug( "Adding META-INF/*.SF, META-INF/*.DSA and META-INF/*.RSA exclusions" ); + + Map 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" ) ) ) ); + } filters.addAll( simpleFilters ); @@ -842,6 +861,44 @@ private List getFilters() return filters; } + private Set getMatchingJars( final Map artifacts, final ArtifactId pattern ) + { + final Set jars = new HashSet(); + + for ( final Map.Entry entry : artifacts.entrySet() ) + { + if ( entry.getValue().matches( pattern ) ) + { + final Artifact artifact = entry.getKey(); + + jars.add( artifact.getFile() ); + + if ( createSourcesJar ) + { + final File file = resolveArtifactSources( artifact ); + if ( file != null ) + { + jars.add( file ); + } + } + } + } + return jars; + } + + private Map getArtifactIds() + { + final Map artifacts = new HashMap(); + + artifacts.put( project.getArtifact(), new ArtifactId( project.getArtifact() ) ); + + for ( final Artifact artifact : project.getArtifacts() ) + { + artifacts.put( artifact, new ArtifactId( artifact ) ); + } + return artifacts; + } + private File shadedArtifactFileWithClassifier() { Artifact artifact = project.getArtifact(); 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 fa869c8f..e47180b3 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 @@ -29,6 +29,7 @@ import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -41,10 +42,13 @@ import org.apache.maven.plugins.shade.ShadeRequest; import org.apache.maven.plugins.shade.Shader; import org.apache.maven.plugins.shade.filter.Filter; +import org.apache.maven.plugins.shade.filter.SimpleFilter; import org.apache.maven.plugins.shade.relocation.Relocator; import org.apache.maven.plugins.shade.relocation.SimpleRelocator; import org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer; +import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer; import org.apache.maven.plugins.shade.resource.ResourceTransformer; +import org.apache.maven.plugins.shade.resource.ServicesResourceTransformer; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate; @@ -60,6 +64,48 @@ public class ShadeMojoTest extends PlexusTestCase { + public void testDefaultConfiguration() throws Exception + { + final ShadeMojo shadeMojo = new ShadeMojo(); + setProject(shadeMojo); + + // default transformers are present + final Method getResourceTransformers = ShadeMojo.class.getDeclaredMethod("getResourceTransformers"); + getResourceTransformers.setAccessible(true); + final List transformers = + List.class.cast(getResourceTransformers.invoke(shadeMojo)); + assertEquals(2, transformers.size()); + assertTrue(ServicesResourceTransformer.class.isInstance(transformers.get(0))); + assertTrue(ManifestResourceTransformer.class.isInstance(transformers.get(1))); + + // default exclusion is present + final Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters"); + getFilters.setAccessible(true); + final List filters = + List.class.cast(getFilters.invoke(shadeMojo)); + assertEquals(1, filters.size()); + + final Filter filter = filters.iterator().next(); + assertTrue(SimpleFilter.class.isInstance(filter)); + + final Field jars = filter.getClass().getDeclaredField("jars"); + jars.setAccessible(true); + assertEquals(1, Collection.class.cast(jars.get(filter)).size()); + + final Field excludes = filter.getClass().getDeclaredField("excludes"); + excludes.setAccessible(true); + final Collection excludesValues = Collection.class.cast(excludes.get(filter)); + assertEquals(3, excludesValues.size()); + for ( final String exclude : Arrays.asList( "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA" ) ) + { + assertTrue(exclude, excludesValues.contains(exclude) ); + } + + final Field includes = filter.getClass().getDeclaredField("includes"); + includes.setAccessible(true); + assertTrue(Collection.class.cast(includes.get(filter)).isEmpty()); + } + public void testShaderWithDefaultShadedPattern() throws Exception { @@ -124,67 +170,8 @@ public void testShadeWithFilter() createSourcesJar.setAccessible( true ); createSourcesJar.set( mojo, Boolean.TRUE ); - // configure artifactResolver (mocked) for mojo - ArtifactResolver mockArtifactResolver = new ArtifactResolver() - { - @Override - public ArtifactResult resolveArtifact( ProjectBuildingRequest req, final Artifact art ) - throws ArtifactResolverException - { - return new ArtifactResult() - { - - @Override - public Artifact getArtifact() - { - art.setResolved( true ); - String fileName = art.getArtifactId() + "-" + art.getVersion() - + ( art.getClassifier() != null ? "-" + art.getClassifier() : "" ) + ".jar"; - art.setFile( new File( fileName ) ); - return art; - } - }; - } - - @Override - public ArtifactResult resolveArtifact( ProjectBuildingRequest req, final ArtifactCoordinate coordinate ) - throws ArtifactResolverException - { - return new ArtifactResult() - { - - @Override - public Artifact getArtifact() - { - Artifact art = mock( Artifact.class ); - when( art.getGroupId() ).thenReturn( coordinate.getGroupId() ); - when( art.getArtifactId() ).thenReturn( coordinate.getArtifactId() ); - when( art.getType() ).thenReturn( coordinate.getExtension() ); - when( art.getClassifier() ).thenReturn( coordinate.getClassifier() ); - when( art.isResolved() ).thenReturn( true ); - String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() - + ( coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "" ) + ".jar"; - when( art.getFile() ).thenReturn( new File( fileName ) ); - return art; - } - }; - } - }; - Field artifactResolverField = ShadeMojo.class.getDeclaredField( "artifactResolver" ); - artifactResolverField.setAccessible( true ); - artifactResolverField.set( mojo, mockArtifactResolver ); - - // create and configure MavenProject - MavenProject project = new MavenProject(); - ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE ); - Artifact artifact = new DefaultArtifact( "org.apache.myfaces.core", "myfaces-impl", - VersionRange.createFromVersion( "2.0.1-SNAPSHOT" ), "compile", "jar", - null, artifactHandler ); - artifact = mockArtifactResolver.resolveArtifact( null, artifact ).getArtifact(); // setFile and setResolved - project.setArtifact( artifact ); - Field projectField = ShadeMojo.class.getDeclaredField( "project" ); - projectField.setAccessible( true ); - projectField.set( mojo, project ); + // setup a project + setProject(mojo); // create and configure the ArchiveFilter ArchiveFilter archiveFilter = new ArchiveFilter(); @@ -247,4 +234,69 @@ public void shaderWithPattern( String shadedPattern, File jar ) s.shade( shadeRequest ); } + private void setProject(final ShadeMojo mojo) throws Exception + { + // configure artifactResolver (mocked) for mojo + ArtifactResolver mockArtifactResolver = new ArtifactResolver() + { + @Override + public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final Artifact art ) + throws ArtifactResolverException + { + return new ArtifactResult() + { + + @Override + public Artifact getArtifact() + { + art.setResolved( true ); + String fileName = art.getArtifactId() + "-" + art.getVersion() + + ( art.getClassifier() != null ? "-" + art.getClassifier() : "" ) + ".jar"; + art.setFile( new File( fileName ) ); + return art; + } + }; + } + + @Override + public ArtifactResult resolveArtifact( ProjectBuildingRequest req, final ArtifactCoordinate coordinate ) + throws ArtifactResolverException + { + return new ArtifactResult() + { + + @Override + public Artifact getArtifact() + { + Artifact art = mock( Artifact.class ); + when( art.getGroupId() ).thenReturn( coordinate.getGroupId() ); + when( art.getArtifactId() ).thenReturn( coordinate.getArtifactId() ); + when( art.getType() ).thenReturn( coordinate.getExtension() ); + when( art.getClassifier() ).thenReturn( coordinate.getClassifier() ); + when( art.isResolved() ).thenReturn( true ); + String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() + + ( coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "" ) + ".jar"; + when( art.getFile() ).thenReturn( new File( fileName ) ); + return art; + } + }; + } + }; + Field artifactResolverField = ShadeMojo.class.getDeclaredField( "artifactResolver" ); + artifactResolverField.setAccessible( true ); + artifactResolverField.set( mojo, mockArtifactResolver ); + + // create and configure MavenProject + MavenProject project = new MavenProject(); + ArtifactHandler artifactHandler = (ArtifactHandler) lookup( ArtifactHandler.ROLE ); + Artifact artifact = new DefaultArtifact( "org.apache.myfaces.core", "myfaces-impl", + VersionRange.createFromVersion( "2.0.1-SNAPSHOT" ), "compile", "jar", + null, artifactHandler ); + artifact = mockArtifactResolver.resolveArtifact( null, artifact ).getArtifact(); // setFile and setResolved + project.setArtifact( artifact ); + Field projectField = ShadeMojo.class.getDeclaredField( "project" ); + projectField.setAccessible( true ); + projectField.set( mojo, project ); + } + } From b6a9bffcabed9ba5ab7448e0dfefe75960f02268 Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Wed, 19 Dec 2018 10:53:04 +0100 Subject: [PATCH 2/2] MSHADE-308 SPI support for Filter and ResourceTransformer --- pom.xml | 3 + src/it/spi-shade/invoker.properties | 18 +++++ src/it/spi-shade/pom.xml | 77 +++++++++++++++++++ src/it/spi-shade/verify.groovy | 31 ++++++++ src/it/spi/invoker.properties | 18 +++++ src/it/spi/pom.xml | 52 +++++++++++++ .../shade/it/spi/CustomTransformer.java | 51 ++++++++++++ ...plugins.shade.resource.ResourceTransformer | 14 ++++ .../maven/plugins/shade/mojo/ShadeMojo.java | 29 ++++++- .../examples/implicit-configuration.apt.vm | 68 ++++++++++++++++ src/site/apt/index.apt.vm | 2 + src/site/site.xml | 1 + 12 files changed, 361 insertions(+), 3 deletions(-) create mode 100644 src/it/spi-shade/invoker.properties create mode 100644 src/it/spi-shade/pom.xml create mode 100644 src/it/spi-shade/verify.groovy create mode 100644 src/it/spi/invoker.properties create mode 100644 src/it/spi/pom.xml create mode 100644 src/it/spi/src/main/java/org/apache/maven/plugins/shade/it/spi/CustomTransformer.java create mode 100644 src/it/spi/src/main/resources/META-INF/services/org.apache.maven.plugins.shade.resource.ResourceTransformer create mode 100644 src/site/apt/examples/implicit-configuration.apt.vm diff --git a/pom.xml b/pom.xml index cb10a847..4cb6dc7a 100644 --- a/pom.xml +++ b/pom.xml @@ -276,6 +276,9 @@ install + + spi/pom.xml + ${https.protocols} diff --git a/src/it/spi-shade/invoker.properties b/src/it/spi-shade/invoker.properties new file mode 100644 index 00000000..bdf5c615 --- /dev/null +++ b/src/it/spi-shade/invoker.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals=clean package diff --git a/src/it/spi-shade/pom.xml b/src/it/spi-shade/pom.xml new file mode 100644 index 00000000..6a1d70a6 --- /dev/null +++ b/src/it/spi-shade/pom.xml @@ -0,0 +1,77 @@ + + + + + + 4.0.0 + + + org.apache.maven.plugins.shade.its + shade-parent + 1.0 + ../setup-parent + + + spi-shade + 1.0-SNAPSHOT + + IT :: SPI :: Shade + + + 1.7 + 1.7 + + + + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + + + org.apache.maven.plugins + maven-shade-plugin + @project.version@ + tests + + + ${project.groupId} + spi + ${project.version} + + + + + create-shaded-artifact + package + + shade + + + false + + + + + + + diff --git a/src/it/spi-shade/verify.groovy b/src/it/spi-shade/verify.groovy new file mode 100644 index 00000000..febbe05a --- /dev/null +++ b/src/it/spi-shade/verify.groovy @@ -0,0 +1,31 @@ +import java.util.jar.JarFile + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +shade = new File(basedir, 'target/spi-shade-1.0-SNAPSHOT.jar') +assert shade.exists(); + +jar = new JarFile(shade) +entry = jar.getEntry('org.apache.maven.plugins.shade.it.spi.CustomTransformer') +assert entry != null +stream = jar.getInputStream(entry) +assert stream.text == 'executed' +jar.close() + +return true; \ No newline at end of file diff --git a/src/it/spi/invoker.properties b/src/it/spi/invoker.properties new file mode 100644 index 00000000..24178d35 --- /dev/null +++ b/src/it/spi/invoker.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals=clean install diff --git a/src/it/spi/pom.xml b/src/it/spi/pom.xml new file mode 100644 index 00000000..8f1bf775 --- /dev/null +++ b/src/it/spi/pom.xml @@ -0,0 +1,52 @@ + + + + + + 4.0.0 + + + org.apache.maven.plugins.shade.its + shade-parent + 1.0 + ../setup-parent + + + spi + 1.0-SNAPSHOT + + IT :: SPI + + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-shade-plugin + + 3.2.1 + provided + + + diff --git a/src/it/spi/src/main/java/org/apache/maven/plugins/shade/it/spi/CustomTransformer.java b/src/it/spi/src/main/java/org/apache/maven/plugins/shade/it/spi/CustomTransformer.java new file mode 100644 index 00000000..566227cc --- /dev/null +++ b/src/it/spi/src/main/java/org/apache/maven/plugins/shade/it/spi/CustomTransformer.java @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2006-2018 Talend Inc. - www.talend.com + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.maven.plugins.shade.it.spi; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; + +import org.apache.maven.plugins.shade.relocation.Relocator; +import org.apache.maven.plugins.shade.resource.ResourceTransformer; + +public class CustomTransformer implements ResourceTransformer { + @Override + public boolean canTransformResource( final String s ) { + return false; + } + + @Override + public void processResource( final String s, final InputStream inputStream, + final List list ) throws IOException { + // no-op + } + + @Override + public boolean hasTransformedResource() { + return true; + } + + @Override + public void modifyOutputStream( final JarOutputStream jarOutputStream ) throws IOException { + jarOutputStream.putNextEntry( new JarEntry( CustomTransformer.class.getName() ) ); + jarOutputStream.write( "executed".getBytes( StandardCharsets.UTF_8 ) ); + jarOutputStream.closeEntry(); + } +} diff --git a/src/it/spi/src/main/resources/META-INF/services/org.apache.maven.plugins.shade.resource.ResourceTransformer b/src/it/spi/src/main/resources/META-INF/services/org.apache.maven.plugins.shade.resource.ResourceTransformer new file mode 100644 index 00000000..15ff9128 --- /dev/null +++ b/src/it/spi/src/main/resources/META-INF/services/org.apache.maven.plugins.shade.resource.ResourceTransformer @@ -0,0 +1,14 @@ +# Copyright (C) 2006-2018 Talend Inc. - www.talend.com +#

+# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +#

+# http://www.apache.org/licenses/LICENSE-2.0 +#

+# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +org.apache.maven.plugins.shade.it.spi.CustomTransformer 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 69cad48e..974504a3 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; /** @@ -765,12 +766,22 @@ private List getRelocators() private List getResourceTransformers() { - if ( transformers == null ) + final List transformers; + if ( this.transformers == null ) + { + transformers = getDefaultResourceTransformers(); + } + else { - return getDefaultResourceTransformers(); + transformers = new ArrayList<>( Arrays.asList( this.transformers ) ); } - return Arrays.asList( transformers ); + for ( final ResourceTransformer transformer : ServiceLoader.load( ResourceTransformer.class ) ) + { + transformers.add( transformer ); + } + + return transformers; } private List getDefaultResourceTransformers() @@ -841,6 +852,18 @@ else if ( this.filters == null ) Collections.emptySet(), new HashSet<>( Arrays.asList( "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA" ) ) ) ); } + for ( final Filter filter : ServiceLoader.load( Filter.class ) ) + { + if ( SimpleFilter.class.isInstance( filter ) ) + { + simpleFilters.add ( SimpleFilter.class.cast( filter ) ); + } + else + { + filters.add( filter ); + } + } + filters.addAll( simpleFilters ); diff --git a/src/site/apt/examples/implicit-configuration.apt.vm b/src/site/apt/examples/implicit-configuration.apt.vm new file mode 100644 index 00000000..e4f69964 --- /dev/null +++ b/src/site/apt/examples/implicit-configuration.apt.vm @@ -0,0 +1,68 @@ + ------ + Maven Shade Plugin and implicit configuration + ------ + Romain Manni-Bucau + ------ + 2018-12-19 + ------ + +~~ Licensed to the Apache Software Foundation (ASF) under one +~~ or more contributor license agreements. See the NOTICE file +~~ distributed with this work for additional information +~~ regarding copyright ownership. The ASF licenses this file +~~ to you under the Apache License, Version 2.0 (the +~~ "License"); you may not use this file except in compliance +~~ with the License. You may obtain a copy of the License at +~~ +~~ http://www.apache.org/licenses/LICENSE-2.0 +~~ +~~ Unless required by applicable law or agreed to in writing, +~~ software distributed under the License is distributed on an +~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +~~ KIND, either express or implied. See the License for the +~~ specific language governing permissions and limitations +~~ under the License. + +Auto-registered configuration elements + + For framework and libraries writers it can be neat to skip to their user some registration. + This is why some extension points are supported. + + Assuming your configuration is the following one and that my-extension is a module implementing + a <<>> or <<>>: + ++----- + + ... + + + + org.apache.maven.plugins + maven-shade-plugin + ${project.version} + + + org.foo.bar + my-extension + 1.0 + + + + + package + + shade + + + + + + + ... + ++----- + + Then if you register through Java <<> mecanism your <<>> / <<>> + then Maven Shade Plugin will automatically add them to the configuration even if not references into + <<>> or <<>>. This way, if your extension does not have any configuration it will + be active just adding the plugin dependency. diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm index fb5e8662..62bc1a03 100644 --- a/src/site/apt/index.apt.vm +++ b/src/site/apt/index.apt.vm @@ -75,4 +75,6 @@ ${project.name} * {{{./examples/resource-transformers.html}Resource Transformers}} + * {{{./examples/implicit-configuration.html}Implicit Configuration}} + [] diff --git a/src/site/site.xml b/src/site/site.xml index cba65de0..a991b5a0 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -65,6 +65,7 @@ under the License. +