Class ShadeMojo
- java.lang.Object
-
- org.apache.maven.plugin.AbstractMojo
-
- org.apache.maven.plugins.shade.mojo.ShadeMojo
-
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled,org.apache.maven.plugin.Mojo
@Mojo(name="shade", defaultPhase=PACKAGE, threadSafe=true, requiresDependencyResolution=RUNTIME) public class ShadeMojo extends org.apache.maven.plugin.AbstractMojoMojo that performs shading delegating to the Shader component.
-
-
Field Summary
Fields Modifier and Type Field Description private ArtifactSetartifactSetArtifacts to include/exclude from the final artifact.private booleancreateDependencyReducedPomFlag whether to generate a simplified POM for the shaded artifact.private booleancreateSourcesJarWhen true, it will attempt to create a sources jar as wellprivate booleancreateTestSourcesJarWhen true, it will attempt to create a test sources jar.private java.io.FiledependencyReducedPomLocationWhere to put the dependency reduced pom.private java.util.Set<java.lang.String>entryPointsUse this option in order to fine-tuneminimizeJar: By default, all of the target module's classes are kept and used as entry points for JAR minimization.private java.util.List<java.lang.String>extraArtifactsExtra Artifacts to infuse into shaded result.private java.util.List<java.io.File>extraJarsExtra JAR files to infuse into shaded result.private ArchiveFilter[]filtersArchive Filters to be used.private java.lang.StringfinalNameThe name of the shaded artifactId.private booleangenerateUniqueDependencyReducedPomCreate a dependency-reduced POM in ${basedir}/drp-UNIQUE.pom.private booleankeepDependenciesWithProvidedScopeWhen true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.private booleanminimizeJarWhen true, dependencies will be stripped down on the class level to only the transitive hull required for the artifact.private java.io.FileoutputDirectoryThe destination directory for the shaded artifact.private java.io.FileoutputFileThe path to the output file for the shaded artifact.private org.apache.maven.project.MavenProjectprojectThe current Maven project.private org.apache.maven.project.ProjectBuilderprojectBuilderProjectBuilder, needed to create projects from the artifacts.private org.apache.maven.project.MavenProjectHelperprojectHelperprivate booleanpromoteTransitiveDependenciesWhen true, transitive deps of removed dependencies are promoted to direct dependencies.private PackageRelocation[]relocationsPackages to be relocated.private org.eclipse.aether.RepositorySystemrepositorySystemprivate org.apache.maven.execution.MavenSessionsessionThe current Maven session.private booleanshadedArtifactAttachedDefines whether the shaded artifact should be attached as classifier to the original artifact.private java.lang.StringshadedArtifactIdThe name of the shaded artifactId.private java.lang.StringshadedClassifierNameThe name of the classifier used in case the shaded artifact is attached.private java.lang.StringshadedGroupFilterIf specified, this will include only artifacts which have groupIds which start with this.private Shadershaderprivate java.lang.StringshaderHintYou can pass here the roleHint about your own Shader implementation plexus component.private java.util.Map<java.lang.String,Shader>shadersAll the present Shaders.private booleanshadeSourcesContentWhen true, it will attempt to shade the contents of Java source files when creating the sources JAR.private booleanshadeTestJarWhen true, creates a shaded test-jar artifact as well.private booleanskipWhen true, skips the execution of this MOJO.private ResourceTransformer[]transformersResource transformers to be used.private booleanuseBaseVersionWhen true, the version of each dependency of the reduced pom will be based on the baseVersion of the original dependency instead of its resolved version.private booleanuseDependencyReducedPomInJarAdd dependency reduced POM to the JAR instead of the original one provided by the project.
-
Constructor Summary
Constructors Constructor Description ShadeMojo()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddSystemScopedDependencyFromNonInterpolatedPom(java.util.List<org.apache.maven.model.Dependency> dependencies, java.util.List<org.apache.maven.model.Dependency> originalDependencies)private voidcopyFiles(java.io.File source, java.io.File target)private org.apache.maven.model.DependencycreateDependency(org.apache.maven.artifact.Artifact artifact)private voidcreateDependencyReducedPom(java.util.Set<java.lang.String> artifactsToRemove)private voidcreateErrorOutput()private ShadeRequestcreateShadeSourcesRequest(java.lang.String shade, java.util.Set<java.io.File> testArtifacts, java.io.File testJar, java.util.List<Filter> filters, java.util.List<Relocator> relocators, java.util.List<ResourceTransformer> resourceTransformers)private booleandependencyHasExclusion(org.apache.maven.model.Dependency dep, org.apache.maven.artifact.Artifact exclusionToCheck)voidexecute()private java.util.List<Filter>getFilters(java.util.List<org.apache.maven.artifact.Artifact> artifactCollection)private java.lang.StringgetId(java.lang.String groupId, java.lang.String artifactId, java.lang.String type, java.lang.String classifier)private java.lang.StringgetId(org.apache.maven.artifact.Artifact artifact)private java.lang.StringgetId(org.apache.maven.model.Dependency dependency)private java.util.List<Relocator>getRelocators()private java.util.List<ResourceTransformer>getResourceTransformers()private booleaninvalidMainArtifact()private java.util.List<org.apache.maven.artifact.Artifact>processArtifactSelectors(java.util.Set<java.io.File> artifacts, java.util.Set<java.lang.String> artifactIds, java.util.Set<java.io.File> sourceArtifacts, java.util.Set<java.io.File> testArtifacts, java.util.Set<java.io.File> testSourceArtifacts, ArtifactSelector artifactSelector)private voidremoveSystemScopedDependencies(java.util.Set<java.lang.String> artifactsToRemove, java.util.List<org.apache.maven.model.Dependency> originalDependencies)private voidreplaceFile(java.io.File oldFile, java.io.File newFile)private org.eclipse.aether.artifact.ArtifactresolveArtifact(org.eclipse.aether.artifact.Artifact artifact)private org.apache.maven.artifact.ArtifactresolveArtifactForClassifier(org.apache.maven.artifact.Artifact artifact, java.lang.String classifier)private voidrewriteDependencyReducedPomIfWeHaveReduction(java.util.List<org.apache.maven.model.Dependency> dependencies, boolean modified, java.util.List<org.apache.maven.model.Dependency> transitiveDeps, org.apache.maven.model.Model model)private voidsetupHintedShader()private java.io.FileshadedArtifactFile(java.lang.String classifier)private java.io.FileshadedArtifactFileWithClassifier()private java.io.FileshadedArtifactFileWithClassifier(java.lang.String classifier)private java.io.FileshadedSourceArtifactFileWithClassifier()private java.io.FileshadedSourcesArtifactFile()private java.io.FileshadedTestArtifactFile()private java.io.FileshadedTestArtifactFileWithClassifier()private java.io.FileshadedTestSourceArtifactFileWithClassifier()private java.io.FileshadedTestSourcesArtifactFile()private ShadeRequestshadeRequest(java.lang.String shade, java.util.Set<java.io.File> artifacts, java.io.File outputJar, java.util.List<Filter> filters, java.util.List<Relocator> relocators, java.util.List<ResourceTransformer> resourceTransformers)private java.util.List<ResourceTransformer>toResourceTransformers(java.lang.String shade, java.util.List<ResourceTransformer> resourceTransformers)booleanupdateExcludesInDeps(org.apache.maven.project.MavenProject project, java.util.List<org.apache.maven.model.Dependency> dependencies, java.util.List<org.apache.maven.model.Dependency> transitiveDeps)
-
-
-
Field Detail
-
session
@Parameter(defaultValue="${session}", readonly=true, required=true) private org.apache.maven.execution.MavenSession sessionThe current Maven session.
-
project
@Parameter(defaultValue="${project}", readonly=true, required=true) private org.apache.maven.project.MavenProject projectThe current Maven project.
-
artifactSet
@Parameter private ArtifactSet artifactSet
Artifacts to include/exclude from the final artifact. Artifacts are denoted by composite identifiers of the general formgroupId:artifactId:type:classifier. Since version 1.3, the wildcard characters '*' and '?' can be used within the sub parts of those composite identifiers to do pattern matching. For convenience, the syntaxgroupIdis equivalent togroupId:*:*:*,groupId:artifactIdis equivalent togroupId:artifactId:*:*andgroupId:artifactId:classifieris equivalent togroupId:artifactId:*:classifier. For example:<artifactSet> <includes> <include>org.apache.maven:*</include> </includes> <excludes> <exclude>*:maven-core</exclude> </excludes> </artifactSet>
-
relocations
@Parameter private PackageRelocation[] relocations
Packages to be relocated. For example:<relocations> <relocation> <pattern>org.apache</pattern> <shadedPattern>hidden.org.apache</shadedPattern> <includes> <include>org.apache.maven.*</include> </includes> <excludes> <exclude>org.apache.maven.Public*</exclude> </excludes> </relocation> </relocations>Note: Support for includes exists only since version 1.4.
-
transformers
@Parameter private ResourceTransformer[] transformers
Resource transformers to be used. Please see the "Examples" section for more information on available transformers and their configuration.
-
filters
@Parameter private ArchiveFilter[] filters
Archive Filters to be used. Allows you to specify an artifact in the form of a composite identifier as used byartifactSetand a set of include/exclude file patterns for filtering which contents of the archive are added to the shaded jar. From a logical perspective, includes are processed before excludes, thus it's possible to use an include to collect a set of files from the archive then use excludes to further reduce the set. By default, all files are included and no files are excluded. If multiple filters apply to an artifact, the intersection of the matched files will be included in the final JAR. For example:<filters> <filter> <artifact>junit:junit</artifact> <includes> <include>org/junit/**</include> </includes> <excludes> <exclude>org/junit/experimental/**</exclude> </excludes> </filter> </filters>
-
outputDirectory
@Parameter(defaultValue="${project.build.directory}") private java.io.File outputDirectoryThe destination directory for the shaded artifact.
-
finalName
@Parameter private java.lang.String finalName
The name of the shaded artifactId. If you like to change the name of the native artifact, you may use the <build><finalName> setting. If this is set to something different than <build><finalName>, no file replacement will be performed, even if shadedArtifactAttached is being used.
-
shadedArtifactId
@Parameter(defaultValue="${project.artifactId}") private java.lang.String shadedArtifactIdThe name of the shaded artifactId. So you may want to use a different artifactId and keep the standard version. If the original artifactId was "foo" then the final artifact would be something like foo-1.0.jar. So if you change the artifactId you might have something like foo-special-1.0.jar.
-
shadedGroupFilter
@Parameter private java.lang.String shadedGroupFilter
If specified, this will include only artifacts which have groupIds which start with this.
-
shadedArtifactAttached
@Parameter private boolean shadedArtifactAttached
Defines whether the shaded artifact should be attached as classifier to the original artifact. If false, the shaded jar will be the main artifact of the project
-
createDependencyReducedPom
@Parameter(defaultValue="true") private boolean createDependencyReducedPom
Flag whether to generate a simplified POM for the shaded artifact. If set totrue, dependencies that have been included into the uber JAR will be removed from the<dependencies>section of the generated POM. The reduced POM will be nameddependency-reduced-pom.xmland is stored into the same directory as the shaded artifact. Unless you also specify dependencyReducedPomLocation, the plugin will create a temporary file nameddependency-reduced-pom.xmlin the project basedir.
-
dependencyReducedPomLocation
@Parameter(defaultValue="${basedir}/dependency-reduced-pom.xml") private java.io.File dependencyReducedPomLocationWhere to put the dependency reduced pom. Note: setting a value for this parameter with a directory other than ${basedir} will change the value of ${basedir} for all executions that come after the shade execution. This is often not what you want. This is considered an open issue with this plugin.- Since:
- 1.7
-
generateUniqueDependencyReducedPom
@Parameter(defaultValue="false") private boolean generateUniqueDependencyReducedPom
Create a dependency-reduced POM in ${basedir}/drp-UNIQUE.pom. This avoids build collisions of parallel builds without moving the dependency-reduced POM to a different directory. The property maven.shade.dependency-reduced-pom is set to the generated filename.- Since:
- 1.7.2
-
useDependencyReducedPomInJar
@Parameter(defaultValue="false") private boolean useDependencyReducedPomInJar
Add dependency reduced POM to the JAR instead of the original one provided by the project. IfcreateDependencyReducedPomisfalsethis parameter will be ignored.- Since:
- 3.3.0
-
keepDependenciesWithProvidedScope
@Parameter private boolean keepDependenciesWithProvidedScope
When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.
-
promoteTransitiveDependencies
@Parameter private boolean promoteTransitiveDependencies
When true, transitive deps of removed dependencies are promoted to direct dependencies. This should allow the drop in replacement of the removed deps with the new shaded jar and everything should still work.
-
shadedClassifierName
@Parameter(defaultValue="shaded") private java.lang.String shadedClassifierName
The name of the classifier used in case the shaded artifact is attached.
-
createSourcesJar
@Parameter private boolean createSourcesJar
When true, it will attempt to create a sources jar as well
-
createTestSourcesJar
@Parameter private boolean createTestSourcesJar
When true, it will attempt to create a test sources jar.
-
shadeSourcesContent
@Parameter(property="shadeSourcesContent", defaultValue="false") private boolean shadeSourcesContentWhen true, it will attempt to shade the contents of Java source files when creating the sources JAR. When false, it will just relocate the Java source files to the shaded paths, but will not modify the actual source file contents.Please note: This feature uses a heuristic search & replace approach which covers many, but definitely not all possible cases of source code shading and its excludes. There is no full Java parser behind this functionality, which would be the only way to get this right for Java language elements. As for matching within Java string constants, this is next to impossible to get 100% right, trying to guess if they are used in reflection or not.
Please understand that the source shading feature is not meant as a source code generator anyway, merely as a tool creating reasonably plausible source code when navigating to a relocated library class from an IDE, hopefully displaying source code which makes 95% sense - no more, no less.
-
minimizeJar
@Parameter private boolean minimizeJar
When true, dependencies will be stripped down on the class level to only the transitive hull required for the artifact. See alsoentryPoints, if you wish to further optimize JAR minimization.Note: This feature uses jdependency. Its accuracy therefore depends on jdependency's limitations.
- Since:
- 1.4
-
entryPoints
@Parameter private java.util.Set<java.lang.String> entryPoints
Use this option in order to fine-tuneminimizeJar: By default, all of the target module's classes are kept and used as entry points for JAR minimization. By explicitly limiting the set of entry points, you can further minimize the set of classes kept in the shaded JAR. This affects both classes in the module itself and dependency classes. IfminimizeJaris inactive, this option has no effect either.Note: This feature requires Java 1.8 or higher due to its use of jdependency. Its accuracy therefore also depends on jdependency's limitations.
Configuration example:
<minimizeJar>true</minimizeJar> <entryPoints> <entryPoint>org.acme.Application</entryPoint> <entryPoint>org.acme.OtherEntryPoint</entryPoint> </entryPoints>- Since:
- 3.5.0
-
outputFile
@Parameter private java.io.File outputFile
The path to the output file for the shaded artifact. When this parameter is set, the created archive will neither replace the project's main artifact nor will it be attached. Hence, this parameter causes the parametersfinalName,shadedArtifactAttached,shadedClassifierNameandcreateDependencyReducedPomto be ignored when used.- Since:
- 1.3
-
shaderHint
@Parameter private java.lang.String shaderHint
You can pass here the roleHint about your own Shader implementation plexus component.- Since:
- 1.6
-
useBaseVersion
@Parameter(defaultValue="false") private boolean useBaseVersion
When true, the version of each dependency of the reduced pom will be based on the baseVersion of the original dependency instead of its resolved version. For example, if the original pom (transitively) depends on a:a:2.7-SNAPSHOT, if useBaseVersion is set to false, the reduced pom will depend on a:a:2.7-20130312.222222-12 whereas if useBaseVersion is set to true, the reduced pom will depend on a:a:2.7-SNAPSHOT- Since:
- 3.0
-
shadeTestJar
@Parameter(defaultValue="false") private boolean shadeTestJar
When true, creates a shaded test-jar artifact as well.
-
skip
@Parameter(defaultValue="false") private boolean skip
When true, skips the execution of this MOJO.- Since:
- 3.3.0
-
extraJars
@Parameter private java.util.List<java.io.File> extraJars
Extra JAR files to infuse into shaded result. Accepts list of files that must exists. If any of specified files does not exist (or is not a file), Mojo will fail.Extra JARs will be processed in same way as main JAR (if any) is: applied relocation, resource transformers but not filtering.
Note: this feature should be used lightly, is not meant as ability to replace dependency hull! It is more just a feature to be able to slightly "differentiate" shaded JAR from main only.
- Since:
- 3.6.0
-
extraArtifacts
@Parameter private java.util.List<java.lang.String> extraArtifacts
Extra Artifacts to infuse into shaded result. Accepts list of GAVs in form of<groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>that will be resolved. If any of them cannot be resolved, Mojo will fail.The artifacts will be resolved (not transitively), and will be processed in same way as dependency JARs are (if any): applied relocation, resource transformers and filtering.
Note: this feature should be used lightly, is not meant as ability to replace dependency hull! It is more just a feature to be able to slightly "differentiate" shaded JAR from main only.
- Since:
- 3.6.0
-
projectHelper
@Inject private org.apache.maven.project.MavenProjectHelper projectHelper
-
shader
@Inject private Shader shader
-
repositorySystem
@Inject private org.eclipse.aether.RepositorySystem repositorySystem
-
projectBuilder
@Inject private org.apache.maven.project.ProjectBuilder projectBuilder
ProjectBuilder, needed to create projects from the artifacts.
-
shaders
@Inject private java.util.Map<java.lang.String,Shader> shaders
All the present Shaders.
-
-
Method Detail
-
execute
public void execute() throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException- in case of an error.
-
createErrorOutput
private void createErrorOutput()
-
shadeRequest
private ShadeRequest shadeRequest(java.lang.String shade, java.util.Set<java.io.File> artifacts, java.io.File outputJar, java.util.List<Filter> filters, java.util.List<Relocator> relocators, java.util.List<ResourceTransformer> resourceTransformers)
-
createShadeSourcesRequest
private ShadeRequest createShadeSourcesRequest(java.lang.String shade, java.util.Set<java.io.File> testArtifacts, java.io.File testJar, java.util.List<Filter> filters, java.util.List<Relocator> relocators, java.util.List<ResourceTransformer> resourceTransformers)
-
setupHintedShader
private void setupHintedShader() throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
processArtifactSelectors
private java.util.List<org.apache.maven.artifact.Artifact> processArtifactSelectors(java.util.Set<java.io.File> artifacts, java.util.Set<java.lang.String> artifactIds, java.util.Set<java.io.File> sourceArtifacts, java.util.Set<java.io.File> testArtifacts, java.util.Set<java.io.File> testSourceArtifacts, ArtifactSelector artifactSelector) throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
invalidMainArtifact
private boolean invalidMainArtifact()
-
replaceFile
private void replaceFile(java.io.File oldFile, java.io.File newFile) throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
copyFiles
private void copyFiles(java.io.File source, java.io.File target) throws java.io.IOException- Throws:
java.io.IOException
-
resolveArtifactForClassifier
private org.apache.maven.artifact.Artifact resolveArtifactForClassifier(org.apache.maven.artifact.Artifact artifact, java.lang.String classifier)
-
resolveArtifact
private org.eclipse.aether.artifact.Artifact resolveArtifact(org.eclipse.aether.artifact.Artifact artifact) throws org.eclipse.aether.resolution.ArtifactResolutionException- Throws:
org.eclipse.aether.resolution.ArtifactResolutionException
-
getRelocators
private java.util.List<Relocator> getRelocators()
-
getResourceTransformers
private java.util.List<ResourceTransformer> getResourceTransformers() throws org.apache.maven.plugin.MojoExecutionException
- Throws:
org.apache.maven.plugin.MojoExecutionException
-
getFilters
private java.util.List<Filter> getFilters(java.util.List<org.apache.maven.artifact.Artifact> artifactCollection) throws org.apache.maven.plugin.MojoExecutionException
- Throws:
org.apache.maven.plugin.MojoExecutionException
-
shadedArtifactFileWithClassifier
private java.io.File shadedArtifactFileWithClassifier()
-
shadedSourceArtifactFileWithClassifier
private java.io.File shadedSourceArtifactFileWithClassifier()
-
shadedTestSourceArtifactFileWithClassifier
private java.io.File shadedTestSourceArtifactFileWithClassifier()
-
shadedArtifactFileWithClassifier
private java.io.File shadedArtifactFileWithClassifier(java.lang.String classifier)
-
shadedTestArtifactFileWithClassifier
private java.io.File shadedTestArtifactFileWithClassifier()
-
shadedSourcesArtifactFile
private java.io.File shadedSourcesArtifactFile()
-
shadedTestSourcesArtifactFile
private java.io.File shadedTestSourcesArtifactFile()
-
shadedArtifactFile
private java.io.File shadedArtifactFile(java.lang.String classifier)
-
shadedTestArtifactFile
private java.io.File shadedTestArtifactFile()
-
createDependencyReducedPom
private void createDependencyReducedPom(java.util.Set<java.lang.String> artifactsToRemove) throws java.io.IOException, org.apache.maven.project.ProjectBuildingException, org.eclipse.aether.collection.DependencyCollectionException- Throws:
java.io.IOExceptionorg.apache.maven.project.ProjectBuildingExceptionorg.eclipse.aether.collection.DependencyCollectionException
-
rewriteDependencyReducedPomIfWeHaveReduction
private void rewriteDependencyReducedPomIfWeHaveReduction(java.util.List<org.apache.maven.model.Dependency> dependencies, boolean modified, java.util.List<org.apache.maven.model.Dependency> transitiveDeps, org.apache.maven.model.Model model) throws java.io.IOException, org.apache.maven.project.ProjectBuildingException, org.eclipse.aether.collection.DependencyCollectionException- Throws:
java.io.IOExceptionorg.apache.maven.project.ProjectBuildingExceptionorg.eclipse.aether.collection.DependencyCollectionException
-
removeSystemScopedDependencies
private void removeSystemScopedDependencies(java.util.Set<java.lang.String> artifactsToRemove, java.util.List<org.apache.maven.model.Dependency> originalDependencies)
-
addSystemScopedDependencyFromNonInterpolatedPom
private void addSystemScopedDependencyFromNonInterpolatedPom(java.util.List<org.apache.maven.model.Dependency> dependencies, java.util.List<org.apache.maven.model.Dependency> originalDependencies)
-
createDependency
private org.apache.maven.model.Dependency createDependency(org.apache.maven.artifact.Artifact artifact)
-
getId
private java.lang.String getId(org.apache.maven.artifact.Artifact artifact)
-
getId
private java.lang.String getId(org.apache.maven.model.Dependency dependency)
-
getId
private java.lang.String getId(java.lang.String groupId, java.lang.String artifactId, java.lang.String type, java.lang.String classifier)
-
updateExcludesInDeps
public boolean updateExcludesInDeps(org.apache.maven.project.MavenProject project, java.util.List<org.apache.maven.model.Dependency> dependencies, java.util.List<org.apache.maven.model.Dependency> transitiveDeps) throws org.eclipse.aether.collection.DependencyCollectionException- Throws:
org.eclipse.aether.collection.DependencyCollectionException
-
dependencyHasExclusion
private boolean dependencyHasExclusion(org.apache.maven.model.Dependency dep, org.apache.maven.artifact.Artifact exclusionToCheck)
-
toResourceTransformers
private java.util.List<ResourceTransformer> toResourceTransformers(java.lang.String shade, java.util.List<ResourceTransformer> resourceTransformers)
-
-