springboot打war包指定配置环境

springboot打war包指定配置环境

参考网址:

springboot项目打包方式

https://editor.csdn.net/md/?articleId=109153530

说明:

最近项目是一个springboot项目,但是是打war包部署到服务器里面的,遇到的问题就是war包程序不好命令行单独启动指定配置文件,所以需要在打包时候指定配置文件,然后放到服务器里面运行

测试前准备

准备一个springboot测试项目,引入web依赖写一个 controller 测试方法

package com.shaoming.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
/**
 * @ClassName: HelloController
 * @Author: 邵明
 * @Date: 2021/9/6 00:27
 * @Description:
 */
@RestController//表示这是springmvc的controller层,并且这个类的所有方法都是以json格式返回给前端
@RequestMapping("/springboot")//设置这个controlle的前缀url
public class HelloController {
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);
    @Value("${server.port}")
    private String port;
    @Value("${project.message}")
    private String message;
    @Value("${spring.application.name}")
    private String projectName;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(HttpServletRequest request){
        String name = request.getParameter("name");
        log.info("项目端口号为: {}",port);
        log.info("项目名称为: {}",projectName);
        return "springboot整合测试,项目端口号为:"+port+",  项目名称为: "+projectName+
                "配置环境"+message;
    }
}

对应的springboot配置文件

application.yml

server:
  port: 8080
spring:
  application:
    name: springboot-war
  profiles:
    active: @activatedProperties@
project:
  message: default环境%

application-dev.yml

server:
  port: 8080
spring:
  application:
    name: springboot-war
  profiles:
    active: @activatedProperties@
project:
  message: dev环境%

application-test.yml

server:
  port: 8080
spring:
  application:
    name: springboot-war
  profiles:
    active: @activatedProperties@
project:
  message: test环境%

项目pom.xml配置

打包之前在pom.xml里面指定打包方式为war包

说明:

1.pom.xml最下面配置profiles标签,这是个模板

2.正常一个项目需要配置多个环境,开发(dev),测试(test),pre(预生产),pro(生产)

 <!--  配置环境  -->
    <profiles>
        <profile>
            <!-- 开发 -->
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
        </profile>
        <profile>
            <!-- 测试 -->
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
        <profile>
            <!-- 准生产 -->
            <id>pre</id>
            <properties>
                <activatedProperties>pre</activatedProperties>
            </properties>
        </profile>
        <profile>
            <!-- 生产 -->
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

打包命令

mvn clean install -DskipTests -Pdev

-DskipTests 表示测试环境

-P 指定配置环境

运行war包

前提服务器环境上要安装jdk和tomcat

省略。。。

war包复制到tomcat的webapps目录下

[root@VM-0-15-centos webapps]# pwd
/usr/local/tomcat8.5/webapps
[root@VM-0-15-centos webapps]# ls
boot-war boot-war.war docs examples host-manager layuimini manager ROOT
[root@VM-0-15-centos webapps]#

启动项目

sh /usr/local/tomcat8.5/bin/startup.sh

访问项目测试

image-20210916211354245

打包指定就是dev环境

测试成功

项目地址

项目已经提交至gitee

网址:https://gitee.com/shao_ming314/springboot-war-package-profile.git

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值