maven 学习---Maven依赖管理
其中一个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使用传递性依赖机制来管理这些细节。
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:http://www.yiibai.com/maven/maven_manage_dependencies.html
maven 学习---Maven依赖管理的更多相关文章
- 4.Maven概念模型,maven的生命周期,Maven坐标,依赖管理(依赖范围,依赖声明),仓库管理,私服概念
1 maven概念模型 2 maven的生命周期,项目构建过程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg== ...
- maven 学习---Maven外部依赖
现在,你也知道Maven做依赖管理使用Maven仓库的概念.但是,如果依赖是不提供任何远程存储库和中央存储库发生了什么? Maven提供为使用外部依赖的概念,应用在这样的场景. 举一个例子,让我们做以 ...
- Maven教程3(依赖管理)
Maven教程2(Eclipse配置及maven项目) Maven项目,依赖,构建配置,以及构件:所有这些都是要建模和表述的对象.这些对 象通过一个名为项目对象模型(Project Object Mo ...
- maven 学习---Maven构建自动化-Hudson
建立自动化定义场景,依赖项目建设过程中被启动,一旦项目生成成功完成,以确保相关的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序,网页UI和应用程序的桌面UI ...
- maven 学习---Maven教程
Apache Maven是一个软件项目管理和综合工具.基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件. 本教程将介绍如何使用Maven在Java开发,或任何其 ...
- maven 学习---Maven添加远程仓库
默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资源库 添加Java.ne ...
- maven 学习---Maven中央存储库
当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载. 首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源, 如果没有找到,然后把它会 ...
- maven 学习---Maven安装配置
想要安装 Apache Maven 在Windows 系统上, 只需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : JDK 1.8 ...
- Maven解读:项目依赖管理如何优化
Github地址:https://github.com/zwjlpeng/Maven_Detail Maven最大的好处莫过于其强大的依赖管理系统,在Pom配置文件中指定项目需要的Jar包的坐标,Ma ...
随机推荐
- WindowServer优化
Windows Server 2016 禁止自动更新 1. 打开cmd,输入sconfig,出现如下图: 2. 输入5回车,在输入m回车,完成关闭自动更新.
- 一文解读DevOps工具链 (转)
在列出DevOps 工具链之前,介绍一下什么是DevOps,虽然DevOps这个概念现在还没有标准的定义,但我们可以追溯一下其过去九年的历史发展过程(从2009年-2017年),列出几个相对明确又有所 ...
- [转]Eclipse插件开发之基础篇(3) 插件的测试与调试
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/17/eclipse_plugin_1_1_2.html 1. 使用JUnit对插件进行测试 E ...
- Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
本博主在一次个人移动端项目中,遇到这么一个需求:希望自己的项目中,头部导航条的效果可以像今日头条那样,横向滚动! 对于这样的效果,在各大移动端项目中几乎是随处可见,为什么呢? 我们都知道,对于移动端也 ...
- rocksdb和leveldb性能比较——写性能
前面学习了一下rocksdb,这个db是对leveldb的一个改进,是基于leveldb1.5的版本上的改进,而且leveldb1.5以后也在不断的优化,下面从写入性能对两者进行对比. 前言 比较的l ...
- nginx在centos下的安装
第一步:打开浏览器下载,再上传到centOS系统中 http://nginx.org/download/ 或者在 centOS系统输入: wget http://nginx.org/download/ ...
- centos 下创建本地镜像源,结合 nginx
1. 创建同步文件 参考清华的Centos源,配置同步文件.https://mirrors.tuna.tsinghua.edu.cn/help/centos/ [root@localhost cent ...
- 将网站发布到阿里云的Linux服务器上(简述)
这里以idea的springboot项目为例(打成jar包) 这里去阿里云购买服务器的过程就不说了,自行百度 还有这篇只是简述,因为把一个项目部署到服务器要做的事情还是不少的,然后没学过Linux的建 ...
- Html学习之十四(阴影文字的设计)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Pwn-level3
题目地址 https://dn.jarvisoj.com/challengefiles/level3.rar.2047525b05c499c9dd189ba212bba1f8 借鉴 https://w ...