<profiles>
標簽:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
標簽:
<build>
<finalName>chain</finalName>
<filters>
<filter>conf/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
conf/dev.properties
文件:
env.db.url:jdbc:mysql://xxx.xxx.xxx.xxx:3306/dbname?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
env.db.driverClassName:com.mysql.jdbc.Driver
env.db.username:root
env.db.password:123456
conf/prod.properties
文件:
env.db.url:jdbc:mysql://xxx.xxx.xxx.xxx:3306/dbname?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
env.db.driverClassName:com.mysql.jdbc.Driver
env.db.username:root
env.db.password:123456
src/main/resources
目錄下的文件引用環(huán)境變量的方式:
備注:該目錄在pom.xml
文件中的<build>
標簽中定義
database.username=${env.db.username}
執(zhí)行打包命令:
mvn package -P <env>
src/main/resources
目錄下文件中的${env.db.username}
會直接被替換成對應(yīng)的profile
文件中的值鲫骗。
如:原來的:
database.username=${env.db.username}
打包后會直接替換成:
database.username=root