这里以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] ------------------------------------------------------------------------

Maven - 实例-4-依赖传递的更多相关文章

  1. maven scope 以及依赖传递

    https://www.cnblogs.com/mxm2005/p/4947905.html

  2. Maven中的依赖相关总结

    一.Maven的依赖作用域scope compile:编译.测试.运行都会依赖,会打进包中. rumtime:不存于编译,后期运行和测试都会参与,会打进包中. test:只在test classpat ...

  3. 项目开发中Maven的单向依赖-2022新项目

    一.业务场景 工作多年,在真实的项目开发中经常会遇到将一个项目拆分成多个工程的情况,比如将一个真实的项目拆分成controller层,service层, dao层,common公共服务层等等.这样拆分 ...

  4. Maven依赖传递、依赖传递排除、依赖冲突

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628429.html  一:Maven依赖传递 假如有Maven项目A,项目B依赖A,项目C依赖B.那么我们可 ...

  5. 理解maven项目的pom.xml文件中,<scope>标签的作用——作用域以及依赖传递

    问题介绍: 在maven项目中,最关键的就是pom.xml这个文件,这个文件是用来导入maven项目依赖的jar包以及一些插件等. 在这个文件中导入jar包使用的标签是<dependency&g ...

  6. maven课程 项目管理利器-maven 3-8 maven依赖传递 4星

    本节主要讲了 1 maven依赖传递 本地项目路径:F:\xiangmu3\Xin\FuQiang\maven\code 2 maven排除依赖 3 注意事项 4 零散知识点 1 maven依赖传递 ...

  7. 快速发现并解决maven依赖传递冲突

    此文已由作者翟曜授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在测试过程中,遇到了几次maven传递依赖冲突的问题,所以记录下解决的过程,遇到类似问题供参照. 问题现象 ...

  8. 说说maven依赖冲突,依赖调解,依赖传递和依赖范围

    说maven依赖冲突之前需要先说说maven的 依赖传递. 依赖传递 当前项目引入了一个依赖,该依赖的依赖也会被引入项目.更加准确的说法是,maven会解析直接依赖的POM,将那些必要的间接依赖,以传 ...

  9. Java开发学习(二十九)----Maven依赖传递、可选依赖、排除依赖解析

    现在的项目一般是拆分成一个个独立的模块,当在其他项目中想要使用独立出来的这些模块,只需要在其pom.xml使用<dependency>标签来进行jar包的引入即可. <depende ...

随机推荐

  1. Java学习笔记(十五):import关键字

  2. python下划线的5种含义

    本文介绍了Python中单下划线和双下划线("dunder")的各种含义和命名约定,名称修饰(name mangling)的工作原理,以及它如何影响你自己的Python类. 单下划 ...

  3. 禁止 pdf 下载、打印的方法

    https://blog.csdn.net/For_GG/article/details/78616063 https://blog.csdn.net/xiangcns/article/details ...

  4. Python+Selenium学习--自动生成HTML测试报告

    前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果.        HTMLTestRunner 是Python 标准库的unit ...

  5. [leetcode]36. Valid Sudoku验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  6. linux下apt安装mysql导致mysql.user table is damaged

    笔者在ubuntu下用 apt install mysql-server类似的命令安装mysql, 安装了最新版的mysql5.7,覆盖了操作系统内置的数据库mysql系统库. 最初启动mysql出错 ...

  7. python基础之Day12

    一.闭包函数 什么是闭包函数? 闭:函数是一个内部函数 包:指的是该函数包含对外部作用域(非全局作用域)名字的引用. 给函数传值的方式有两种: 1.使用参数直接给函数传值 2.包给函数 1 2 3 4 ...

  8. You are using pip version 9.0.1, however version 9.0.3 is available.

    1,pip不能用了,提示:You are using pip version 8.1.1, however version 9.0.1 is available.网上搜索了一箩筐的安装程序,各种安装, ...

  9. HTML语言

    复习: 1.Web项目的部署结构  静态Web技术(客户端技术):提供的内容任何人在任何时间访问都是一样的 HTML/CSS/JS/Flash.... 动态Web技术(服务器端技术):提供的内容不同人 ...

  10. P3258 [JLOI2014]松鼠的新家 (简单的点差分)

    https://www.luogu.org/problemnew/show/P3258 注意开始和最后结尾 #include <bits/stdc++.h> #define read re ...