栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1 修改项目打包类型

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

war

2 移除内置tomcat容器

在pom.xml里设置:


    
    
      org.springframework.boot
      spring-boot-starter-web
      
      
 
   spring-boot-starter-tomcat
   org.springframework.boot
 
      
    

3 添加servlet-api依赖

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

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

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

  
    javax.servlet  
    javax.servlet-api  
    3.1.0  
    provided

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


  org.apache.tomcat
  tomcat-servlet-api
  8.0.36
  provided

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:WorkspaceMaxBill-RedisPlus-masterRedisPlustargetRedisPlus-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 FilesApache Software FoundationTomcat 8.0webapps

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

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

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号