2012/02/23

Maven: Creating Executable JAR with Shade Plugin

I was pleasantly surprised by versatility of Maven shade plugin. Creating a custom-named executable JAR and bundle only the dependencies you really need is a piece of cake. The documentation is partly is online, partly in the plugin's help and provides all necessary information.

<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>
         <outputFile>target/tool.jar</outputFile>
         <artifactSet>
           <includes>
              <include>org.springframework:spring-core</include>
              <include>org.springframework:spring-beans</include>
           </includes>
         </artifactSet>
         <transformers>
           <transformer 
             implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
             <mainClass>org.bithill.example.Tool</mainClass>
           </transformer>
         </transformers>
       </configuration>
     </execution>
  </executions>
</plugin>