使用maven-resources-plugin插件分环境配置
一、项目目录结构
二、pom文件中引入maven-resources-plugin插件和相关的标签
pom.xml文件添加plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <configuration> <resources> <resource> <directory>src/main/resources</directory> <!-- 先把所有环境的配置全部排除 --> <excludes> <exclude>dev/**</exclude> <exclude>prod/**</exclude> </excludes> </resource> <resource> <directory>src/main/resources/${active.profile}</directory> <!-- 再把当前环境的配置引入 --> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </configuration> </plugin>
pom.xml文件添加resource
<resources> <resource> <directory>src/main/resources</directory> <!-- 先把所有环境的配置全部排除 --> <excludes> <exclude>dev/**</exclude> <exclude>prod/**</exclude> </excludes> </resource> <resource> <directory>src/main/resources/${active.profile}</directory> <!-- 再把当前环境的配置引入 --> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources>
pom.xml文件添加profile
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <active.profile>dev</active.profile> </properties> </profile> <profile> <id>prod</id> <properties> <active.profile>prod</active.profile> </properties> </profile> </profiles>
打包命令:
mvn clean install -Pdev
mvn clean install -Pprod
参考:
1、博客,https://www.cnblogs.com/owenma/p/7999023.html
2、博客,https://www.cnblogs.com/mahuan2/p/6909521.html
3、博客,https://www.codenong.com/jsc7f81095397c/