Correct way to deploy .jar file in production

Viewed 2935

I'm new to deployment, I've jar file as a package how should I go ahead for deployment in my prod server?

My prod server is basically an EC2 machine running on AWS

  1. Shall I just run java - jar "jar file"? is this the practiced way for prod servers?
  2. Shall I package .jar file to ear/war and deploy in tomcat/jboss server?

Help appreciated!

5 Answers

Both of the approaches you described are valid.

For small web APIs that include their own application server (e.g. Spark, Javalin, Spring Boot) on Linux servers, I start a screen session and run java -jar. This allows me to exit the SSH session (disconnect from the server) without terminating the program.

For other Spring applications that don't include an application server, I package the code into a WAR and copy it to an application server's deployment directory. For Tomcat, that's webapps. The application server can then read the WAR and spin up a running instance (assuming hot-deploy is enabled).

Tomcat in particular also has a web page where you can upload your WAR file to deploy it.

If you would that the program is still held run after disconnecting from SSH or closing the terminal in Linux. You can use this command:

nohup java -jar file.jar &

This is basically up to your preference and usual standard at your place. We have services in Docker that basically have exec java -jar param param... at the end of entry-point.sh script. You can run whole Tomcat, etc.

If you're using Spring Boot (which is a good idea), then java -jar is perfectly fine - you can use embedded Tomcat and skip installing dedicated application server (or, to be precise, servlet container).

Second approach is preferred over first in Jboss servers as things can be easily done through console. You can control the deployment of the application on the servers. If say you don't want to deploy the jar on all the servers part of cluster ,that can be taken care.

Related