灵活使用Maven Profile
- 2019 年 10 月 3 日
- 笔记
???????Maven?profile?????????????????????????????????????????????????????????SpringMVC????????Spring Boot??????SpringMVC????
??????Profile?????????????????????
1 Profile?????
?????????Profile???????????????
1.1 Profile??
????pom.xml??????profile????????????
<profiles> <profile> <id>dev</id> <properties> <active.profile>dev</active.profile> <database.host>localhost</database.host> </properties> </profile> <profile> <id>test</id> <properties> <active.profile>test</active.profile> <database.host>test.codestory.tech</database.host> </properties> </profile> <profile> <id>prod</id> <properties> <active.profile>prod</active.profile> <database.host>prod.codestory.tech</database.host> </properties> </profile> </profiles> |
1.2 ???????
?????????????????? src/main/resources/config??props.properties ???????
database.pool.host=${database.host} |
?pom.xml??? resources ?????????????
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering><!– ????? –> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> <overwrite>true</overwrite><!– ????????? –> </configuration> </plugin> </plugins> </build> |
?? maven ????? profile ????????????? target/classes ??????????profile??mvn ????????
mvn clean resources:resources -P dev
database.pool.host=localhost |
mvn clean resources:resources -P test
database.pool.host=test.codestory.tech |
mvn clean resources:resources -P prod
database.pool.host=prod.codestory.tech |
1.3 ??????
????properties?????????????????profile?????????????profile?????
????profile?????????src/main/resources ????? profiles ?????????????dev/test/prod???????????props.properties????????
src/main/resources/profiles/dev/props.properties
database.pool.host=localhost |
src/main/resources/profiles/test/props.properties
database.pool.host=test.codestory.tech |
src/main/resources/profiles/prod/props.properties
database.pool.host=prod.codestory.tech |
????resources-plugin??? overwrite ???? src/main/resources/config/props.properties ?????????
database.pool.host=${database.host} database.pool.port=3306 |
?pom.xml???resources????
<resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>profiles/**</exclude> </excludes> <filtering>true</filtering><!– ????? –> </resource> <resource> <directory>src/main/resources/profiles/${active.profile}</directory> <targetPath>config</targetPath> <filtering>false</filtering><!– ??????????? –> </resource> </resources> |
????maven resources??????????
mvn clean resources:resources -P dev
database.pool.host=localhost |
????????? database.pool.port=3306 ??????????????????????????
2 ??????profile
??????????????????????????????????????????
2.1 ???test????
?????????props.properties?????????????????????(???????????????????webdav?????????)?
database.pool.host=${database.host} filesystem.path.root=${path.root} |
??????????????BUG???????test???????????????????????????? -P test???Tomcat??????????????????????????????pom.xml??profile[test]?path.root?????????????profile?????????????
2.2 ??profile ????????
???????????profile???????????????????profile??????????
pom.xml?profiles?????
<profiles> <profile> <id>local</id> <properties> <active.profile>local</active.profile> <path.root>d:/develop/attachments</path.root> </properties> </profile> <profile> <id>dev</id> <properties> <active.profile>dev</active.profile> <database.host>localhost</database.host> <path.root>d:/develop/attachments</path.root> </properties> </profile> <profile> <id>test</id> <properties> <active.profile>test</active.profile> <database.host>test.codestory.tech</database.host> <path.root>/app/attachments</path.root> </properties> </profile> <profile> <id>prod</id> <properties> <active.profile>prod</active.profile> <database.host>prod.codestory.tech</database.host> <path.root>/app/attachments</path.root> </properties> </profile> </profiles> |
????profile??-P?????????????????????local??????test????????? local?????(???pom.xml????<directory>src/main/resources/profiles/${active.profile}</directory>??resource??)
mvn clean resources:resources -P test,local
database.pool.host=test.codestory.tech filesystem.path.root=/app/attachments |
??????????????????????????test????????????????????????? https://zhidao.baidu.com/question/139071460381210925.html ??????profile?????????????????????????????????
????? pom.xml?profiles?????local???????????
mvn clean resources:resources -P test,local
database.pool.host=test.codestory.tech filesystem.path.root= d:/develop/attachments |
2.3 ??profile????
??????????????????????????profiles/{active.profile}????????????????????????profile?????env-dev.properties/env-test.properties /env-prod.properties?
???????profile??
mvn clean resources:resources -P test
?target/classes/config ??????????? env-test.properties?props.properties???????????????????????? src/main/resources/profiles/test ???????
????profile?????? target/classes/config????????? props.properties?????? src/main/resource/config/props.properties???????????
mvn clean resources:resources -P test,local
database.pool.host=test.codestory.tech filesystem.path.root=d:/develop/attachments |
???????? src/main/resource/config/props.properties ???????activeProfiles???????
database.pool.host=${database.host} filesystem.path.root=${path.root} active.profiles=${active.profile} |
mvn clean resources:resources -P test,local
database.pool.host=test.codestory.tech filesystem.path.root=d:/develop/attachments active.profiles=local |
?????????????active.profile???????? local????????? test ?????????
2.4 ??profile??????
?maven?pom???????profile??????build??????pom.xml?profiles???????
<profiles> <profile> <id>dev</id> <properties> <active.profile>dev</active.profile> <database.host>localhost</database.host> <path.root>d:/develop/attachments</path.root> </properties> <build> <resources> <resource> <directory>src/main/resources/profiles/dev</directory> <targetPath>config</targetPath> <filtering>false</filtering> </resource> </resources> </build> </profile> <profile> <id>test</id> <properties> <active.profile>test</active.profile> <database.host>test.codestory.tech</database.host> <path.root>/app/attachments</path.root> </properties> <build> <resources> <resource> <directory>src/main/resources/profiles/test</directory> <targetPath>config</targetPath> <filtering>false</filtering> </resource> </resources> </build> </profile> <profile> <id>prod</id> <properties> <active.profile>prod</active.profile> <database.host>prod.codestory.tech</database.host> <path.root>/app/attachments</path.root> </properties> <build> <resources> <resource> <directory>src/main/resources/profiles/prod</directory> <targetPath>config</targetPath> <filtering>false</filtering> </resource> </resources> </build> </profile> <profile> <id>local</id> <properties> <active.profile>local</active.profile> <path.root>d:/develop/attachments</path.root> </properties> </profile> </profiles> |
????????profile????????????????????????????? ${active.profile}??????profile???????????<directory>src/main/resources/profiles/${active.profile}</directory>?????
mvn clean resources:resources -P test,local
?target/classes/config ??????????? env-test.properties?props.properties??????????
????????????????????test??????????props.properties?????????????????????????????????????????????????????????
3 ????????????????Profiles
?????????active.profiles=${active.profile}???????????profile?id????????????profile???????????active.profiles=test,local???????
???????maven???????? activeProfiles??????????? active.profiles=${activeProfiles}
mvn clean resources:resources -P test,local
active.profiles=[Profile {id: test, source: pom}, Profile {id: local, source: pom}] |
?????????????????????${activeProfiles}?????????????????????????????????????????? active.profiles=test,local
4 ?Maven?settings.xml???profile
????pom.xml???profile?????maven/conf/settings.xml????????profile??????????profile??????????active.profile.label????local?test??????
<profiles> <profile> <id>local</id> <properties> <active.profile>local</active.profile> <active.profile.label>settings profile local</active.profile.label> <filesystem.path.root>d:/develop/attachments</filesystem.path.root> </properties> </profile> <profile> <id>test</id> <properties> <active.profile>test</active.profile> <active.profile.label>settings profile test</active.profile.label> </properties> </profile> </profiles> |
????profiles.txt????????????????????????#??????
############################################### active.profiles=${activeProfiles} ############################################### active.profile.label=${active.profile.label} ############################################### |
????
mvn clean resources:resources -P test,local
############################################### active.profiles=[Profile {id: test, source: pom}, Profile {id: local, source: pom}, Profile {id: local, source: settings.xml}, Profile {id: test, source: settings.xml}] ############################################### active.profile.label=settings profile test ############################################### |
?????????pom.xml?settins.xml??????id?profile???????????? pom.xml??Profiles????settings.xml??profiles??????????????????????????