springboot项目打包
一、 查看项目版本对应的tomcat
比如我项目的父级依赖版本为 2.2.8.RELEASE
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
<relativePath/>
</parent>
1.查看你的项目maven依赖存放的地址
这个地址是配置在你的maven-conf-settings.xml中
然后根据 maven依赖地址\org\springframework\boot\spring-boot-dependencies\2.2.8.RELEASE\spring-boot-dependencies-2.2.8.RELEASE.pom
2.打开这个pom文件,搜索“tomcat.version” 会找到:
<tomcat.version>9.0.36</tomcat.version>
根据这个去下载对应版本的tomcat,这样适配较好,使用不对应版本可能会有问题出现.
二、打包,这里是打war包
1.基础信息修改
<groupId>com.xyz</groupId>
<artifactId>application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xyz</name>
<!--一般默认为jar,改为war方式-->
<packaging>war</packaging>
<description>xyz link,jdk1.8,version2.28,2021-5-20</description>
2.排除内置的Tomcat容器
使用的方式
<!--最终打成war包,排除内置的tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
网上看到的其他方式(没用过,不清楚)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3. 修改项目名称
<build>
<finalName>test</finalName>
</build>
要注意的是:包名应该和application.properties的 server.servlet.context-path 保持一致
server.servlet.context-path=/test
4.注册启动类
修改启动类继承SpringBootServletInitializer类,并重写configure方法:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
}
5.打包
这里是环境是eclipse,主要讲eclipse中的操作
双清
1.project —> clean… —>选择要打包的项目
2.右键项目,run as–maven clean
3.右键项目,run as–maven install
到这里,不出问题的话,应该是打包成功了.一个叫target的文件夹会出现在你的项目路径里,里面会有war包.