• Springboot的项目如何打成war包


    1、在SpringBoot中默认支持Tomcat容器,所以当一个SpringBoot项目打包生成*.jar文件,并且直接执行的时候就会自动启动内部的Tomcat容器。除了此种模式之外,也可以将Web项目打包为*.war文件,采用部署的形式通过Tomcat进行发布处理,这种方式和传统模式比较类似,打成war包丢到tomcat里面进行运行。

    2、在将SpringBoot打包为*.war文件的时候,如果想正常部署一定要注意以下两点:

      第一点:是取消项目中的Jetty容器的配置。

      第二点:是将所有的源文件夹目录设置输出资源,修改父pom.xml中的<resource>配置。千万注意,创建WEB-INF/web.xml配置文件,不然会提示报错的哦。

    3、开始修改pom.xml配置文件,将程序的打包类型定义为*.war,修改pom.xml配置文件,追加war文件打包插件。

      1 <?xml version="1.0"?>
      2 <project
      3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
      4     http://maven.apache.org/xsd/maven-4.0.0.xsd"
      5     xmlns="http://maven.apache.org/POM/4.0.0"
      6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      7     <modelVersion>4.0.0</modelVersion>
      8 
      9     <parent>
     10         <groupId>com.bie</groupId>
     11         <artifactId>springboot-base</artifactId>
     12         <version>0.0.1-SNAPSHOT</version>
     13     </parent>
     14 
     15     <!-- 父项目已经指定,这里可以省略 -->
     16     <!-- <groupId>com.bie</groupId> -->
     17     <artifactId>springboot-tentent</artifactId>
     18     <!-- <version>0.0.1-SNAPSHOT</version> -->
     19     <name>springboot-tentent</name>
     20     <url>http://maven.apache.org</url>
     21     <!-- 将Springboot项目打包成war包的形式 -->
     22     <packaging>war</packaging>
     23 
     24     <properties>
     25         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     26     </properties>
     27 
     28     <dependencies>
     29         <dependency>
     30             <groupId>org.springframework.boot</groupId>
     31             <artifactId>spring-boot-starter-web</artifactId>
     32         </dependency>
     33         <dependency>
     34             <groupId>org.springframework.boot</groupId>
     35             <artifactId>spring-boot-starter-test</artifactId>
     36             <scope>test</scope>
     37         </dependency>
     38         <dependency>
     39             <groupId>junit</groupId>
     40             <artifactId>junit</artifactId>
     41             <scope>test</scope>
     42         </dependency>
     43         <dependency>
     44             <groupId>org.springframework.boot</groupId>
     45             <artifactId>spring-boot-starter-jetty</artifactId>
     46         </dependency>
     47     </dependencies>
     48 
     49     <build>
     50         <plugins>
     51             <!-- 该插件的主要功能是进行项目的打包发布处理 -->
     52             <plugin>
     53                 <groupId>org.springframework.boot</groupId>
     54                 <artifactId>spring-boot-maven-plugin</artifactId>
     55                 <!-- 设置程序执行的主类 -->
     56                 <configuration>
     57                     <mainClass>org.springboot.tentent.Springboot01Application</mainClass>
     58                 </configuration>
     59                 <executions>
     60                     <execution>
     61                         <goals>
     62                             <goal>repackage</goal>
     63                         </goals>
     64                     </execution>
     65                 </executions>
     66             </plugin>
     67             <plugin>
     68                 <groupId>org.apache.maven.plugins</groupId>
     69                 <artifactId>maven-war-plugin</artifactId>
     70                 <configuration>
     71                     <!-- 打包后的war文件名称 -->
     72                     <warName>springboot-tentent</warName>
     73                 </configuration>
     74             </plugin>
     75         </plugins>
     76         <resources>
     77             <resource>
     78                 <directory>src/main/resources</directory>
     79                 <includes>
     80                     <include>**/*.properties</include>
     81                     <include>**/*.yml</include>
     82                     <include>**/*.xml</include>
     83                     <include>**/*.tld</include>
     84                 </includes>
     85                 <filtering>false</filtering>
     86             </resource>
     87             <resource>
     88                 <directory>src/main/java</directory>
     89                 <includes>
     90                     <include>**/*.properties</include>
     91                     <include>**/*.xml</include>
     92                     <include>**/*.tld</include>
     93                 </includes>
     94                 <filtering>false</filtering>
     95             </resource>
     96         </resources>
     97     </build>
     98 
     99 
    100 </project>

    修改完pom.xml文件之后,更新项目会报错提示缺少web.xml配置文件,此时创建一个web.xml配置文件即可。

    如果现在项目要以Tomcat的形式运行,那么需要修改SpringBoot程序启动类定义,该类必须要继承SpringBootServletInitializer父类,同时还需要覆写configure()方法。 

     1 package org.springboot.tentent;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.boot.builder.SpringApplicationBuilder;
     6 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
     7 
     8 @SpringBootApplication // 启动Springboot程序,自带子包扫描
     9 public class Springboot01Application extends SpringBootServletInitializer {
    10 
    11     @Override
    12     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    13         // 配置Springboot的应用环境
    14         SpringApplicationBuilder sources = builder.sources(Springboot01Application.class);
    15         return sources;
    16     }
    17 
    18     public static void main(String[] args) {
    19         SpringApplication.run(Springboot01Application.class, args);
    20     }
    21 
    22 }

    对项目进行打包部署(clean package),成功之后会在target目录中形成xxx.war程序文件,随后可以将此文件直接复制到Tomcat所在目录之中,而后启动Tomcat进行项目发布。

    生成的xxx.war包在target目录下面,由于我的使用的maven创建父子工程,所以生成了两个,如下所示:

    此时可以将此文件直接复制到Tomcat所在目录(apache-tomcat-8.5.34webapps)之中,而后启动Tomcat进行项目发布。

  • 相关阅读:
    SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
    SPOJ GSS3 Can you answer these queries III ——线段树
    SPOJ GSS2 Can you answer these queries II ——线段树
    SPOJ GSS1 Can you answer these queries I ——线段树
    BZOJ 2178 圆的面积并 ——Simpson积分
    SPOJ CIRU The area of the union of circles ——Simpson积分
    HDU 1724 Ellipse ——Simpson积分
    HDU 1071 The area ——微积分
    HDU 4609 3-idiots ——FFT
    BZOJ 2194 快速傅立叶之二 ——FFT
  • 原文地址:https://www.cnblogs.com/biehongli/p/13875201.html
Copyright © 2020-2023  润新知