1.pom文件bulid配置
<build> <filters> <filter>src/main/filters/${env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!-- springboot把${}改成了@@,使用一下配置可以恢复成${}接收变量 <plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> </plugin> --> </plugins> </build>
2.pom文件中profiles配置
<profiles> <profile> <id>dev</id> <properties> <env>development</env> </properties> <!--默认使用开发环境--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> <profile> <id>pre</id> <properties> <env>preProduct</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>product</env> </properties> </profile> </profiles>
3.src/main/filters/目录下开发环境配置距离,文件名development.properties
#开发环境配置 ###### 数据源配置 maven.datasource.ip=192.12.0.90 maven.datasource.port=1234 maven.datasource.database=test maven.datasource.username=dev maven.datasource.password=7576 ######日志配置 maven.logback.logpath=/data/logs/car maven.logback.root.level=INFO maven.logback.rolling.trace.level=INFO maven.logback.rolling.file.level=INFO maven.logback.console.level=INFO
4.resources目录下properties和xml文件接收变量
spring.datasource.username=@maven.datasource.username@ spring.datasource.password=@maven.datasource.password@
<filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>@maven.logback.rolling.file.level@</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter>
<property name="LOG_PATH" value='@maven.logback.logpath@' />5.按环境配置打包命令举例:
mvn clean -DskipTests=true package -P prod