其中一个Maven的核心特征是依赖管理。管理依赖关系变得困难的任务一旦我们处理多模块项目(包含数百个模块/子项目)。 Maven提供了一个高程度的控制来管理这样的场景。

传递依赖发现

这是很通常情况下,当一个库说A就依赖于其他库说B的情况下,另一个项目Ç想用A,则该项目需要使用库中B。

在Maven帮助下以避免这样的要求来发现所有需要的库。 Maven通过读取依赖项项目文件(pom.xml中),找出它们的依赖等。

我们只需要在每个项目POM定义直接依赖关系。 Maven自动处理其余部分。

传递依赖,包括库的图形可能会快速增长在很大程度上。可能出现情况下,当有重复的库。 Maven提供一些功能来控制传递依赖程度

Feature 描述
Dependency mediation Determines what version of a dependency is to be used when multiple versions of an artifact are encountered. If two dependency versions are at the same depth in the dependency tree, the first declared dependency will be used.
Dependency management Directly specify the versions of artifacts to be used when they are encountered in transitive dependencies. For an example project C can include B as a dependency in its dependencyManagement section and directly control which version of B is to be used when it is ever referenced.
Dependency scope Includes dependencies as per the current stage of the build
Excluded dependencies Any transitive dependency can be excluede using "exclusion" element. As example, A depends upon B and B depends upon C then A can mark C as excluded.
Optional dependencies Any transitive dependency can be marked as optional using "optional" element. As example, A depends upon B and B depends upon C. Now B marked C as optional. Then A will not use C.

依赖范围

传递依赖发现可以使用各种依赖范围如下文所述受到限制

Scope 描述
compile This scope indicates that dependency is available in classpath of project. It is default scope.
provided This scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime.
runtime This scope indicates that dependency is not required for compilation, but is required during execution.
test This scope indicates that the dependency is only available for the test compilation and execution phases.
system This scope indicates that you have to provide the system path.
import This scope is only used when dependency is of type pom. This scopes indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section.

依赖关系管理

通常情况下,我们已经一套项目在一个共同的项目下。在这种情况下,我们可以创造让所有的公共依赖一个共同的POM,然后进行分项目POMS为这个POM父。下面的例子将帮助你理解这个概念

以下是上述的依赖图的细节

  • APP-UI-WAR依赖于App-Core-lib和 App-Data-lib。

  • Root 是 App-Core-lib 和 App-Data-lib 的父类。

  • Root 定义LIB1,LIB2,Lib3作为其依赖部分依赖关系。

App-UI-WAR

<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.companyname.groupname</groupId>
<artifactId>App-UI-WAR</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Core-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Data-lib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

App-Core-lib

<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>Root</artifactId>
<groupId>com.companyname.groupname</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Core-lib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>

App-Data-lib

<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>Root</artifactId>
<groupId>com.companyname.groupname</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.groupname</groupId>
<artifactId>App-Data-lib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>

Root

<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.companyname.groupname</groupId>
<artifactId>Root</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.companyname.groupname1</groupId>
<artifactId>Lib1</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname2</groupId>
<artifactId>Lib2</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.companyname.groupname3</groupId>
<artifactId>Lib3</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>

现在,当我们建立App-UI-WAR项目,Maven会发现所有的依赖通过遍历依赖图和构建应用程序。

从上面的例子中,我们可以学到以下关键概念

  • 常见的依赖关系可以用父POM的概念被放置在一个地方。 App-Data-lib 和 App-Core-lib 项目列表在 Root 目录(见Roots包类型。它是POM).

  • 不需要Lib1, lib2, Lib3 作为依赖于 App-UI-WAR. Maven使用传递性依赖机制来管理这些细节。

 

标签:Maven    依赖管理

本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:http://www.yiibai.com/maven/maven_manage_dependencies.html

maven 学习---Maven依赖管理的更多相关文章

  1. 4.Maven概念模型,maven的生命周期,Maven坐标,依赖管理(依赖范围,依赖声明),仓库管理,私服概念

     1 maven概念模型 2 maven的生命周期,项目构建过程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg== ...

  2. maven 学习---Maven外部依赖

    现在,你也知道Maven做依赖管理使用Maven仓库的概念.但是,如果依赖是不提供任何远程存储库和中央存储库发生了什么? Maven提供为使用外部依赖的概念,应用在这样的场景. 举一个例子,让我们做以 ...

  3. Maven教程3(依赖管理)

    Maven教程2(Eclipse配置及maven项目) Maven项目,依赖,构建配置,以及构件:所有这些都是要建模和表述的对象.这些对 象通过一个名为项目对象模型(Project Object Mo ...

  4. maven 学习---Maven构建自动化-Hudson

    建立自动化定义场景,依赖项目建设过程中被启动,一旦项目生成成功完成,以确保相关的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序,网页UI和应用程序的桌面UI ...

  5. maven 学习---Maven教程

    Apache Maven是一个软件项目管理和综合工具.基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件. 本教程将介绍如何使用Maven在Java开发,或任何其 ...

  6. maven 学习---Maven添加远程仓库

    默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资源库 添加Java.ne ...

  7. maven 学习---Maven中央存储库

    当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载. 首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源, 如果没有找到,然后把它会 ...

  8. maven 学习---Maven安装配置

    想要安装 Apache Maven 在Windows 系统上, 只需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : JDK 1.8 ...

  9. Maven解读:项目依赖管理如何优化

    Github地址:https://github.com/zwjlpeng/Maven_Detail Maven最大的好处莫过于其强大的依赖管理系统,在Pom配置文件中指定项目需要的Jar包的坐标,Ma ...

随机推荐

  1. [转]Oracle 11g R2 RAC高可用连接特性 – SCAN详解

    原文地址:http://czmmiao.iteye.com/blog/2124373   昨天帮朋友解决11g RAC SCAN问题,当时为这朋友简单解答了一些SCAN特性相关的问题,但我知道这仅仅是 ...

  2. laravel 数据库操作之 DB facade & 查询构造器 & Eloquent ORM

    <?php namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class S ...

  3. mysql操作数据表

    目录 创建数据表 列约束 查看数据表结构 列类型(字段类型) 整型 浮点型 字符串 时间日期类型 Date Time Datetime Timestamp Year 枚举enum 修改表名 增加字段 ...

  4. [Linux] 使用mount来挂载设备到目录

    一般情况下直接mount 设备路径 目录路径,就可以了.umount 设备名,就可以卸载这个设备了使用lsblk -f可以查看挂载的设备,以及这些设备的文件系统. root@tao-PC:/boot# ...

  5. 联邦学习(Federated Learning)

    联邦学习简介        联邦学习(Federated Learning)是一种新兴的人工智能基础技术,在 2016 年由谷歌最先提出,原本用于解决安卓手机终端用户在本地更新模型的问题,其设计目标是 ...

  6. [C1W2] Neural Networks and Deep Learning - Basics of Neural Network programming

    第二周:神经网络的编程基础(Basics of Neural Network programming) 二分类(Binary Classification) 这周我们将学习神经网络的基础知识,其中需要 ...

  7. Python学习笔记2基本语法规则_20170611

    # 1.print 显示示例 print('Hello, World!') 逗号分割变量,输出插入空格 name = 'BB' print('AA', name) # output: >> ...

  8. scrapyd--scrapydweb

    scrapyd-实际的管理爬虫程序 scrapyd 是由scrapy 官方提供的爬虫管理工具,使用它我们可以非常方便地上传.控制爬虫并且查看运行日志. scrapyd是c/s架构 所有的爬虫调度工作全 ...

  9. 8.Go-Reader,Writer和ioutil

    8.1.Reader (1)输入流 流是应用程序和外部资源进行数据交互的纽带 流分为输入流和输出流,输入和输出都是相对于程序,把外部数据传入程序中叫做输入流,反之叫做输出流 在Go语言标准库中io包下 ...

  10. C语言中关于输出n个数后就换行的问题。

    例如:n=10 ........; n++; if(n%10==0&&n!=0)    //因为当n=0时,n%10的值也是0,就也会转行,为了防止这种情况的发生,就用了&&a ...