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 Details

    • session

      @Parameter(defaultValue="${session}", readonly=true, required=true) private org.apache.maven.execution.MavenSession session
      The current Maven session.
    • project

      @Parameter(defaultValue="${project}", readonly=true, required=true) private org.apache.maven.project.MavenProject project
      The current Maven project.
    • projectHelper

      @Component private org.apache.maven.project.MavenProjectHelper projectHelper
    • shader

      @Component(hint="default", role=Shader.class) private Shader shader
    • dependencyGraphBuilder

      @Component private org.apache.maven.shared.dependency.graph.DependencyGraphBuilder dependencyGraphBuilder
      The dependency graph builder to use.
    • projectBuilder

      @Component private org.apache.maven.project.ProjectBuilder projectBuilder
      ProjectBuilder, needed to create projects from the artifacts.
    • remoteArtifactRepositories

      @Parameter(readonly=true, required=true, defaultValue="${project.remoteArtifactRepositories}") protected List<org.apache.maven.artifact.repository.ArtifactRepository> remoteArtifactRepositories
      Remote repositories which will be searched for source attachments.
    • localRepository

      @Parameter(readonly=true, required=true, defaultValue="${localRepository}") protected org.apache.maven.artifact.repository.ArtifactRepository localRepository
      Local maven repository.
    • artifactResolver

      @Component protected org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver artifactResolver
      Artifact resolver, needed to download source jars for inclusion in classpath.
    • artifactSet

      @Parameter private ArtifactSet artifactSet
      Artifacts to include/exclude from the final artifact. Artifacts are denoted by composite identifiers of the general form groupId: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 syntax groupId is equivalent to groupId:*:*:*, groupId:artifactId is equivalent to groupId:artifactId:*:* and groupId:artifactId:classifier is equivalent to groupId: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 by artifactSet 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

      @Parameter(defaultValue="${project.build.directory}") private File outputDirectory
      The destination directory for the shaded artifact.
    • finalName

      @Parameter private 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 String 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

      @Parameter private 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 to true, 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 named dependency-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 named dependency-reduced-pom.xml in the project basedir.
    • dependencyReducedPomLocation

      @Parameter(defaultValue="${basedir}/dependency-reduced-pom.xml") private File dependencyReducedPomLocation
      Where 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. If createDependencyReducedPom is false this 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 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 shadeSourcesContent
      When 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. Note: Usage of this feature requires Java 1.5 or higher.
      Since:
      1.4
    • outputFile

      @Parameter private 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 parameters finalName, shadedArtifactAttached, shadedClassifierName and createDependencyReducedPom to be ignored when used.
      Since:
      1.3
    • shaderHint

      @Parameter private 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.
    • shaders

      @Inject private Map<String,Shader> shaders
      All the present Shaders.
    • skip

      @Parameter(defaultValue="false") private boolean skip
      When 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

      private ShadeRequest shadeRequest(String shade, Set<File> artifacts, File outputJar, List<Filter> filters, List<Relocator> relocators, List<ResourceTransformer> resourceTransformers)
    • createShadeSourcesRequest

      private ShadeRequest createShadeSourcesRequest(String shade, Set<File> testArtifacts, File testJar, List<Filter> filters, List<Relocator> relocators, List<ResourceTransformer> resourceTransformers)
    • setupHintedShader

      private void setupHintedShader() throws org.apache.maven.plugin.MojoExecutionException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
    • processArtifactSelectors

      private void processArtifactSelectors(Set<File> artifacts, Set<String> artifactIds, Set<File> sourceArtifacts, Set<File> testArtifacts, Set<File> testSourceArtifacts, ArtifactSelector artifactSelector)
    • 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

      private void copyFiles(File source, File target) throws IOException
      Throws:
      IOException
    • resolveArtifactForClassifier

      private File resolveArtifactForClassifier(org.apache.maven.artifact.Artifact artifact, String classifier)
    • getRelocators

      private List<Relocator> getRelocators()
    • getResourceTransformers

      private List<ResourceTransformer> getResourceTransformers()
    • getFilters

      private List<Filter> getFilters() throws org.apache.maven.plugin.MojoExecutionException
      Throws:
      org.apache.maven.plugin.MojoExecutionException
    • shadedArtifactFileWithClassifier

      private File shadedArtifactFileWithClassifier()
    • shadedSourceArtifactFileWithClassifier

      private File shadedSourceArtifactFileWithClassifier()
    • shadedTestSourceArtifactFileWithClassifier

      private File shadedTestSourceArtifactFileWithClassifier()
    • shadedArtifactFileWithClassifier

      private File shadedArtifactFileWithClassifier(String classifier)
    • shadedTestArtifactFileWithClassifier

      private File shadedTestArtifactFileWithClassifier()
    • shadedSourcesArtifactFile

      private File shadedSourcesArtifactFile()
    • shadedTestSourcesArtifactFile

      private File shadedTestSourcesArtifactFile()
    • shadedArtifactFile

      private File shadedArtifactFile(String classifier)
    • shadedTestArtifactFile

      private File 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

      private void removeSystemScopedDependencies(Set<String> artifactsToRemove, List<org.apache.maven.model.Dependency> originalDependencies)
    • addSystemScopedDependencyFromNonInterpolatedPom

      private void addSystemScopedDependencyFromNonInterpolatedPom(List<org.apache.maven.model.Dependency> dependencies, List<org.apache.maven.model.Dependency> originalDependencies)
    • createDependency

      private org.apache.maven.model.Dependency createDependency(org.apache.maven.artifact.Artifact artifact)
    • getId

      private String getId(org.apache.maven.artifact.Artifact artifact)
    • getId

      private String getId(org.apache.maven.model.Dependency dependency)
    • getId

      private String getId(String groupId, String artifactId, String type, String classifier)
    • 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)