dependencyManagement標籤在項目中的實際使用

首先,現在項目中,大家對於maven的使用,就是聚合和依賴,子類和父類之間的繼承,現在項目中遇到這樣一種情況,就是有些依賴的jar包,隨着使用的過程,版本會需要更新,如果使用的模塊比較少的話,一個模塊一個模塊去修改其實還可以(要麼修改partner的pom文件,要麼就修改引入子類的pom文件,說到這裏,在解釋一句,在parent中嚴禁直接使用depandencys預定義依賴,壞處是子model會自動繼承depandencys中所有預定義依賴,所以一般在子類裏邊引入依賴最好。),但是如果模塊很多的情況,修改起來就比較繁瑣,maven的版本管理作用就沒有意義了,我們想要的是隻修改一個地方,其他地方依賴的jar的版本就都會保持一致,所以針對這樣的場景,就需要今天要說的dependencyManagement標籤來處理了;

針對剛纔說的項目中遇到的場景,比如fastjson這個jar,版本會改變,其他的依賴版本無需處理,爲了防止以後再次改變造成所有依賴的模塊都要修改,那麼此時可以做的就是先新建一個maven模塊,裏邊存放fastjaon的版本,然後在每個模塊的父模塊裏邊引入這個新建的模塊,最後在子模塊裏邊去實際的引入,看代碼:

1:新建模塊

<?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.yuan</groupId>
  <artifactId>baseJar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--<packaging>pom</packaging>-->
  <name>baseJar</name>
  <dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.40</version>
    </dependency>
  </dependencies>
  </dependencyManagement>
</project>

2:父類模塊引用:

<?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.yuan.yuan-bottom</groupId>
  <artifactId>yuan-bottom</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>yuan-bottomtest</module>
  </modules>
  <properties>
    <!-- jdk版本 -->
    <compile_version>1.8</compile_version>
    <file_encoding>UTF-8</file_encoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.3.11.RELEASE</spring.version>
    <log4j2.version>2.6.2</log4j2.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.yuan</groupId>
        <artifactId>baseJar</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.9</version>
          <configuration>
            <skipTests>true</skipTests><!-- 編譯的時候跳過test -->
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <!-- compiler插件, 設定JDK版本 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>${compile_version}</source>
          <target>${compile_version}</target>
          <encoding>${file_encoding}</encoding>
          <showWarnings>false</showWarnings>
        </configuration>
      </plugin>
      <!-- clean插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
    </plugins>
  </build>
</project>

3:子類模塊去實際引用,無需填寫版本號

<?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">
    <parent>
        <artifactId>yuan-bottom</artifactId>
        <groupId>com.yuan.yuan-bottom</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yuan.yuan-bottomtest</groupId>
    <artifactId>yuan-bottomtest</artifactId>

    <name>yuan-bottomtest</name>

    <dependencies>
   
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>
</project>

dependencyManagement只會影響現有依賴的配置(dependencyManagement其實跟父節點屬於同級,子model的引入代碼也可以在partner模塊裏邊寫,只是這樣寫不提倡,上邊已經解釋過,所有子類會自動繼承),但不會引入依賴,即子model不會繼承parent中dependencyManagement所有預定義的depandency,只引入需要的依賴即可,簡單說就是“按需引入依賴”或者“按需繼承”

這樣配置好之後,以後fastjson的版本再次變化的話,只需要修改baseJar裏邊的版本就可以了,方便整潔;

最後想再總結一點的是,其實在最初構建項目模塊的時候,就應該先建一個類似於baseJar這樣的模塊,然後在每個新建的應用模塊裏邊統一使用,因爲是按需引入的,所以可以很好的去適配,不適用於某個模塊的版本可以自己在模塊裏邊單獨引(定製),合適的就直接引用,這樣就更加整潔了;這個以後注意下,因爲現有大家的公司使用,如果剛開始沒有注意的話,肯定都是每個模塊裏邊都是相同的引用代碼。。。

參考文章:https://www.cnblogs.com/huahua035/p/7680607.html

https://www.cnblogs.com/xuzimian/p/10235164.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章