在编写程序的时候,可能会在不同的环境来使用不同的配置文件,这样的话,就需要不断的修改配置文件中的内容,在本地使用修改为本地的配置内容,在测试环境使用,使用测试环境的配置内容,生产环境使用生产环境的配置内容。那么maven在使用不同的配置文件路径的时候,需要怎样的pom文件呢?
- 项目工程结构
- maven引入profiles文件
<!--配置资源配置文件有哪些?-->
<profiles>
<profile>
<id>production</id>
<!--<activation>-->
<!--<activeByDefault>true</activeByDefault>-->
<!--</activation>-->
<properties>
<env>production</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>local</id>
<properties>
<env>local</env>
</properties>
</profile>
</profiles>
在bulid中引入:
<!--打包要打包进来的资源配置文件-->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/config/${env}</directory>
</resource>
</resources>
- maven编译使用命令
打包时,使用maven package -P production 生产环境的配置文件内容,-P test 测试环境的配置文件内容,-P local 本地环境的配置文件内容。