-
Notifications
You must be signed in to change notification settings - Fork 91
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] adding useDefaultConfiguration flag in ShadeMojo #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,8 +78,10 @@ | |
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.ServiceLoader; | ||
import java.util.Set; | ||
|
||
/** | ||
|
@@ -490,7 +494,7 @@ public void execute() | |
replaceFile( finalFile, testJar ); | ||
testJar = finalFile; | ||
} | ||
|
||
renamed = true; | ||
} | ||
|
||
|
@@ -752,30 +756,66 @@ private List<Relocator> getRelocators() | |
|
||
private List<ResourceTransformer> getResourceTransformers() | ||
{ | ||
final List<ResourceTransformer> resourceTransformers = new LinkedList<>( getDefaultResourceTransformers() ); | ||
if ( transformers == null ) | ||
{ | ||
return Collections.emptyList(); | ||
return resourceTransformers; | ||
} | ||
resourceTransformers.addAll( Arrays.asList( transformers ) ); | ||
return resourceTransformers; | ||
} | ||
|
||
return Arrays.asList( transformers ); | ||
private List<ResourceTransformer> getDefaultResourceTransformers() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're not merging, so you can return a list with these 2 transformers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guess it is addressed by previous comment, we want to merge ;) |
||
final List<ResourceTransformer> 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() ); | ||
} | ||
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; | ||
} | ||
|
||
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 | ||
{ | ||
List<Filter> filters = new ArrayList<>(); | ||
List<SimpleFilter> simpleFilters = new ArrayList<>(); | ||
List<Filter> filters = new LinkedList<>(); | ||
List<SimpleFilter> simpleFilters = new LinkedList<>(); | ||
Map<Artifact, ArtifactId> artifacts = null; | ||
|
||
if ( this.filters != null && this.filters.length > 0 ) | ||
{ | ||
Map<Artifact, ArtifactId> artifacts = new HashMap<>(); | ||
|
||
artifacts.put( project.getArtifact(), new ArtifactId( project.getArtifact() ) ); | ||
|
||
for ( Artifact artifact : project.getArtifacts() ) | ||
{ | ||
artifacts.put( artifact, new ArtifactId( artifact ) ); | ||
} | ||
artifacts = getArtifactIds(); | ||
|
||
for ( ArchiveFilter filter : this.filters ) | ||
{ | ||
|
@@ -813,6 +853,51 @@ private List<Filter> getFilters() | |
} | ||
} | ||
|
||
// first check for backward compatibility this is not already configured explicitly | ||
boolean addExclusion = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a similar trick here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likely the same |
||
if ( this.filters != null ) | ||
{ | ||
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() ) ) | ||
{ | ||
addExclusion = false; | ||
break; | ||
} | ||
} | ||
} | ||
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( "*:*" ) ), | ||
new ArchiveFilter( | ||
Collections.<String>emptySet(), | ||
new HashSet<>( Arrays.asList( "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA" ) ) ) ) ); | ||
} | ||
|
||
for ( final Filter filter : ServiceLoader.load( Filter.class ) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using ServiceLoader requires a separate JIRA issue, and I wonder if this is going to work. See https://issues.apache.org/jira/browse/MNG-6275 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It must work if the extension is added in plugin dependencies as commonly done for transformers - http://openwebbeans.apache.org/meecrowave/meecrowave-maven/index.html#_shading for example. About the other issue: it can be the case but the issue is to make the configuration easier, if we don't want of a toggle then the SPI is the answer so it is the same issue, no? |
||
{ | ||
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 ); | ||
|
||
if ( minimizeJar ) | ||
|
@@ -832,6 +917,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(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're still merging. Here you should replace
Collections.emptyList()
withgetDefaultResourceTransformers()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the case, resourceTransformers is initialized with getDefaultResourceTransformers. If the else skips the default transformers the feature is pointless since in 80% of the case you need to add a custom transformer or customize a config (like mainClass for the manifest)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's agree to disagree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine to disagree but I'd like to solve the issue which aims to reduce the entry cost of the plugin and save hours of debug time due to a bad packaging which is hard to identify - and even more with jpms since module-info.java drops SPI registration :s.
any alternative proposal to have a trivial to enable good auto configuration compatible with user customizations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't think of a clean, clear and simple solution yet. So let's split it into 2 issues: 1. have good default transformers; 2. combine default+custom transfomers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's split. Just to ensure I get it and not do work for nothing, it means 3 PR - no order:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 done at #12
2 done at #13
will let 3 to a bit later to see if we find a good idea