springboot 开发部署指定不同的配置文件

本文详细介绍如何在Maven项目中配置多环境(profiles),包括开发(dev)和生产(prod)环境,通过在pom.xml中定义不同的profiles来切换数据库配置等参数。文章还解释了如何在不同环境下激活特定的配置文件,并提供了应用到实际项目中的步骤,如修改application.properties文件以适配不同环境,以及如何使用Maven命令进行打包。
  • 在pom.xml中配置profile
    <dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>6.0.6</version>
    		</dependency>
    		<!-- springdatajpa -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-jpa</artifactId>
    		</dependency>
    	</dependencies>
    
    	<profiles>
    		<profile>
    			<!-- 本地开发环境 -->
    			<id>dev</id>
    			<properties>
    				<activefile>dev</activefile>
    			</properties>
    			<activation>
    				<activeByDefault>true</activeByDefault>
    			</activation>
    		</profile>
    		<profile>
    			<!-- 生产环境 -->
    			<id>prod</id>
    			<properties>
    				<activefile>prod</activefile>
    			</properties>
    		</profile>
    	</profiles>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>

    profiles标签下可配置多个配置文件,默认配置文件添加activation标签,值为ture

  • 配置文件

        src/main/resources/application.properties(汇总配置文件 用以配置不同环境下使用不同的配置文件)

spring.profiles.active=@activefile@
server.port=8080

src/main/resources/application-dev.properties (开发环境配置)

数据库配置=xxx
xxx=xxx

src/main/resources/application-prod.properties (生产环境配置)

数据库配置=xxx
xxx=xxx
  • 打包执行

  打包生产环境jar

mvn package -P prod

  打包开发环境jar

mvn package -P dev

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值