项目中经常会把项目部署到多个环境,例如本地和远程生产环境。
如果每次部署之前都手动修改配置文件会显得太土锤,maven的profile正好可以解决此类问题。
直接上实例:
pom.xml
<profiles>
<profile>
<!-- 本地环境 -->
<id>local</id>
<properties>
<jdbcUrl>jdbc:postgresql://127.0.0.1/cloudData</jdbcUrl>
<dataFileOutputPath>E:\\dataMigration</dataFileOutputPath>
<quartzUrl>jdbc:postgresql://127.0.0.1:5432/quartz</quartzUrl>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 生产环境 -->
<id>production</id>
<properties>
<jdbcUrl>jdbc:postgresql://***.**.**.***/GPSData</jdbcUrl>
<dataFileOutputPath>D:\\dataMigration</dataFileOutputPath>
<quartzUrl>jdbc:postgresql://***.**.**.***:5432/quartz</quartzUrl>
</properties>
</profile>
</profiles>
配置文件中以占位符标识变量:
jdbcUrl=${jdbcUrl}
# dataFile path
dataFileOutputPath=${dataFileOutputPath}
打包时指定profile,配置文件中的变量即可被替换
maven package -P local
或
maven package -P production