Scope属性
XML
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
先在pom文件中将依赖换成读取本地:<systemPath>
然后通过<scope>标签控制该jar包(依赖)的生存周期:
1. **compile(默认作用域)**
- 可用阶段:编译、测试、运行
- 传递性:会传递给依赖项目
- 打包行为:包含在项目构建的输出中(如 JAR/WAR 文件)
- 典型场景:项目核心依赖(如 Spring Framework、日志库)
XML
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>compile</scope> <!-- 可省略 -->
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
2. provided
- 可用阶段:编译、测试
- 传递性:不会传递
- 打包行为 :不包含在构建输出中
- 典型场景:容器提供的依赖(如 Servlet API、JavaEE API)
XML
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
3. runtime
- 可用阶段:测试、运行
- 传递性:会传递
- 打包行为:包含在构建输出中
- 典型场景:运行时需要的驱动或实现(如 JDBC 驱动)
XML
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
4. test
- 可用阶段:仅测试阶段
- 传递性:不会传递
- 打包行为:不包含在构建输出中
- 典型场景:测试框架(JUnit、Mockito)
XML
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>test</scope>
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
5. **system(谨慎使用)**
- 可用阶段:编译、测试(需手动配置运行)
- 传递性:不会传递
- 打包行为:默认不包含,需特殊配置
- 典型场景:本地特殊 JAR(无法从仓库获取)
XML
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
6. **import(特殊作用域)**
- 可用阶段:仅依赖管理
- 传递性:不会传递
- 打包行为:不包含
- 典型场景:继承其他 POM 的依赖管理
XML
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.hik</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
作用域对生命周期阶段的影响
作用域 | 编译 | 测试 | 运行 | 打包 | 传递依赖 |
---|---|---|---|---|---|
compile | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
provided | ✔️ | ✔️ | ❌ | ❌ | ❌ |
runtime | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
test | ❌ | ✔️ | ❌ | ❌ | ❌ |
system | ✔️ | ✔️ | ⚠️* | ️* | ❌ |
⚠️ system 作用域:运行和打包需要额外配置(如添加 <includeSystemScope>true</includeSystemScope>
)
最佳实践建议
- 避免使用 system 作用域 :优先将本地 JAR 安装到仓库 (
mvn install:install-file
) - 作用域最小化原则 :使用能满足需求的最严格作用域(如能用
runtime
不用compile
) - provided 作用域用于容器依赖:防止与容器提供的库冲突
- test 作用域隔离测试依赖:减少最终包大小
- 使用
mvn dependency:tree
验证:检查依赖传递和冲突
打包扫描(jar包)
使用system引用时,<scope>,<systemPath>标签,这种方式在部署时才会出现的问题。部署到容器运行时,就会提示找不到类,因为该jar未被注入到项目lib中,需要在pom文件中增加打包扫描的配置。
需要配置 <build>
XML
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>