# [Spring Boot] maven 打包 jar 後... ###### tags: `Spring` `maven` `Spring Boot` `docker` `Java` [TOC] ## 讀取順序 1. config/application.properties(專案根目錄中 config 內) 2. config/application.yml 3. application.properties(專案根目錄中) 4. application.yml 5. resources/config/application.properties(專案 resources 目錄內的 config 目錄內) 6. resources/config/application.yml 7. resources/application.properties(專案內 resources 目錄內) 8. resources/application.yml 資料來源:[SpringBoot配置文件——加载顺序](https://www.feilewu.cn/#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%89%80%E5%9C%A8%E7%9B%AE%E5%BD%95) ## 運行時加載配置檔 ### 加載指定的設定檔 #### 1. 在 pom.xml 加入設定 ```=xml <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring.profiles.active>dev</spring.profiles.active> </properties> </profile> <profile> <id>prod</id> <properties> <spring.profiles.active>prod</spring.profiles.active> </properties> </profile> <profile> <id>winnie</id> <properties> <spring.profiles.active>winnie</spring.profiles.active> </properties> </profile> </profiles> ``` #### 2. 分別在檔案內加入下面設定 - src/main/resources/application.properties ``` spring.profiles.active=@spring.profiles.active@ my.name=application ``` - src/main/resources/application-dev.properties ``` my.name=dev ``` - src/main/resources/application-prod.properties ``` my.name= ``` - src/main/resources/application-winnie.properties ``` my.name=winnie ``` #### 3. 加入 IndexController 的 @Value 標注 ```=Java @RestController public class HomeController { @Value("${spring.profiles.active}") private String myProfile; @Value("${my.name}") private String myName; @GetMapping("/") public void index() { System.out.println("my name: " + this.myProfile); System.out.println(this.myName); } } ``` #### 4. 執行 現在有 4 個設定檔,分別是 default、dev、prod 與 winnie。 嘗試指定winnie ``` mvn clean spring-boot:run -Pwinnie $ curl localhost:8080 my name: winnie winnie ``` 或 ``` java -jar test.jar --spring.profiles.active=master ``` >spring.profiles.active為指定配置檔的名稱,如 master.yml --- 資料來源: [透過 Spring Profiles 切換不同的應用程式屬性檔](https://blog.miniasp.com/post/2022/09/21/Mastering-Spring-Boot-Profiles) ### 加載外部設定檔 若設定檔在外部,則可透過命令指定啟動的配置檔路徑(spring.config.location)與檔案名稱(spring.config.name) ``` 檔案路徑如下 -config/ -application.properties -winnie.properties -test.jar ``` ```=! java -jar test.jar --spring.config.location=config/ --spring.config.name=application,winnie ``` ## 打包後自動建置 Docker image #### 1. 於 `pom.xml` 加入此段依賴 ```= <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-image</goal> </goals> <configuration> <image> <name>demo-test</name> </image> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` goal - 動作 configuration - 設定 #### 2. 打包 jar ```=cmd mvn spring-boot:build-image ``` #### 3. 輸入 `docker images` 即可看到新增的映象檔 --- [SpringBoot的設定【組態檔、載入順序、設定原理】(超詳細)](https://tw511.com/a/01/26225.html#Profile_749) [深入淺出 Spring Boot 多重設定檔管理 (Spring Profiles)](https://blog.miniasp.com/post/2022/09/21/Mastering-Spring-Boot-Profiles)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up