Class ShadeMojo
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled,org.apache.maven.plugin.Mojo
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ArtifactSetArtifacts to include/exclude from the final artifact.private booleanFlag whether to generate a simplified POM for the shaded artifact.private booleanWhen true, it will attempt to create a sources jar as wellprivate booleanWhen true, it will attempt to create a test sources jar.private org.apache.maven.shared.dependency.graph.DependencyGraphBuilderThe dependency graph builder to use.private FileWhere to put the dependency reduced pom.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.private ArchiveFilter[]Archive Filters to be used.private StringThe name of the shaded artifactId.private booleanCreate a dependency-reduced POM in ${basedir}/drp-UNIQUE.pom.private booleanWhen true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.private booleanWhen true, dependencies will be stripped down on the class level to only the transitive hull required for the artifact.private FileThe destination directory for the shaded artifact.private FileThe path to the output file for the shaded artifact.private org.apache.maven.project.MavenProjectThe current Maven project.private org.apache.maven.project.ProjectBuilderProjectBuilder, needed to create projects from the artifacts.private org.apache.maven.project.MavenProjectHelperprivate booleanWhen true, transitive deps of removed dependencies are promoted to direct dependencies.private PackageRelocation[]Packages to be relocated.private org.eclipse.aether.RepositorySystemprivate org.apache.maven.execution.MavenSessionThe current Maven session.private booleanDefines whether the shaded artifact should be attached as classifier to the original artifact.private StringThe name of the shaded artifactId.private StringThe name of the classifier used in case the shaded artifact is attached.private StringIf specified, this will include only artifacts which have groupIds which start with this.private Shaderprivate StringYou can pass here the roleHint about your own Shader implementation plexus component.All the present Shaders.private booleanWhen true, it will attempt to shade the contents of Java source files when creating the sources JAR.private booleanWhen true, creates a shaded test-jar artifact as well.private booleanWhen true, skips the execution of this MOJO.private ResourceTransformer[]Resource transformers to be used.private booleanWhen 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 booleanAdd 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 voidaddSystemScopedDependencyFromNonInterpolatedPom(List<org.apache.maven.model.Dependency> dependencies, List<org.apache.maven.model.Dependency> originalDependencies) private voidprivate org.apache.maven.model.DependencycreateDependency(org.apache.maven.artifact.Artifact artifact) private voidcreateDependencyReducedPom(Set<String> artifactsToRemove) private voidprivate ShadeRequestcreateShadeSourcesRequest(String shade, Set<File> testArtifacts, File testJar, List<Filter> filters, List<Relocator> relocators, List<ResourceTransformer> resourceTransformers) private booleandependencyHasExclusion(org.apache.maven.model.Dependency dep, org.apache.maven.artifact.Artifact exclusionToCheck) voidexecute()private Stringprivate StringgetId(org.apache.maven.artifact.Artifact artifact) private StringgetId(org.apache.maven.model.Dependency dependency) private List<ResourceTransformer>private booleanprivate voidprocessArtifactSelectors(Set<File> artifacts, Set<String> artifactIds, Set<File> sourceArtifacts, Set<File> testArtifacts, Set<File> testSourceArtifacts, ArtifactSelector artifactSelector) private voidremoveSystemScopedDependencies(Set<String> artifactsToRemove, List<org.apache.maven.model.Dependency> originalDependencies) private voidreplaceFile(File oldFile, File newFile) private FileresolveArtifactForClassifier(org.apache.maven.artifact.Artifact artifact, String classifier) private voidrewriteDependencyReducedPomIfWeHaveReduction(List<org.apache.maven.model.Dependency> dependencies, boolean modified, List<org.apache.maven.model.Dependency> transitiveDeps, org.apache.maven.model.Model model) private voidprivate FileshadedArtifactFile(String classifier) private Fileprivate FileshadedArtifactFileWithClassifier(String classifier) private Fileprivate Fileprivate Fileprivate Fileprivate Fileprivate Fileprivate ShadeRequestshadeRequest(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) booleanupdateExcludesInDeps(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. -
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
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 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
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.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 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. IfcreateDependencyReducedPomisfalsethis 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. See alsoentryPoints, if you wish to further optimize JAR minimization.Note: This feature requires Java 1.8 or higher due to its use of jdependency. Its accuracy therefore also depends on jdependency's limitations.
- Since:
- 1.4
-
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
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
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. -
skip
@Parameter(defaultValue="false") private boolean skipWhen true, skips the execution of this MOJO.- Since:
- 3.3.0
-
projectHelper
@Inject private org.apache.maven.project.MavenProjectHelper projectHelper -
shader
-
repositorySystem
@Inject private org.eclipse.aether.RepositorySystem repositorySystem -
dependencyGraphBuilder
@Inject private org.apache.maven.shared.dependency.graph.DependencyGraphBuilder dependencyGraphBuilderThe dependency graph builder to use. -
projectBuilder
@Inject private org.apache.maven.project.ProjectBuilder projectBuilderProjectBuilder, needed to create projects from the artifacts. -
shaders
All the present Shaders.
-
-
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:
IOExceptionorg.apache.maven.shared.dependency.graph.DependencyGraphBuilderExceptionorg.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:
IOExceptionorg.apache.maven.project.ProjectBuildingExceptionorg.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
-
dependencyHasExclusion
private boolean dependencyHasExclusion(org.apache.maven.model.Dependency dep, org.apache.maven.artifact.Artifact exclusionToCheck) -
toResourceTransformers
private List<ResourceTransformer> toResourceTransformers(String shade, List<ResourceTransformer> resourceTransformers)
-