默認(rèn)情況下,我們 spring boot 項(xiàng)目的配置文件<small>(application.yaml、application.properties)</small>是在項(xiàng)目的 jar 包『里面』的。
如果是要改配置文件中的配置項(xiàng)時(shí),就需要將項(xiàng)目重新打包,在某些情況下,這就顯得十分不方便。
對(duì)此,我們可以將 spring boot 項(xiàng)目的配置文件『挪到』jar 包之外,然后再啟動(dòng) spring boot 項(xiàng)目時(shí)再指定它使用外部的這些配置文件。
-
為 pom.xml 添加插件及配置
<plugin> <!-- copy資源文件 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/application*.yml</include> <include>**/application*.properties</include> <include>**/bootstrap.yml</include> <include>**/logback*.xml</include> </includes> <excludes> <exclude>**/i18n/**</exclude> <exclude>**/mybatis/**</exclude> <exclude>**/public/**</exclude> <exclude>**/static/**</exclude> <exclude>**/templates/**</exclude> </excludes> </resource> </resources> <outputDirectory>${project.build.directory}/resources</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- 打jar包時(shí)忽略配置文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>**/application*.yml</exclude> <exclude>**/application*.properties</exclude> <exclude>**/logback*.xml</exclude> <exclude>**/bootstrap.yml</exclude> </excludes> </configuration> </plugin>
根據(jù)上述的 <outputDirectory> 的配置,相關(guān)的配置文件會(huì)被復(fù)制到 target 下的 resources 目錄中,并且,jar 包中也不會(huì)包含你所配置的這些配置文件。
這種情況下,在啟動(dòng) spring boot 項(xiàng)目時(shí),需要額外的參數(shù)(-Dspring.config.location)告訴它項(xiàng)目的配置文件在哪:
java -jar -Dspring.config.location=E:/Workspace/projects/target/resources/ ./xxx.jar
注意:
如果外部文件指定的是目錄,那么
-Dspring.config.location配置的地址必須以/結(jié)尾,表明這個(gè)配置是一個(gè)目錄,而非單個(gè)文件;如果想要指定單個(gè)文件,可以使用
-Dspring.config.location=E:/Workspace/projects/target/resources/application.properties這種方式來(lái)指明只需要加載這個(gè)配置文件內(nèi)容。當(dāng)然,你可以可以將配置文件放在與啟動(dòng)的 jar 包同級(jí)目錄,或者在啟動(dòng)的jar的同級(jí)目錄下建一個(gè)名為 config 的目錄,然后將配置文件放到里面,然后直接使用
java -jar ./xxx.jar命令啟動(dòng),spring 默認(rèn)會(huì)去./config目錄中查詢。
補(bǔ)充
spring boot 默認(rèn)是以 classpath:/,classpath:/config/,file:./,file:./config/ 這樣的配置在查找、加載配置文件,有意思的是查找順序是上述配置的反向順序:
- file:./config/
- file:./
- classpath:/config/
- classpath:/
因此,如果你在 spring.config.location 中也定義了多個(gè)配置文件位置,例如:classpath:/custom-config/,file:./custom-config/, 那么配置文件的查找、加載順序同樣是反向的:
- file:./custom-config
- classpath:/custom-config
另外,還有一個(gè)功能相似的配置 spring.config.additional-location,使用它的話,它會(huì)作為默認(rèn)配置路徑的『擴(kuò)展配置』路徑來(lái)使用。擴(kuò)展的配置路徑會(huì)比默認(rèn)的配置優(yōu)先被掃描到. 比如說(shuō), 如果設(shè)置了擴(kuò)展的配置文件所在路徑為:classpath:/custom-config/,file:./custom-config/ , 那么查找路徑將會(huì)是下面的順序:
- file:./custom-config/
- classpath:custom-config/
- file:./config/
- file:./
- classpath:/config/
- classpath:/
這種掃描順序使得你可以通過(guò)自己的自定義配置來(lái)修改默認(rèn)的配置項(xiàng)。