前言
springboot 项目使用IDEA 创建的,开始时未设置war类型,但实际想通过tomcat来部署项目,需要转为war。
场景:前端端分离项目,前端也需要部署,如果使用jar方式,前端项目就需要部署一个服务,使用tomcat来部署,只需要一个tomcat就可以了
实现步骤
1、修改pom.xml文件,添加war
<groupId>com.tsingshan</groupId>
<artifactId>mesreport</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mesreport</name>
<packaging>war</packaging>
2、启动类继承 SpringBootServletInitializer,重写configure方法
@SpringBootApplication
@MapperScan
public class MesreportApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MesreportApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(NyApplication.class);
}
}
3、pom.xml打包war的配置,这里<webResources> 解决私有包打包失效
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/lib/</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
4、部署到tomcat,需要去除自带tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
补充
多环境打包命令
1、application.yml如下配置,其中@profile@为占位符
spring:
profiles:
active: @profile@
2、pom.xml文件中配置profiles
<profiles>
<profile>
<id>dev</id>
<properties>
<profile>dev</profile>
</properties>
<activation>
<!-- 默认环境 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>produce</id>
<properties>
<profile>produce</profile>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>report</finalName>
</build>
打包时,需要指定war包的名称,修改pom.xml
<build>
<finalName>api</finalName>
</build>
打包命令
mvn clean install -Dmaven.test.skip=true -Pproduce
或
mvn clean package -Dmaven.test.skip=true -Pproduce
测试环境是否生效:
1、通过接口数据判断 2、代码中获取当前profile打印日志