dependency中 pom 依赖 scope 用法(网上答案)

本文详细介绍了Maven中dependency的六种scope:compile(默认)、provided、system、runtime、test和import,分别阐述了它们在编译、打包、运行及测试时的作用。例如,provided用于依赖在运行时由服务器提供的库,如servlet-api.jar;runtime用于在运行时才需要的依赖,如JDBC驱动;import则用于在多模块项目中解决依赖管理冲突问题,实现依赖配置的复用。
摘要由CSDN通过智能技术生成

Maven中依赖的域有: compile、provided、runtime、system、test、import。

依赖范围(Scope) 编译classpath 测试classpath 运行时classpath 传递性 说明
compile Y Y Y Y spring-core
test - Y - - junit
provided Y Y - - servlet-api
runtime - Y Y Y jdbc驱动
system Y Y - - 本地jar包

compile(默认)

当依赖的scope为compile的时候,那么当前这个依赖的包,会在编译的时候被加入进来,并且

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
POM(Project Object Model)是 Maven 构建工具的核心,它用于描述 Maven 项目的结构和依赖关系。在 POM 文件,可以通过 `<dependencies>` 元素来管理项目的依赖。 在 Maven ,可以使用依赖管理器来自动管理项目的依赖依赖管理器可以帮助我们从 Maven 仓库自动下载所需的依赖,并将其添加到项目的 classpath 。 下面是一个示例 POM 文件的 `<dependencies>` 元素: ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.3.RELEASE</version> </dependency> </dependencies> ``` 在这个示例,我们声明了两个依赖spring-core 和 spring-webmvc。Maven 将会自动从 Maven 仓库下载这两个依赖,并将其添加到项目的 classpath 。 您还可以使用 `<dependencyManagement>` 元素来管理多个模块之间的依赖关系。在 `<dependencyManagement>` 元素,您可以声明所有模块需要的依赖项,然后在 `<dependencies>` 元素,只需声明需要的依赖项即可。 ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.3.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> </dependencies> ``` 在这个示例,我们在 `<dependencyManagement>` 元素声明了两个依赖项,然后在 `<dependencies>` 元素只声明了需要的依赖项。Maven 将会自动从 `<dependencyManagement>` 元素查找依赖项的版本号,并将其添加到项目的 classpath 。 最后需要注意的是,POM 文件的 `<dependencies>` 元素可以包含多个 `<dependency>` 子元素。每个 `<dependency>` 子元素都描述了一个依赖项。在 `<dependency>` 元素,必须声明依赖项的 groupId、artifactId 和 version。可以使用 `<scope>` 元素来声明依赖项的作用范围(例如:compile、runtime、test 或 provided)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值