- 在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