Spring Boot将项目打包成war包的操作方法
作者:ycyzharry 时间:2022-01-02 22:30:50
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服务,在浏览器访问
http://localhost:[端口号]/[打包项目名]/
总结
以上所述是小编给大家介绍的Spring Boot将项目打包成war包的操作方法网站的支持!
来源:http://www.cnblogs.com/ycyzharry/p/9713228.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot插件spring tool suite安装及使用详解
![](https://img.aspxhome.com/file/2023/4/71844_0s.png)
JAVA实现的CrazyArcade泡泡堂游戏
![](https://img.aspxhome.com/file/2023/6/63156_0s.png)
Spring钩子接口汇总分析使用示例
代码分析Android消息机制
Java中的BaseTypeHandler自定义类型转换器的使用
Spring注解驱动开发实现属性赋值
C# Dynamic之:ExpandoObject,DynamicObject,DynamicMetaOb的应用(上)
![](https://img.aspxhome.com/file/2023/1/74811_0s.jpg)
springboot常用注释的讲解
如何使用Spring AOP的通知类型及创建通知
![](https://img.aspxhome.com/file/2023/2/61242_0s.png)
java 获取字节码文件的几种方法总结
Mybatis核心组成部分之SQL映射文件揭秘详解
Mybatis中的resultType和resultMap查询操作实例详解
![](https://img.aspxhome.com/file/2023/3/76233_0s.png)
Unity实现俄罗斯方块
![](https://img.aspxhome.com/file/2023/8/69198_0s.jpg)
SpringBoot自动装配之Condition深入讲解
jvm调优的几种场景(小结)
![](https://img.aspxhome.com/file/2023/3/60323_0s.png)
Java学习基础之安装JDK/配置JDK环境&IEDA工具安装
![](https://img.aspxhome.com/file/2023/5/60315_0s.png)
RestTemplate自定义请求失败异常处理示例解析
![](https://img.aspxhome.com/file/2023/0/60700_0s.png)
C# 键盘Enter键取代Tab键实现代码
![](https://img.aspxhome.com/file/2023/7/68107_0s.png)
nacos中的配置使用@Value注解获取不到值的原因及解决方案
![](https://img.aspxhome.com/file/2023/9/60769_0s.jpg)
Linux下动静态库的打包与使用指南(C/C++)
![](https://img.aspxhome.com/file/2023/8/68778_0s.png)