Deploying Standalone Java Project

There are two common approaches to deploy a Java program in a standalone environment:

Single (Uber Jar) Approach

“Uber Jar” is a single jar with all dependent classes and resources are dumped inside. This can be done easily using Maven assembly plugin:



  org.apache.maven.plugins
  maven-assembly-plugin
  2.3
  
    
      jar-with-dependencies
    
  

mvn clean package assembly:single

However this method could be faulty. Multiple dependency jars could have configuration file with the same path and they will overlap each other. For example both spring-context.jar and spring-beans.jar has META-INF/spring.handlers file:

spring-context-3.2.3.RELEASE.jar/META-INF/spring.handlers
spring-beans-3.2.3.RELEASE.jar/META-INF/spring.handlers

Exploded Deployment Approach

This slightly take more effort, but saver approach. Instead of dumping everything in a Uber Jar, deploy in an exploded fashion. You can get all runtime dependencies (that is “runtime”, “compile” and “provided” maven scope) placed inside target/dependency folder by configuring maven-dependency-plugin:



  maven-dependency-plugin
  2.8
  
    
      
        copy-dependencies
      
      
        runtime
      
    
  

Then deploy your jar in production server in directory structure similar like following

C:myscript
  +-- dependency
        +-- dep1.jar
        +-- dep2.jar
        +-- dep3.jar
  +-- myjar.jar
  +-- run.bat

The run.bat script is a simple java program to run your code (assuming MainClass name of com.mycompany.MainClass)

java -jar "./*;dependency/*" com.mycompany.MainClass

Leave a Reply