Make delicious recipes!

Maven shade plugin


The maven shade plugin provides an ability to create an uber-jar.
It does so by renaming (called shading in maven) similarly named dependencies and jars during uber-jar creation.

In its simplest form, the shade plugin can be added to the pom.xml file as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.companyname.simple</groupId>
  <artifactId>simple</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>simple</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.apache.logging.log4j.adapters</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.0-beta4</version>
    </dependency>
	
  </dependencies>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <configuration>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  
</project>

The above shade plugin declaration tells maven to execute shade plugin during the package phase (<phase>package</phase>).
If the phase tag is not present in the declaration, then the shade-plugin will not be executed.


The shade plugin comes with a lot of options. Some of the commonly used ones are as follows:
Configuration Option Description
ServicesResourceTransformer
<configuration>
  <transformers>
    <transformer implementation=
      "org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
    </transformers>
</configuration>
  
Jars often have a META-INF/services/ directory where classes implementing every interface are listed.
This transformer helps in merging multiple implementations of same interface from several jar files.

See Service Loader for more info.
AppendingTransformer and
XmlAppendingTransformer
<configuration>
  <transformers>
    <transformer implementation=
      "org.apache.maven.plugins.shade.resource.AppendingTransformer">
        <resource>META-INF/spring.handlers</resource>
    </transformer>
    <transformer implementation=
      "org.apache.maven.plugins.shade.resource.AppendingTransformer">
        <resource>META-INF/spring.schemas</resource>
     </transformer>
  </transformers>
</configuration>
  
Used to append resources present in jar files like properties and xml files.
AppendingTransformer is for normal files.
XmlAppendingTransformer is for xml files.
DontIncludeResourceTransformer
<configuration>
  <transformers>
    <transformer implementation=
      "org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
      <resources>
        <resource>.pdf</resource>
        <resource>.doc</resource>
      </resources>
    </transformer>
  </transformers>
</configuration>
  
Excludes the matching resources from the final jar file.
IncludeResourceTransformer
<configuration>
  <transformers>
    <transformer implementation=
      "org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
      <resource>target-file-name</resource>
      <file>src-file-name</file>
    </transformer>
  </transformers>
</configuration>
  
Helps in renaming/moving the file during jar creation
ManifestResourceTransformer
<configuration>
  <transformers>
    <transformer implementation=
      "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
      <manifestEntries>
        <Main-Class>My-Own-Java-Class</Main-Class>
        <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
        <X-Compile-Target-JDK>1.7</X-Compile-Target-JDK>
      </manifestEntries>
    </transformer>
  </transformers>
</configuration>
  
Allows modification of entries in the Manifest file.
For example: setting main-class for execution.
Relocations
<configuration>
  <relocations>
    <relocation>
      <pattern>org.company.package</pattern>
      <shadedPattern>org.shaded.company.package</shadedPattern>
      <excludes>
        <exclude>org.company.package.main.Extras</exclude>
        <exclude>org.company.package.util.*</exclude>
      </excludes>
    </relocation>
  </relocations>
</configuration>
    
Helps in moving classes from one package to another.
The classes matching <exclude>pattern</exclude> are not moved between packages.
<configuration>
  <minimizeJar>true</minimizeJar>
</configuration>
    
Removes classes which are not used by the project
<configuration>
  <artifactSet>
    <excludes>
      <exclude>pkg1:*classname</exclude>
      <exclude>pkg2:lib.jar:</exclude>
    </excludes>
  </artifactSet>
</configuration>
    
Helps removing unwanted classes, jars


An exhaustive source of examples can be found at Apache Maven






Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal