Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHADE-307] defaults for transformers and filters #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 86 additions & 29 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -764,12 +767,44 @@ private List<ResourceTransformer> getResourceTransformers()
{
if ( transformers == null )
{
return Collections.emptyList();
return getDefaultResourceTransformers();
}

return Arrays.asList( transformers );
}

private List<ResourceTransformer> getDefaultResourceTransformers()
{
final List<ResourceTransformer> transformers = new LinkedList<>();
if ( missTransformer( ServicesResourceTransformer.class ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDefaultResourceTransformers() is only called when transformers is null, so no need for missTransformer()-call

{
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<Filter> getFilters()
throws MojoExecutionException
{
Expand All @@ -778,39 +813,13 @@ private List<Filter> getFilters()

if ( this.filters != null && this.filters.length > 0 )
{
Map<Artifact, ArtifactId> artifacts = new HashMap<Artifact, ArtifactId>();

artifacts.put( project.getArtifact(), new ArtifactId( project.getArtifact() ) );

for ( Artifact artifact : project.getArtifacts() )
{
artifacts.put( artifact, new ArtifactId( artifact ) );
}
Map<Artifact, ArtifactId> artifacts = getArtifactIds();

for ( ArchiveFilter filter : this.filters )
{
ArtifactId pattern = new ArtifactId( filter.getArtifact() );

Set<File> jars = new HashSet<File>();

for ( Map.Entry<Artifact, ArtifactId> 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<File> jars = getMatchingJars( artifacts, pattern );

if ( jars.isEmpty() )
{
Expand All @@ -822,6 +831,16 @@ private List<Filter> 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" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a method getDefaultFilters()


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

filters.addAll( simpleFilters );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect a NPE here with the default filters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure i'm following, this is not this.filters but the list created in the method


Expand All @@ -842,6 +861,44 @@ private List<Filter> getFilters()
return filters;
}

private Set<File> getMatchingJars( final Map<Artifact, ArtifactId> artifacts, final ArtifactId pattern )
{
final Set<File> jars = new HashSet<File>();

for ( final Map.Entry<Artifact, ArtifactId> 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<Artifact, ArtifactId> getArtifactIds()
{
final Map<Artifact, ArtifactId> artifacts = new HashMap<Artifact, ArtifactId>();

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();
Expand Down
13 changes: 13 additions & 0 deletions src/site/apt/examples/includes-excludes.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ Selecting Contents for Uber JAR
was introduced in plugin version 1.3. It excludes all signature related files from every artifact, regardless of its
group or artifact id.

Note that if you didn't specify a not null <<<filters>>> value then the following setup will be done:

+----
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
+----

Besides user-specified filters, the plugin can also be configured to automatically remove all classes of dependencies
that are not used by the project, thereby minimizing the resulting uber JAR:

Expand Down
7 changes: 7 additions & 0 deletions src/site/apt/examples/resource-transformers.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,10 @@ Transformers in <<<org.apache.maven.plugins.shade.resource>>>
...
</project>
+-----

* Default transformers

If no transformer is configured (which means you didn't opened/closed <<<transformers>>>
because an empty list is considered valid) then a default setup is done. It adds <<<ServicesResourceTransformer>>>
and <<<ManifestResourceTransformer>>>.

Loading