Differences between jar and war in Spring Boot?

时光毁灭记忆、已成空白 提交于 2019-12-03 12:05:04

问题


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?


回答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.




回答2:


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




回答3:


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.




回答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)



来源:https://stackoverflow.com/questions/45165428/differences-between-jar-and-war-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!