如何構建Project: Intro to Maven
---
> Maven是常用的build tool, 依循預定義的資料結構(predifined folder sturcture),主要的特色為套件管理, 套件有分direct(需配置)和transitive(傳遞依賴), 透過pom.xml, maven指令進行操作; 另外常見的build tool還有gradle
- 套件管理(Frameworks & libraries used in a project),`使用pom.xml設定`, 指定要載入的library(dependency):
- Parent Pom: spring-boot-starter-parent
- dependency management套件版控: spring-boot-dependencies
- 套件denpendencies: 中央repository藉由artifact及group Id做錨定
- 來源單位groupId
- 成品jar檔artifactId
- 版本version: 通常parent會管控部分套件版本,若有特殊需求或其他未納管的dependencies會用到此tag
- 執行範圍scope:
- compile(default)
- test: 運行時不需要, 單純為test purpose, 常見在Test dependencies
- provided: at runtime by JDK or a container, 例如部署在container上的web app, 環境已提供部分libraries
- runtime: 在編譯code的時候不需要但運行時需要,但例如JDBC driver
- system(is deprecated): 和provided相似, 需要再額外指定到system特定的jar
- import: 只在pom中可用, 表示此套件需替換為POM當中的有效套件
- 排除套件exclusions:
```xml
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
<version>1.31.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
```
- Properties: java.version, plugin and configuration
- name of project: 辨識及控制版本
- version x.xx.x: 主版本(大更動), 次版本(增功能), 增量版本(修正bugs)
- 1.0.0-SNAPSHOT: 不穩定版, 可無限次上傳remote repos, 可覆蓋, 有此後綴則為SNAPSHOT版, 若無則視為RELEASE版
- 1.0.3-RELEASE: 穩定版, 只能上傳一次, 不可覆蓋
```xml
<groupId>com.yahsin</groupId>
<artifactId>learn-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learn-maven</name>
<description>Demo project for Spring Boot</description>
```
Maven從哪裡取得這些套件
---
- remote repository: NEXUS管理software的artifacts, 可以download 或publish artifact到這
- local repository: 從遠端拉下來的各種jar
- 成品預設放在根目錄: ~/.m2/repository, 隱藏資料夾
- 遠端倉庫設定: ~/.m2/settings.xml
Maven生命週期與執行緒
---
當執行Maven command時, 會使用maven build life cycle, 週期為各自獨立的序列steps,分有3條生命週期
1. Clean Lifecycle(3 phase): 清理專案之前建置或打包產生的檔案, 一般就是把target資料夾刪除
2. Default Lifecycle(23 Phase): 建構專案
1. Validate: 檢查專案資訊正確
2. Compile: 編譯原始碼
3. Test: 透過unit test framwork執行Unit test
4. Package: 將編譯後的內容打包成jar(常用)或war
5. Verify: 檢查測試結果
6. Install: install jar to target file/local repository
7. Deploy: 將成品(.jar)上傳到remote repository
3. Site Lifecycle(4 Phase): 建立專案站點, 用於產生一些專案說明文件的網站,會將生成html的頁面作為網站的說明文件。
REF:
- Spring Boot教學 By古君葳
- Micro Service By28minutes
- [Maven Dependency Scopes](https://www.baeldung.com/maven-dependency-scopes)
- [What is the difference between Nexus and Maven?](https://stackoverflow.com/questions/23082621/what-is-the-difference-between-nexus-and-maven)
- [(1) Maven的生命週期(Phase, Plugin, Goal)](https://medium.com/learning-from-jhipster/1-maven%E7%9A%84%E7%94%9F%E5%91%BD%E9%80%B1%E6%9C%9F-phase-plugin-goal-d69a2591dc45)
- [Introduction to the Build Lifecycle](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)