在 Eclipse 中从 Spring:boot 项目创建 war 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47908312/
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
Create war file from Spring:boot project in Eclipse
提问by bajen micke
I am pretty new to Spring Boot and I have completed a application that works well on my localhost. As I have been told to deploy it outside my localhost on for example a webbhotel or simular I need to export the project as a war-file and not as a jar-file.
我是 Spring Boot 的新手,我已经完成了一个在我的本地主机上运行良好的应用程序。正如我被告知将它部署在我的本地主机之外,例如 webbhotel 或 simular,我需要将项目导出为 war 文件而不是 jar 文件。
UPDATE!! I run the project as a Springproject generated in Spring Initialzr and using Eclipse as a IDE.
更新!!我将项目作为在 Spring Initialzr 中生成的 Springproject 运行,并使用 Eclipse 作为 IDE。
In Eclipse I have followed the steps
在 Eclipse 中,我已按照以下步骤操作
<packaging>war</packaging>
and
和
<dependencies>
<!-- … -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- … -->
</dependencies>
from Spring Boot Referencepage https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
来自 Spring Boot 参考页面 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
In my project I use
在我的项目中,我使用
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Do I need to add the sprinng-boot-starter-tomcat dependency and add provided to that on aswell as tomcat-embed-jasper so that my dependency will be like this?
我是否需要添加 spring-boot-starter-tomcat 依赖项并在 tomcat-embed-jasper 上添加提供的依赖项,以便我的依赖项像这样?
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
When I try to export to war-file in Eclipse, Eclipse can't find my project. It can find it if I try to export Java>JAR FILE but not if I try Web>WAR FILE
当我尝试在 Eclipse 中导出到 war 文件时,Eclipse 找不到我的项目。如果我尝试导出 Java>JAR FILE 可以找到它,但如果我尝试导出 Web>WAR FILE 则找不到
Do anyone know what I am doing wrong and if it is neccesary to export to a WAR-file to deploy to a external server?
有谁知道我做错了什么,是否需要导出到 WAR 文件以部署到外部服务器?
回答by Neeraj Benjwal
You need to extend ****SpringBootServletInitializer**** in your @SpringBootApplication
您需要在@SpringBootApplication 中扩展 ****SpringBootServletInitializer****
You don't need to add the ****sprinng-boot-starter-tomcat**** dependency for war file generation
您不需要为War文件生成添加 ****spring-boot-starter-tomcat**** 依赖项
Use below configuration in pom.xml
在 pom.xml 中使用以下配置
<packaging>war</packaging>
Configure Build Path for your project and select JDK
为您的项目配置构建路径并选择 JDK
Right click on project > Run As > Maven Install
右键单击项目 > 运行方式 > Maven 安装
It will generate the war file inside target folder.
它将在目标文件夹中生成war文件。
Copy this war and deploy to any web/application server (I have renamed it to demo.war).
复制此War并将其部署到任何 Web/应用程序服务器(我已将其重命名为 demo.war)。
You can access the app by using your host name followed by the port and app name.
您可以使用主机名后跟端口和应用程序名称来访问该应用程序。
回答by NELSON RODRIGUEZ
To generate WAR file in STS (Spring Tools Suite): Run as-> Maven Install
在 STS (Spring Tools Suite) 中生成 WAR 文件:Run as-> Maven Install
回答by Anuj Dhiman
If you want to deploy spring boot project as war file you need follow three steps:
如果要将 spring boot 项目部署为 war 文件,则需要执行以下三个步骤:
Extends SpringBootServletInitializer
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
if you are using multiple main class then define startup one
<start-class>com.javavogue.DemoApplication</start-class>
Update packaging to war pom.xml
packaging war packaging
Marked the embedded servlet container as provided. inside pom file.
Reference : Deploy a Spring Boot WAR to Tomcat Server
扩展 SpringBootServletInitializer
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
如果您使用多个主类,则定义启动类
<start-class>com.javavogue.DemoApplication</start-class>
将包装更新为 war pom.xml
packaging war packaging
将嵌入式 servlet 容器标记为提供。在 pom 文件中。