springboot 项目jar转war

前言

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打印日志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值