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

时间:2022-09-10 12:55:52

1 修改项目打包类型

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

?
1
<packaging>war</packaging>

2 移除内置tomcat容器

在pom.xml里设置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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里添加如下依赖:

?
1
2
3
4
5
6
<dependency> 
    <groupid>javax.servlet</groupid> 
    <artifactid>javax.servlet-api</artifactid> 
    <version>3.1.0</version> 
    <scope>provided</scope>
</dependency>

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

?
1
2
3
4
5
6
<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内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
  }
}

修改后内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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服务,在浏览器访问

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

总结

以上所述是小编给大家介绍的spring boot将项目打包成war包的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/ycyzharry/p/9713228.html