1、创建springboot项目,然后创建对应文件
2、在pom.xml中定义全局配置信息
<profiles> <profile> <id>test</id> <!--为env起一个名字--> <properties> <env>test</env> </properties> <activation> <activeByDefault>true</activeByDefault> <!--默认加载uat,实际根据环境需求--> </activation> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!--build标签中添加如下的配置--> <resources> <resource> <directory>src/main/resources</directory> <!--指定文件下--> <filtering>true</filtering> <!--动态配置--> <excludes> <!--去除多余的文件--> <exclude>application.yml</exclude> <exclude>application-test.yml</exclude> <exclude>application-prod.yml</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <!--动态配置 ${env}--> <includes> <!--包含加载的文件(env 默认加载uat 具体看配置)--> <include>application-${env}.yml</include> <include>application.yml</include> </includes> </resource> </resources> </build>
3、打包测试
1、使用命令:mvn clean package -P参数
p:就是前面定义的profiles文件,-p后面的参数就是profile中配置的id值
例如执行命令:
E:\workspace\user-account>mvn clean package -Ptest