Maven - 实例-4-依赖传递

这里以Eclipse创建Maven工程来演示。

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项目

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

aaa

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

bbb

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

ccc

<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安装在本地仓库。

编译aaa:
选中pom.xml文件,鼠标右键Run AS--->Maven build---》在弹出的界面,填写Goals内容:compile,然后Run
如果需要再次运行,可以在填写Goals内容:clean compile,然后Run

安装aaa:
选中pom.xml文件,鼠标右键Run AS--->Maven install

[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] ------------------------------------------------------------------------
posted @ 2017-12-08 22:42  Anliven  阅读(367)  评论(0编辑  收藏  举报