Java Spring Boot中jar和war的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45165428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Differences between jar and war in Spring Boot?
提问by Hama Saadwn
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.
我即将使用 Spring Boot 使用 Spring 框架在 Java 中构建我的第一个网站,在jar
.
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/
.
在jar
文件中,视图在 下/resources/templates
,但在war
文件中它在/webapp/WEB-INF/
.
What are the differences? Can I deploy a jar
on an online host?
有什么区别?我可以jar
在在线主机上部署一个吗?
采纳答案by glytching
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 生成一个“胖 JAR”,其中包含所有模块/服务的依赖项,并且可以使用java -jar <your jar>
. 请参阅此处的“使用 Maven 创建可执行 JAR” 。
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.
还可以告诉 Spring Boot生成 WAR 文件,在这种情况下,您可能会选择将其部署到 Web 容器,例如 Tomcat 或 Jetty。
Plenty more details on Spring Boot deployment here.
有关 Spring Boot 部署的更多详细信息,请访问此处。
回答by lazyneuron
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.
取决于您的部署。如果您计划将应用程序部署到现有的 Java EE 应用程序服务器(例如 Tomcat),那么标准方法是执行war
构建。
When you use fat jar approach, your application will be deployed on embedded application container provided by spring boot. Conduct Deploying Spring Boot Applicationsfor more information.
当您使用 fat jar 方法时,您的应用程序将部署在 Spring Boot 提供的嵌入式应用程序容器上。执行部署 Spring Boot 应用程序以获取更多信息。
回答by rilaby
Running spring-boot
application as fat *.jar
spring-boot
以胖方式运行应用程序*.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
可以使用嵌入式应用程序容器(作为默认选项)构建所谓fat JAR
的可执行*.jar
文件Tomcat
。有spring-boot
各种构建系统的插件。这是一个maven
:spring-boot-maven-plugin
To execute the kind of fat
*.jar
you could simple run command:
要执行那种fat
*.jar
你可以简单的运行命令:
java -jar *.jar
Or using spring-boot-maven
goal:
或使用spring-boot-maven
目标:
mvn spring-boot:run
Building spring-boot
application as *.war
archive
将spring-boot
应用程序构建为*.war
存档
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:
另一种选择是将您的应用程序作为老式war
文件发送。它可以部署到任何 servlet 容器。以下是分步操作方法列表:
- Change
packaging
towar
(talking about maven'spom.xml
) - Inherit main
spring-boot
application class fromSpringBootServletInitializer
and overrideSpringApplicationBuilder configure(SpringApplicationBuilder)
method (see javadoc) - Make sure to set the
scope
ofspring-boot-starter-tomcat
asprovided
- 更改
packaging
为war
(谈论 maven 的pom.xml
) spring-boot
从继承主应用程序类SpringBootServletInitializer
并覆盖SpringApplicationBuilder configure(SpringApplicationBuilder)
方法(请参阅javadoc)- 确保将
scope
of设置spring-boot-starter-tomcat
为provided
回答by Groot
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
当我在本地免费部署 jar 问题时,我遇到了同样的问题。然后我不得不在服务器上演示它。您可以通过更改 pom.xml 标签来创建一个 war 文件
<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)
并且您的目标中将有一个战争文件,您可以将其部署到您的服务器(在我的情况下为 tomcat)
回答by herman
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).
如果需要将其部署在外部容器中,通常必须创建一个 war 文件(不必是可执行的)。
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.
如果要使用嵌入式容器,可以选择创建可执行的.jar 文件或可执行的.war 文件。AFAIK 唯一的区别在于存档的布局,因此通常也是源存储库的布局。
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 .jar file they should be in src/main/webapp
.
例如,在 Maven/Gradle 中使用标准文件夹结构,.jar 的静态资源需要在src/main/resources/static
.jar 文件中,而对于 .jar 文件,它们应该在 .jar 中src/main/webapp
。