Wednesday, December 3, 2014

Maven: Creating fat JARs and executable JARs




the executable JAR


For Java applications that can (and should) be executed on the Windows command line or Linux terminal session, create an executable jar.

This plugin works well for me:
<plugin>  
 <artifactId>maven-assembly-plugin</artifactId>  
 <configuration>  
  <archive>  
   <manifest>  
    <mainClass>dev.sandbox.twitter.StreamTwitter</mainClass>  
   </manifest>  
  </archive>  
  <descriptorRefs>  
   <descriptorRef>jar-with-dependencies</descriptorRef>  
  </descriptorRefs>  
 </configuration>  
</plugin>  

I run this on the command line like this:
 c:\temp>java -cp target/twitter-1.0-SNAPSHOT.jar dev.sandbox.twitter.StreamTwitter  
 Writing to File: \\Data\Twitter-raw\raw-fh\1417630549.json  
 ... etc ...  



the fat JAR


If I'm writing mapReduce code in Java, and referencing external POM files, I'll need to build a fat JAR that contains all the dependencies for Hadoop to successfully execute my code.

Adding this plugin has worked well for me on Hadoop 2.6.0:
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-shade-plugin</artifactId>
 <executions>
  <execution>
   <phase>package</phase>
   <goals>
    <goal>shade</goal>
   </goals>
  </execution>
 </executions>
 <configuration>
  <finalName>uber-${artifactId}-${version}</finalName>
 </configuration>
</plugin>

If you try to run this "uber-*" jar from the command line, and get this error:
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

then add this filter to the POM (within the configuration section):
<filters>
 <filter>
  <artifact>*:*</artifact>
  <excludes>
   <exclude>META-INF/*.SF</exclude>
   <exclude>META-INF/*.DSA</exclude>
   <exclude>META-INF/*.RSA</exclude>
  </excludes>
 </filter>
</filters>



References

  1. [AvaJava] Building a JAR file that contains its dependencies
  2. [StackOverflow] Troubleshooting "Invalid Signature Digest"

2 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai.

    ReplyDelete