在pom.xml中配置profiles,对不同环境配置不同变量的值
<profiles>
<profile>
<id>develop</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<testKey>dev</testKey>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<testKey>test</testKey>
</properties>
</profile>
<profile>
<properties>
<testKey>online</testKey>
</properties>
</profile>
</profiles>
- activeByDefault 设置为true为默认环境
- properties下编写参数变量,是变量名,随意命名
在pom文件的build的plugins下新增如下plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/classes</outputDirectory>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>${*}</delimiter>
</delimiters>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- delimiter 为匹配字符(${testKey})
- include为要mvn编译并替换变量的文件
配置文件中的使用
env=${testKey}
验证
使用mvn打包,查看target文件夹中的配置文件是否替换:
打包:mvn clean package -Dmaven.test.skip=true