Maven - 實例-4-依賴傳遞

這裏以Eclipse建立Maven工程來演示。apache

Setp-1 建立Maven項目

File ---> New ---> Maven Project
---> 默認勾選"Use default Workspace location",點擊Next
--->選擇maven-archetype-quickstart,點擊Next
---> 設置必要的項目參數, 而後點擊Finish建立Maven工程瀏覽器

  • Group Id : 項目組織的惟一標識符,實際對應JAVA的包結構,通常都是組織域名的反寫形式
  • Artifact Id : 項目的惟一標識符,實際對應項目的名稱,就是項目根目錄的名稱
  • Version : 項目的版本
  • Pacaking : 項目的打包方式,默認爲jar

這裏咱們依次創建aaa、bbb、ccc三個Maven項目maven

<groupId>testDep.AAA</groupId>
  <artifactId>aaa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <groupId>testDep.BBB</groupId>
  <artifactId>bbb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <groupId>testDep.CCC</groupId>
  <artifactId>ccc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

Setp-2 創建依賴

編寫pom.xml文件,並創建依賴關係
bbb依賴aaa,ccc依賴bbb : aaa--->bbb--->cccui

aaaurl

<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>testDep.AAA</groupId>
  <artifactId>aaa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>aaa</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

bbbspa

<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>testDep.BBB</groupId>
  <artifactId>bbb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bbb</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>testDep.AAA</groupId>
      <artifactId>aaa</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

ccc3d

<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>testDep.CCC</groupId>
  <artifactId>ccc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ccc</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>testDep.BBB</groupId>
      <artifactId>bbb</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>    
  </dependencies>
</project>

Setp-3 編譯並安裝到本地倉庫

注意:
須要引入的依賴jar包必須在倉庫存在,不然會提示報錯。這裏經過mvn install安裝在本地倉庫。code

編譯aaa:
選中pom.xml文件,鼠標右鍵Run AS--->Maven build---》在彈出的界面,填寫Goals內容:compile,而後Run
若是須要再次運行,能夠在填寫Goals內容:clean compile,而後Runxml

安裝aaa:
選中pom.xml文件,鼠標右鍵Run AS--->Maven installblog

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building aaa 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ aaa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\aaa\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ aaa ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ aaa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\aaa\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ aaa ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ aaa ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\aaa\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running testDep.AAA.aaa.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ aaa ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ aaa ---
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\aaa\target\aaa-0.0.1-SNAPSHOT.jar to D:\DownLoadFiles\apache-maven-repo\testDep\AAA\aaa\0.0.1-SNAPSHOT\aaa-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\aaa\pom.xml to D:\DownLoadFiles\apache-maven-repo\testDep\AAA\aaa\0.0.1-SNAPSHOT\aaa-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.416 s
[INFO] Finished at: 2017-10-23T17:39:34+08:00
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------

安裝bbb

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building bbb 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bbb ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\bbb\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ bbb ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ bbb ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\bbb\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ bbb ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\bbb\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ bbb ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\bbb\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running testDep.BBB.bbb.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ bbb ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\bbb\target\bbb-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ bbb ---
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\bbb\target\bbb-0.0.1-SNAPSHOT.jar to D:\DownLoadFiles\apache-maven-repo\testDep\BBB\bbb\0.0.1-SNAPSHOT\bbb-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\bbb\pom.xml to D:\DownLoadFiles\apache-maven-repo\testDep\BBB\bbb\0.0.1-SNAPSHOT\bbb-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.139 s
[INFO] Finished at: 2017-10-23T17:42:54+08:00
[INFO] Final Memory: 16M/212M
[INFO] ------------------------------------------------------------------------

編譯ccc

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building ccc 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ccc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\ccc\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ccc ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\ccc\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.928 s
[INFO] Finished at: 2017-10-23T17:43:37+08:00
[INFO] Final Memory: 15M/293M
[INFO] ------------------------------------------------------------------------

Setp-4 查看依賴傳遞結果

經過包瀏覽器能夠看到各自的Maven依賴關係。

  • bbb依賴aaa,引入了aaa
  • ccc依賴bbb,而且經過bbb間接依賴aaa,所以引入了aaa

Setp-5 排除依賴

在ccc項目的pom文件中添加以下內容,來排除aaa的依賴

<exclusions> 
      <exclusion>
        <groupId>testDep.AAA</groupId>        
        <artifactId>aaa</artifactId> 
      </exclusion> 
    </exclusions>

修改pom.xml文件

<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>testDep.CCC</groupId>
  <artifactId>ccc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ccc</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>testDep.BBB</groupId>
      <artifactId>bbb</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    <exclusions> 
      <exclusion>
        <groupId>testDep.AAA</groupId>        
        <artifactId>aaa</artifactId> 
      </exclusion> 
    </exclusions>        
    </dependency> 
  </dependencies>
</project>

依賴關係發生改變,再也不引入aaa。

編譯ccc

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building ccc 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ccc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\ccc\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ccc ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.217 s
[INFO] Finished at: 2017-10-23T17:57:03+08:00
[INFO] Final Memory: 8M/245M
[INFO] ------------------------------------------------------------------------
相關文章
相關標籤/搜索