15) maven dependency scope

本文详细介绍了Maven中六种依赖范围(compile、provided、runtime、test、system、import)的作用及应用场景。这些依赖范围用于限制依赖传递性,并影响不同构建任务所使用的类路径。

Dependency 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
      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
      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.
    • 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
      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.
    • 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.
    • import (only available in Maven 2.0.9 or later)
      This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that 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.

转载于:https://www.cnblogs.com/zno2/p/4809301.html

### Maven Dependency 参数详解及用法 #### 1. 基本结构 Maven依赖项通常在`pom.xml`文件中声明,基本结构如下: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <scope>compile</scope> <type>jar</type> <optional>true</optional> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> </dependencies> ``` 此配置指定了一个具体的依赖项及其属性[^1]。 #### 2. 关键参数说明 ##### groupId, artifactId 和 version 这三个是最基础也是最重要的字段。它们共同唯一标识了一个特定的库或组件。 - `groupId`: 定义了项目所属组的ID。 - `artifactId`: 表示实际构件的名字。 - `version`: 明确指出使用的具体版本号。 这些信息用于定位远程仓库中的资源位置并下载到本地环境[^3]。 ##### scope (作用范围) 决定了该依赖将在哪个阶段被引入以及如何影响编译过程: - **compile**: 默认值;适用于所有阶段(编译、测试、运行)。 - **provided**: 编译期有效但在部署时不打包入最终产物内。 - **runtime**: 运行时期间才需要用到,在编译期间不需要。 - **test**: 只有执行单元测试时才会加载进来。 - **system**: 类似于 provided ,但是还需要指定 systemPath 属性来指向确切的位置。 ##### type (类型) 默认为 jar 文件形式,默认情况下可以省略。如果要引用其他类型的包,则需显式设定,比如 war 或 pom 等特殊格式。 ##### optional (可选标志) 当设为 true 时表示当前模块并不强制要求这个依赖存在,即下游使用者可以选择忽略它而不必担心构建失败的问题。 ##### exclusions (排除列表) 允许开发者从传递性依赖链条里移除某些不必要的子依赖,从而减少潜在冲突的可能性。通过定义 exclusion 节点实现这一点[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值