Maven-shade-plugin is better than maven-assembly plugin

15 April, 2016
Recently I wanted to create am executable jar with all its dependencies. I started using maven assembly plugin to create jar with its dependencies but it was overwriting files with same name.

For example it was overwriting spring.handlers and spring.schemas files in META-INF directory and application was unable to find some namespaces.

Then I came across maven shade plugin. With maven-shade-plugin you can package all dependencies into one uber-jar. It can also be used to build an executable jar by specifying the main class.

I found this plugin particularly useful as it merges content of specific files instead of overwriting them. This is needed when there are resource files that are have the same name across the jars and the plugin tries to package all the resource files.


<plugin> 
    <groupId> org.apache.maven.plugins</groupId> 
    <artifactId> maven-shade-plugin</artifactId> 
    <version> 1.5</version> 
    <executions> 
        <execution> 
             <phase> package</phase> 
            <goals> 
                 <goal> shade</goal> 
            </goals> 
            <configuration> 
                  <transformers> 
                        <transformer  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
                                <mainClass> ${appClass}</mainClass> 
                       </transformer> 
                       <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> 
        </execution> 
    </executions> 
</plugin> 


No comments:

Post a Comment