最近在学习《maven实战》这本书,在看到依赖传递这部分的时候,我发现书本提供的依赖传递表有点不合理,就去官网看文档,最后发现官网提供的表和《maven实战》提供的表不一致,我觉得一切要以官网的为准,同时参考了这篇文章的一部分内容,http://seanzhou.iteye.com/blog/1688740。
摘抄日期为2015年5月13日,如果后续有变化,再说。
=======================================================================================
Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.
There are 6 scopes available:
Each of the scopes (except for import) affects transitive dependencies in different ways, as is demonstrated in the table below. If a dependency is set to the scope in the left column, transitive dependencies of that dependency with the scope across the top row will result in a dependency in the main project with the scope listed at the intersection. If no scope is listed, it means the dependency will be omitted.
compile | provided | runtime | test | |
compile | compile(*) | - | runtime | - |
provided | provided | - | provided | - |
runtime | runtime | - | runtime | - |
test | test | - | test | - |
(*) Note: it is intended that this should be runtime scope instead, so that all compile dependencies must be explicitly listed - however, there is the case where the library you depend on extends a class from another library, forcing you to have available at compile time. For this reason, compile time dependencies remain as compile scope even when they are transitive.
===========================================================================================================
俺英语不好,但是 这些英文不会太难,我仅把自己的理解写下来,如果哪里说错了,你来打我呀!
依赖范围用于限制依赖关系的传递性并影响到用于各种构建任务classpath,传递性表现在具体某个时间段具有依赖性,一共有三种时间段:在编译源代码时 、在编译测试代码及运行测试用例时、在运行时。
1、compile是默认的依赖范围,使用此依赖范围的Maven依赖,对于编译、测试、运行三种 classpath 都有效;
2、test只对于测试classpath 有效,测试classpath包括测试编译和测试运行;
3、provided对于编译和测试classpath有效,但在运行时无效;
4、runtime对于测试和运行classpath有效,但在编译主代码时无效;
5、system和 Provided 依赖范围完全一致,但是,使用 System 范围的依赖时必须通过 systemPath 元素显式地指定依赖文件的路径;
6、impor只用于<dependencyManagement>标签,它会引入其他POM的dependencyManagement的内容,并不参与到依赖传递。
假设a对b的依赖为第一依赖,b对c的依赖为第二依赖
1、表格下的英文可以理解为,a对b的依赖是指a依赖b的字节码文件,只要b是编译好的且b没有继承c中的某个类,则a在编译时不依赖c,如果b继承了c中的某个类,则a在编译时依赖c;
2、第二依赖为test的时候,第一依赖无论是什么都与b的测试代码没有关系,因此依赖不传递;
3、第二依赖为provided的时候,依赖不传递;
4、第二依赖为compile的时候,传递性依赖和第一依赖一样;
5、第二依赖为runtime的时候,除了第一依赖为compile时传递性依赖是runtime,传递性依赖和第一依赖一样;