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.AbstractMojo
Mojo that performs shading delegating to the Shader component.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver
Artifact resolver, needed to download source jars for inclusion in classpath.private ArtifactSet
Artifacts to include/exclude from the final artifact.private boolean
Flag whether to generate a simplified POM for the shaded artifact.private boolean
When true, it will attempt to create a sources jar as wellprivate boolean
When true, it will attempt to create a test sources jar.private org.apache.maven.shared.dependency.graph.DependencyGraphBuilder
The dependency graph builder to use.private File
Where to put the dependency reduced pom.private ArchiveFilter[]
Archive Filters to be used.private String
The name of the shaded artifactId.private boolean
Create a dependency-reduced POM in ${basedir}/drp-UNIQUE.pom.private boolean
When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.protected org.apache.maven.artifact.repository.ArtifactRepository
Local maven repository.private boolean
When true, dependencies will be stripped down on the class level to only the transitive hull required for the artifact.private File
The destination directory for the shaded artifact.private File
The path to the output file for the shaded artifact.private org.apache.maven.project.MavenProject
The current Maven project.private org.apache.maven.project.ProjectBuilder
ProjectBuilder, needed to create projects from the artifacts.private org.apache.maven.project.MavenProjectHelper
private boolean
When true, transitive deps of removed dependencies are promoted to direct dependencies.private PackageRelocation[]
Packages to be relocated.protected List<org.apache.maven.artifact.repository.ArtifactRepository>
Remote repositories which will be searched for source attachments.private org.apache.maven.execution.MavenSession
The current Maven session.private boolean
Defines whether the shaded artifact should be attached as classifier to the original artifact.private String
The name of the shaded artifactId.private String
The name of the classifier used in case the shaded artifact is attached.private String
If specified, this will include only artifacts which have groupIds which start with this.private Shader
private String
You can pass here the roleHint about your own Shader implementation plexus component.All the present Shaders.private boolean
When true, it will attempt to shade the contents of Java source files when creating the sources JAR.private boolean
When true, creates a shaded test-jar artifact as well.private boolean
When true, skips the execution of this MOJO.private ResourceTransformer[]
Resource transformers to be used.private boolean
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.private boolean
Add dependency reduced POM to the JAR instead of the original one provided by the project.Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addSystemScopedDependencyFromNonInterpolatedPom
(List<org.apache.maven.model.Dependency> dependencies, List<org.apache.maven.model.Dependency> originalDependencies) private void
private org.apache.maven.model.Dependency
createDependency
(org.apache.maven.artifact.Artifact artifact) private void
createDependencyReducedPom
(Set<String> artifactsToRemove) private void
private ShadeRequest
createShadeSourcesRequest
(String shade, Set<File> testArtifacts, File testJar, List<Filter> filters, List<Relocator> relocators, List<ResourceTransformer> resourceTransformers) void
execute()
private String
private String
getId
(org.apache.maven.artifact.Artifact artifact) private String
getId
(org.apache.maven.model.Dependency dependency) private List<ResourceTransformer>
private boolean
private void
processArtifactSelectors
(Set<File> artifacts, Set<String> artifactIds, Set<File> sourceArtifacts, Set<File> testArtifacts, Set<File> testSourceArtifacts, ArtifactSelector artifactSelector) private void
removeSystemScopedDependencies
(Set<String> artifactsToRemove, List<org.apache.maven.model.Dependency> originalDependencies) private void
replaceFile
(File oldFile, File newFile) private File
resolveArtifactForClassifier
(org.apache.maven.artifact.Artifact artifact, String classifier) private void
rewriteDependencyReducedPomIfWeHaveReduction
(List<org.apache.maven.model.Dependency> dependencies, boolean modified, List<org.apache.maven.model.Dependency> transitiveDeps, org.apache.maven.model.Model model) private void
private File
shadedArtifactFile
(String classifier) private File
private File
shadedArtifactFileWithClassifier
(String classifier) private File
private File
private File
private File
private File
private File
private ShadeRequest
shadeRequest
(String shade, Set<File> artifacts, File outputJar, List<Filter> filters, List<Relocator> relocators, List<ResourceTransformer> resourceTransformers) private List<ResourceTransformer>
toResourceTransformers
(String shade, List<ResourceTransformer> resourceTransformers) boolean
updateExcludesInDeps
(org.apache.maven.project.MavenProject project, List<org.apache.maven.model.Dependency> dependencies, List<org.apache.maven.model.Dependency> transitiveDeps) Methods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
-
Field Details
-
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. -
projectHelper
@Component private org.apache.maven.project.MavenProjectHelper projectHelper -
shader
-
dependencyGraphBuilder
@Component private org.apache.maven.shared.dependency.graph.DependencyGraphBuilder dependencyGraphBuilderThe dependency graph builder to use. -
projectBuilder
@Component private org.apache.maven.project.ProjectBuilder projectBuilderProjectBuilder, needed to create projects from the artifacts. -
remoteArtifactRepositories
@Parameter(readonly=true, required=true, defaultValue="${project.remoteArtifactRepositories}") protected List<org.apache.maven.artifact.repository.ArtifactRepository> remoteArtifactRepositoriesRemote repositories which will be searched for source attachments. -
localRepository
@Parameter(readonly=true, required=true, defaultValue="${localRepository}") protected org.apache.maven.artifact.repository.ArtifactRepository localRepositoryLocal maven repository. -
artifactResolver
@Component protected org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver artifactResolverArtifact resolver, needed to download source jars for inclusion in classpath. -
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 syntaxgroupId
is equivalent togroupId:*:*:*
,groupId:artifactId
is equivalent togroupId:artifactId:*:*
andgroupId:artifactId:classifier
is equivalent togroupId:artifactId:*:classifier
. For example:<artifactSet> <includes> <include>org.apache.maven:*</include> </includes> <excludes> <exclude>*:maven-core</exclude> </excludes> </artifactSet>
-
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
Resource transformers to be used. Please see the "Examples" section for more information on available transformers and their configuration. -
filters
Archive Filters to be used. Allows you to specify an artifact in the form of a composite identifier as used byartifactSet
and 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
The destination directory for the shaded artifact. -
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
The 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
If specified, this will include only artifacts which have groupIds which start with this. -
shadedArtifactAttached
@Parameter private boolean shadedArtifactAttachedDefines 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 createDependencyReducedPomFlag 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.xml
and 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.xml
in the project basedir. -
dependencyReducedPomLocation
@Parameter(defaultValue="${basedir}/dependency-reduced-pom.xml") private 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 generateUniqueDependencyReducedPomCreate 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 useDependencyReducedPomInJarAdd dependency reduced POM to the JAR instead of the original one provided by the project. IfcreateDependencyReducedPom
isfalse
this parameter will be ignored.- Since:
- 3.3.0
-
keepDependenciesWithProvidedScope
@Parameter private boolean keepDependenciesWithProvidedScopeWhen true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed. -
promoteTransitiveDependencies
@Parameter private boolean promoteTransitiveDependenciesWhen 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
The name of the classifier used in case the shaded artifact is attached. -
createSourcesJar
@Parameter private boolean createSourcesJarWhen true, it will attempt to create a sources jar as well -
createTestSourcesJar
@Parameter private boolean createTestSourcesJarWhen 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 minimizeJarWhen true, dependencies will be stripped down on the class level to only the transitive hull required for the artifact. Note: Usage of this feature requires Java 1.5 or higher.- Since:
- 1.4
-
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
,shadedClassifierName
andcreateDependencyReducedPom
to be ignored when used.- Since:
- 1.3
-
shaderHint
You can pass here the roleHint about your own Shader implementation plexus component.- Since:
- 1.6
-
useBaseVersion
@Parameter(defaultValue="false") private boolean useBaseVersionWhen 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 shadeTestJarWhen true, creates a shaded test-jar artifact as well. -
shaders
All the present Shaders. -
skip
@Parameter(defaultValue="false") private boolean skipWhen true, skips the execution of this MOJO.- Since:
- 3.3.0
-
-
Constructor Details
-
ShadeMojo
public ShadeMojo()
-
-
Method Details
-
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
-
createShadeSourcesRequest
-
setupHintedShader
private void setupHintedShader() throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
processArtifactSelectors
-
invalidMainArtifact
private boolean invalidMainArtifact() -
replaceFile
private void replaceFile(File oldFile, File newFile) throws org.apache.maven.plugin.MojoExecutionException - Throws:
org.apache.maven.plugin.MojoExecutionException
-
copyFiles
- Throws:
IOException
-
resolveArtifactForClassifier
-
getRelocators
-
getResourceTransformers
-
getFilters
- Throws:
org.apache.maven.plugin.MojoExecutionException
-
shadedArtifactFileWithClassifier
-
shadedSourceArtifactFileWithClassifier
-
shadedTestSourceArtifactFileWithClassifier
-
shadedArtifactFileWithClassifier
-
shadedTestArtifactFileWithClassifier
-
shadedSourcesArtifactFile
-
shadedTestSourcesArtifactFile
-
shadedArtifactFile
-
shadedTestArtifactFile
-
createDependencyReducedPom
private void createDependencyReducedPom(Set<String> artifactsToRemove) throws IOException, org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException, org.apache.maven.project.ProjectBuildingException - Throws:
IOException
org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException
org.apache.maven.project.ProjectBuildingException
-
rewriteDependencyReducedPomIfWeHaveReduction
private void rewriteDependencyReducedPomIfWeHaveReduction(List<org.apache.maven.model.Dependency> dependencies, boolean modified, List<org.apache.maven.model.Dependency> transitiveDeps, org.apache.maven.model.Model model) throws IOException, org.apache.maven.project.ProjectBuildingException, org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException - Throws:
IOException
org.apache.maven.project.ProjectBuildingException
org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException
-
removeSystemScopedDependencies
-
addSystemScopedDependencyFromNonInterpolatedPom
-
createDependency
private org.apache.maven.model.Dependency createDependency(org.apache.maven.artifact.Artifact artifact) -
getId
-
getId
-
getId
-
updateExcludesInDeps
public boolean updateExcludesInDeps(org.apache.maven.project.MavenProject project, List<org.apache.maven.model.Dependency> dependencies, List<org.apache.maven.model.Dependency> transitiveDeps) throws org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException - Throws:
org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException
-
toResourceTransformers
private List<ResourceTransformer> toResourceTransformers(String shade, List<ResourceTransformer> resourceTransformers)
-