简体   繁体   English

Spring Boot中jar和war的区别?

[英]Differences between jar and war in Spring Boot?

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吗?

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> .可以告诉 Spring Boot 生成一个“fat JAR”,其中包含所有模块/服务的依赖项,并且可以使用java -jar <your jar> See "Create an executable JAR with Maven" here .请参阅此处的“使用 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 部署的更多详细信息,请访问此处

Depends on your deployment.取决于您的部署。 If you are planning to deploy your application to an existing Java EE Application Server (eg 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.当您使用 fat jar 方法时,您的应用程序将部署在 Spring Boot 提供的嵌入式应用程序容器上。 Conduct Deploying Spring Boot Applications for more information.执行部署 Spring Boot 应用程序以获取更多信息。

Running spring-boot application as fat *.jarspring-boot应用程序作为 fat *.jar

It is possible to build so called fat JAR that is executable *.jar file with embedded application container ( Tomcat as default option).可以使用嵌入式应用程序容器( Tomcat作为默认选项)构建所谓的fat JAR ,它是可执行的*.jar文件。 There are spring-boot plugins for various build systems.有适用于各种构建系统的spring-boot插件。 Here is the one for maven : spring-boot-maven-plugin这是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 archivespring-boot应用程序构建为*.war存档

The other option is to ship your application as old-fashioned war file.另一种选择是将您的应用程序作为老式的war文件发送。 It could be deployed to any servlet container out there.它可以部署到任何 servlet 容器中。 Here is step by step how-to list:以下是分步操作方法列表:

  1. Change packaging to war (talking about maven's pom.xml )packaging改成war (说到 maven 的pom.xml
  2. Inherit main spring-boot application class from SpringBootServletInitializer and override SpringApplicationBuilder configure(SpringApplicationBuilder) method (see javadoc )SpringBootServletInitializer继承主要的spring-boot应用程序类并覆盖SpringApplicationBuilder configure(SpringApplicationBuilder)方法(参见javadoc
  3. Make sure to set the scope of spring-boot-starter-tomcat as provided确保将spring-boot-starter-tomcatscope设置为provided

More info in spring-boot documentation spring-boot 文档中的更多信息

I was under the same problem, when I deployed my jar issue free on my local.当我在本地免费部署 jar 问题时,我遇到了同样的问题。 Then I had to demo it on the server.然后我不得不在服务器上演示它。 You can create a war file by changing the pom.xml , tag您可以通过更改 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)

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.如果要使用嵌入式容器,可以选择创建可执行的.jar 文件或可执行的.war 文件。 AFAIK the only difference is in the layout of the archive, and therefore normally also the layout of your source repository. AFAIK 唯一的区别在于存档的布局,因此通常也是源存储库的布局。

Eg 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 .例如,在 Maven/Gradle 中使用标准文件夹结构,.jar 的静态资源需要在src/main/resources/static而对于 .war 文件,它们应该在src/main/webapp

war file is a Web Application Archive which runs inside an application server while a .jar is Java Application Archive that runs a desktop application on a user's machine. war文件是在应用程序服务器内运行的Web 应用程序存档,而.jar是在用户计算机上运行桌面应用程序的Java 应用程序存档 A war file is a special jar file that is used to package a web application to make it easy to deploy it on an application server. war 文件是一种特殊的 jar 文件,用于打包 Web 应用程序,以便轻松地将其部署到应用程序服务器上。

Simple and easy answer.简单易行的答案。 Packaging type jar represented as standalone application and packaging type war represented as web application.打包类型jar表示为独立应用程序,打包类型war表示为 Web 应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM