想到写这个问题的来源是,自己在一次项目中,导入了一个jar包,并import了其中的一个类,可是在运行的时候IDEA一直报找不到该类,我就十分的纳闷,这个类明明可以引入了,为什么会找不到呢?经过了长时间的查找,才发现原来是在maven中引入该jar包的时候,scope是provided,导致该包在运行的时候不会依赖进来,所以才会导致一直找不到该类。为了避免以后犯这种同样的错误,所以决定来总结一下maven中scope不同值之间的作用。
项目源码:
官方解释:
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:
compile(编译范围)
compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
编译这是默认范围,如果未指定范围,则使用此范围。编译依赖项在项目的所有类路径中均可用。此外,这些依赖项会传播到相关项目。
provided(已提供依赖范围)
provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
provided和compile比较相似,但是provided表明您希望JDK或容器在运行时提供依赖项。例如,在为Java Enterprise Edition构建Web应用程序时,您将对Servlet API和相关Java EE API的依赖关系设置为提供的范围,因为Web容器提供了这些类。该作用域仅在编译和测试类路径上可用,并且不可传递。
例子:
runtime(运行时范围)
runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
运行时范围,表明依赖关系不是编译所必需的,而是执行所必需的。它在运行时和测试类路径中,但不在编译类路径中。
例子:
test(测试范围)
test This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
test范围表明依赖关系对于正常使用应用程序不是必需的,并且仅在测试编译和测试执行阶段可用。此范围不是可传递的。
system(系统范围)
system This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
system范围与provided范围类似,除了必须提供显式包含它的JAR路径。该组件始终可用,并且不会在仓库中查找。
import(引入范围)
import This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
import仅在<dependencyManagement>部分中的pom类型的依赖项上支持此作用域。它在指定的POM的<dependencyManagement>部分中指示要用有效的依赖关系列表替换的依赖关系。由于已替换它们,因此具有导入范围的依赖项实际上并不参与限制依赖项的可传递性。