Differences between jar and war in Spring Boot?

48,350

Solution 1

Spring Boot can be told to produce a 'fat JAR' which includes all of your module/service's dependencies and can be run with java -jar <your jar>. See "Create an executable JAR with Maven" here.

Spring Boot can also be told to produce a WAR file, in which case you'll likely choose to deploy it to a web container such as Tomcat or Jetty.

Plenty more details on Spring Boot deployment here.

Solution 2

Depends on your deployment. If you are planning to deploy your application to an existing Java EE Application Server (e.g. Tomcat), then standard approach is to perform a war build.

When you use fat jar approach, your application will be deployed on embedded application container provided by spring boot. Conduct Deploying Spring Boot Applications for more information.

Solution 3

Running spring-boot application as fat *.jar

It is possible to build so called fat JAR that is executable *.jar file with embedded application container (Tomcat as default option). There are spring-boot plugins for various build systems. Here is the one for maven: spring-boot-maven-plugin

To execute the kind of fat *.jar you could simple run command:

java -jar *.jar

Or using spring-boot-maven goal:

mvn spring-boot:run

Building spring-boot application as *.war archive

The other option is to ship your application as old-fashioned war file. It could be deployed to any servlet container out there. Here is step by step how-to list:

  1. Change packaging to war (talking about maven's pom.xml)
  2. Inherit main spring-boot application class from SpringBootServletInitializer and override SpringApplicationBuilder configure(SpringApplicationBuilder) method (see javadoc)
  3. Make sure to set the scope of spring-boot-starter-tomcat as provided

More info in spring-boot documentation

Solution 4

I was under the same problem, when I deployed my jar issue free on my local. Then I had to demo it on the server. You can create a war file by changing the pom.xml , tag

<packaging>jar</packaging>

to

<packaging>war</packaging>

and you will have a war file in your target which you can deploy to your server(tomcat in my case)

Solution 5

If you need to deploy it in an external container, you'll normally have to create a war file (which doesn't have to be executable).

If you want to use the embedded container, you can choose to create an executable .jar file or an executable .war file. AFAIK the only difference is in the layout of the archive, and therefore normally also the layout of your source repository.

E.g. using standard folder structure with Maven / Gradle, static resources for a .jar will need to be in src/main/resources/static while for a .war file they should be in src/main/webapp.

Share:
48,350
Hama Saadwn
Author by

Hama Saadwn

Updated on June 19, 2021

Comments

  • Hama Saadwn
    Hama Saadwn almost 3 years

    I'm about to build my first website in Java with Spring Framework using Spring Boot and it's much easier to build it in jar, but I have a few questions about it.

    What are the differences in general?

    In jar files the views are under /resources/templates, but in war file it's under /webapp/WEB-INF/.

    What are the differences? Can I deploy a jar on an online host?

  • P.J.Meisch
    P.J.Meisch almost 7 years
    and don't forget to exclude the embedded tomcat in this case; check the link in @lazyneuron's answer
  • rilaby
    rilaby almost 7 years
    @HamaSaadwn just fyi, spring-boot application build as war could also be run as java -jar xxx.war. It could be used in some transition phase when you're making changes to your deployment infrastructure with moving to fat jar
  • Hama Saadwn
    Hama Saadwn almost 7 years
    do the same steps apply to gradle?
  • rilaby
    rilaby almost 7 years
    @HamaSaadwn absolutely!
  • Ashkan Rahmany
    Ashkan Rahmany about 6 years
    Thank you, but in case of fat jar, how we can configure tomcat? for example configure ssl?
  • rilaby
    rilaby about 6 years
    @ashkanr see the list of spring-boot properties here docs.spring.io/spring-boot/docs/current/reference/html/…. Basically, properties are the common way to configure embedded Tomcat instance.
  • Derick Daniel
    Derick Daniel over 4 years
    This is the proper answer (stating the difference) to the question amongst all.
  • herman
    herman about 4 years
    You can create an executable war file as well, in which case it works the same as a jar but with a war layout. So this isn't really the difference between both.
  • herman
    herman about 4 years
    Whether it's "fat" (i.e. containing all dependencies) and executable or not, does not depend on whether it's a .jar or .war file. This currently accepted answer does not explain the difference between both (which is what was asked).
  • D B
    D B about 3 years
    What is the difference? You didn't answer the question!!!
  • Ray Jasson
    Ray Jasson about 3 years
    ... while for a .war file they should be in src/main/webapp.
  • herman
    herman about 3 years
    @HolmesQueen good catch, I've updated the answer.
  • Manoj K Sardana
    Manoj K Sardana over 2 years
    why a person will choose to deploy on an external server ? is not that means loosing on the beauty of sprint boot which was suppose to have auto initializing the server..
  • lee
    lee about 2 years
    the real question was never answered? I thought the original question was about the difference...