In my case let's consider a simple RESTful service created and configured with Spring Boot. This service communicates with a database (e.g. Postgres).
What is a difference between:
.jar
file and run it on my remote public host via java -jar myservice.jar
?or
.war
file and deploy it on Tomcat/Jetty?First option seems to be a lot easier, u just need to run a .jar
. In second option u need to create Tomcat instance, run it and then deploy a .war
file.
Is there any other difference? Any pros and cons of both method?
Package it to .war file and deploy it on Tomcat/Jetty?
Don't do this if at all possible. Reason: with embedded tomcat (or any other server runtime like undertow, netty, jetty etc) it's much easier to build a micro-services architecture.
As Josh Long once said in one of his Spring IO talks “make Jar, not War”
On the other hand, if you have an existing infrastructure out there with servlet containers and already there are multiple application.wars running in those servlet containers, and all you have to do is just package your application as a war file and hand it over to a release team (or rather you are forced to just reuse the existing infrastructure), then it's a different story...But, tech-world is already moving away from this practice.
Again, in the spirit of microservices, in the spirit of what spring boot intended for, I would encourage you to use embedded server and make runnable jar file rather than going with traditional deployment.
If you are dockerizing your application, you have 2 options to package your tomcat in your "final artifact" (by "final artifact", I meant docker image, not the jar. Jar is just an intermediate artifact here).
OR
Still, any day, I would go with 1st option because that make development process much easier (and this goes with out saying, on-boarding a new developer will be easier).