項目中經常會把項目部署到多個環境,例如本地和遠程生產環境。
如果每次部署之前都手動修改配置文件會顯得太土錘,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