简体   繁体   English

在 Eclipse 中从 Spring:boot 项目创建 war 文件

[英]Create war file from Spring:boot project in Eclipse

I am pretty new to Spring Boot and I have completed a application that works well on my localhost.我是 Spring Boot 的新手,我已经完成了一个在我的本地主机上运行良好的应用程序。 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.正如我被告知将它部署在我的本地主机之外,例如 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.当我尝试在 Eclipse 中导出到 war 文件时,Eclipse 找不到我的项目。 It can find it if I try to export Java>JAR FILE but not if I try Web>WAR FILE如果我尝试导出 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 文件以部署到外部服务器?

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您不需要为战争文件生成添加 ****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).复制此战争并将其部署到任何 Web/应用程序服务器(我已将其重命名为 demo.war)。

You can access the app by using your host name followed by the port and app name.您可以使用主机名后跟端口和应用程序名称来访问该应用程序。

在此处输入图片说明

在 STS (Spring Tools Suite) 中生成 WAR 文件:Run as-> Maven Install

If you want to deploy spring boot project as war file you need follow three steps:如果要将 spring boot 项目部署为 war 文件,则需要执行以下三个步骤:

  1. Extends SpringBootServletInitializer扩展 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>
    1. Update packaging to war pom.xml将打包更新为 war pom.xml

       packaging war packaging
    2. Marked the embedded servlet container as provided.将嵌入式 servlet 容器标记为提供。 inside pom file.在 pom 文件中。

      Reference : Deploy a Spring Boot WAR to Tomcat Server参考: 将 Spring Boot WAR 部署到 Tomcat 服务器

After change to <packaging>war</packaging> in pom.xml .pom.xml更改为<packaging>war</packaging>后。

utilize the command: mvn package使用命令: mvn package

您也可以通过选择菜单File -> Export -> Web -> War file

I generated a war file by the following steps:我通过以下步骤生成了一个war文件:

  1. Add above dependency.添加上面的依赖。
  2. Open command prompt.打开命令提示符。
  3. cd into your project directory. cd进入你的项目目录。
  4. Run the command: mvn clean install .运行命令: mvn clean install

Then a war file will be generated in the target directory.然后会在目标目录下生成一个war文件。

If you are using maven, you can add tag to specify the type we want to create - jar or war.如果您使用的是 maven,您可以添加标签来指定我们要创建的类型 - jar 或 war。 After that in sts you trigger a maven build and specify the goals - clean install and specify the number of threads you want to run for the build.之后在 sts 中触发 maven 构建并指定目标 - 全新安装并指定要为构建运行的线程数。 In case you want to skip the test case you can give skipTest =true, or if you to run from command prommpt you can give -Dmaven.test.skip=true.如果你想跳过测试用例,你可以给skipTest =true,或者如果你从命令提示符运行,你可以给-Dmaven.test.skip=true。 Once done, war file will be generated under target folder.完成后,war 文件将在目标文件夹下生成。 (unless we specify a specific deployment folder) (除非我们指定特定的部署文件夹)

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

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