1. 程式人生 > 程式設計 >Maven專案繼承實現過程圖解

Maven專案繼承實現過程圖解

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

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

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

Maven專案繼承實現過程圖解

編輯父專案pom.xml

<?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如下:

<?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

<?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,如下可以發現,如果子專案有明確指定依賴以及具體版本,與父專案發生衝突會以子專案的依賴為準。

<?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屬性。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。