欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot将项目打包成war包的操作方法

程序员文章站 2024-02-25 23:26:33
1 修改项目打包类型 在pom.xml里,项目打包类型将jar设置成war: war...

1 修改项目打包类型

在pom.xml里,项目打包类型将jar设置成war:

<packaging>war</packaging>

2 移除内置tomcat容器

在pom.xml里设置:

<dependencies>
    <!--web启动器依赖-->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
      <!--移除默认启动容器-->
      <exclusions>
        <exclusion>
          <artifactid>spring-boot-starter-tomcat</artifactid>
          <groupid>org.springframework.boot</groupid>
        </exclusion>
      </exclusions>
    </dependency>

3 添加servlet-api依赖

若项目的某些工具类会用到该依赖,如果缺失,会报错:

/tool/webutil.java:[6,26] 程序包javax.servlet.http不存在

需要在pom.xml里添加如下依赖:

<dependency>  
    <groupid>javax.servlet</groupid>  
    <artifactid>javax.servlet-api</artifactid>  
    <version>3.1.0</version>  
    <scope>provided</scope>
</dependency>

或者下面依赖(任选其一):

<dependency>
  <groupid>org.apache.tomcat</groupid>
  <artifactid>tomcat-servlet-api</artifactid>
  <version>8.0.36</version>
  <scope>provided</scope>
</dependency>

4 修改项目启动类

spring boot入口类必须实现springbootservletinitializer接口的configure方法才能让外部容器运行spring boot项目。

原入口类mainapplication.java内容如下:

package com.maxbill;
import com.maxbill.core.desktop.desktopapp;
import org.mybatis.spring.annotation.mapperscan;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.servlet.servletcomponentscan;
import org.springframework.context.configurableapplicationcontext;
@springbootapplication
@mapperscan("com.maxbill.base.dao")
public class mainapplication extends desktopapp {
  public static configurableapplicationcontext context;
  public static void main(string[] args) {
    //启动后台服务
    context = springapplication.run(mainapplication.class, args);
    //启动桌面服务
    launch(args);
  }
}

修改后内容如下:

package com.maxbill;
import com.maxbill.core.desktop.desktopapp;
import org.mybatis.spring.annotation.mapperscan;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.servlet.servletcomponentscan;
import org.springframework.context.configurableapplicationcontext;
import org.springframework.boot.web.servlet.support.springbootservletinitializer;    
import org.springframework.boot.builder.springapplicationbuilder;
@springbootapplication
@mapperscan("com.maxbill.base.dao")
public class mainapplication extends springbootservletinitializer{
  @override
  //修改启动类,继承 springbootservletinitializer并重写 configure方法
  protected springapplicationbuilder configure(springapplicationbuilder builder){
    //注意这里要指向原先用main方法执行的application启动类
    return builder.sources(mainapplication.class);
  }
  public static configurableapplicationcontext context;
  public static void main(string[] args) {
    //启动后台服务
    context = springapplication.run(mainapplication.class, args);
    //启动桌面服务
    launch(args);
  }
}

5 打包部署项目

maven执行命令跳过测试打包

mvn clean package -dskiptests

build信息如下

[info] building war: d:\workspace\maxbill-redisplus-master\redisplus\target\redisplus-0.0.1-snapshot.war
[info]
[info] --- spring-boot-maven-plugin:2.0.4.release:repackage (default) @ redisplus ---
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------

build成功后,在项目target目录下把war包部署到tomcat的webapps目录下,例如:

d:\program files\apache software foundation\tomcat 8.0\webapps

启动tomcat服务,在浏览器访问

:[端口号]/[打包项目名]/

总结

以上所述是小编给大家介绍的spring boot将项目打包成war包的操作方法,希望对大家有所帮助