一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Maven項目繼承實現過程圖解

Maven項目繼承實現過程圖解

2020-08-05 23:29護花使者 Java教程

這篇文章主要介紹了Maven項目繼承實現過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

多個maven項目之間難免有重復的pom配置,重復的配置沒必要重復寫,maven提供了父子繼承的關系,重復的依賴直接放在父項目的pom中。

所以不希望每個開發者隨意定義maven版本依賴,可以在父項目中進行說明,然后子項目沿用即可。

idea創建父項目(這是一個父項目,也是一個空項目,只需要pom.xml,編寫相關的依賴, 父項目必須用pom打包的方式):

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
<?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>com.linewell</groupId>
  <artifactId>maven-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--父項目必須是pom-->
  <packaging>pom</packaging>
 
  <!--定義參數-->
  <properties>
    <common.version>2.6</common.version>
    <spring.version>4.3.6.RELEASE</spring.version>
  </properties>
 
  <!--這邊的依賴子項目會繼承-->
  <dependencies>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>${common.version}</version>
    </dependency>
  </dependencies>
 
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>spring-context-support</groupId>
        <artifactId>org.springframework</artifactId>
        <version>${spring.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
</project>

這邊需要說明下,dependencyManagement,這邊的依賴不會被繼承,如果子項目導入了這個依賴,可以不用寫版本號,會以父項目的為主,因為有的子項目不一定會用父項目中的所有依賴。個別子項目依賴到的包可以放在這里,然后不需要寫版本號,會自動引用父項目。

創建一個子項目,編輯子項目的pom.xml如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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>com.linewell</groupId>
  <artifactId>maven-children</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <parent>
    <groupId>com.linewell</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../mavenparent/pom.xml</relativePath>
  </parent>
 
</project>

可以看到commons-io進來了,spring-context-support沒進來。

我現在不添加spring-context-support的版本,然后看下結果,是會以父項目的版本為主。可以看到如下引入的也是父項目中的4.3.6

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>
 
  <parent>
    <groupId>com.linewell</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../mavenparent/pom.xml</relativePath>
  </parent>
 
  <groupId>com.linewell</groupId>
  <artifactId>maven-children</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
    </dependency>
  </dependencies>
</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
<?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>
 
  <parent>
    <groupId>com.linewell</groupId>
    <artifactId>maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../mavenparent/pom.xml</relativePath>
  </parent>
 
  <groupId>com.linewell</groupId>
  <artifactId>maven-children</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <dependencies>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
  </dependencies>
 
</project>

Maven項目繼承實現過程圖解

ps:如果父項目中執行了mvn install安裝到了本地倉庫,然后子項目中引入父GAV的時候可以不用寫路徑relativePath屬性。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/chenmz1995/p/12794052.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产馆| 日韩欧美一区二区在线观看 | 日本videosdesexo乱 | 日本欧美一二三区色视频 | 亚洲swag精品自拍一区 | 多人群p全肉小说 | 亚洲天堂2015| 32d乳白色的奶罩未删除 | 特黄一级大片 | 91嫩草私人成人亚洲影院 | 深夜福利影院在线观看 | 精品丰满人妻无套内射 | 欧美色图日韩色图 | 欧美式禁忌 | 423hk四虎| 日本精品久久久久久久久免费 | 亚洲AV无码乱码在线观看浪潮 | 国产精品怡红院在线观看 | 亚洲国产成人精品 | 亚洲AV精品无码喷水直播间 | 婷婷去我也去 | 欧美亚洲国产一区二区三区 | 国产精品一区二区久久不卡 | 国产欧美va欧美va香蕉在线观看 | 黄 色 成 年人在线 幻女free性俄罗斯第一次摘花 | 91精品综合| 国产福利一区二区三区四区 | 精品亚洲视频在线 | 精油按摩日本 | 私人影院在线播放 | 北岛玲在线播放 | 欧美乱理伦另类视频 | kkkk4444在线看片 | 日b在线 | 美女精品永久福利在线 | 91尤物在线视频 | 欧美巨吊 | kkkk4444在线看片免费 | 草莓视频深夜释放 | 精品图区| 午夜影视在线观看 |