前言
一般來說在專案開發上,會因為需要而產生許多小專案,而這些小專案會重複使用到許多相同的依賴,如若交給各個小專案分別管理,可能會造成每個小專案使用的依賴套件的版本不同,因此會希望透過統一管理的方式進行。楊藝先前整理「Apache Maven 依賴傳遞」文章,透過統一依賴專案的方式統一套件,不過透過這種方式會造成專案間有著很嚴重的依賴,而並非每個專案都想要繼承所有依賴,若透過依賴排除的方式進行篩選,會造成依賴管理的複雜性,因此就得透過Dependency Management統一進行依賴與版本的管理。
楊藝在整理「Apache Maven Repository & Coordinates 概念」提到專案的打包方式(pom/jar/war,預設為jar),若要使用Dependency Management統一管理套件與套件版本就得使用pom的方式進行專案打包,子專案要使用則透過 <parent>
繼承上層專案設定檔。實際上楊藝目前工作使用的Spring Boot也是透過一樣的方式進行專案配置,在Spring Boot專案會有一段如下配置。
1
2
3
4
5
6
| <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
|
實作
以上就是繼承官方的Spring Boot設定檔,隨後在自己的專案內若使用到Spring Boot相關依賴,只需要於<dependency>
內撰寫<groupId>
以及<artifactId>
即可,無須撰寫<version>
設定。在實作上需要建立一個專案打包方式設定為pom,並在pom.xml檔案內設定 <dependencyManagement>
,楊藝在此建立一個名為artyang-boot專案,pom.xml設定如下所示。
artyang-boot專案Maven設定檔: pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cc.artyang.book</groupId>
<artifactId>artyang-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 定義屬性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<org.apache.poi.version>4.1.2</org.apache.poi.version>
<commons.io.version>2.6</commons.io.version>
<org.apache.commons.version>3.10</org.apache.commons.version>
<mysql.connector.version>8.0.19</mysql.connector.version>
</properties>
<!-- 統一依賴配置 -->
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${org.apache.poi.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${org.apache.commons.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
|
隨後繼承以上pom.xml設定檔的專案,不會直接繼承依賴,而依然需要透過撰寫<dependency>
指定使用依賴,如下所示。
artyang-maven-project專案Maven設定檔: pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cc.artyang.blog</groupId>
<artifactId>artyang-maven-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<!-- 繼承上層 pom.xml 設定 -->
<parent>
<groupId>cc.artyang.book</groupId>
<artifactId>artyang-boot</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
|
若未來對<dependency>
中的套件版本有所更變,可透過再定義另一個上層專案,使用不同版號區分,進行統一管理,不建議直接修改原上層檔案,否則會對底層專案直接造成影響。