Maven项目的pom文件中<dependency><scope>的用法

本文详细介绍了Maven中dependency的scope属性,包括compile、provided、runtime、system、test和import的含义及应用场景。重点解析了import scope在多模块项目中的作用,如何通过它来解决依赖管理中的冲突问题,以及在实际开发中,如Spring Boot项目中如何使用import scope导入外部依赖管理。

<scope>的取值范围有:compile、provided、runtime、system、test、import

  1. compile(默认)
    使用compile修饰的的依赖,会在编译 和 打包 的时候被加入进来
  2. provided
    编译和测试时有效。例如在开发一个web应用时,编译时需要servlet-api.jar,但是运行时不需要该jar包,因为就这个jar包已由web服务器提供,如果在打包时又被加入进去,就可能产生冲突,此时就用provided进行范围修饰。
  3. system
    与provided相同,不过被依赖项不会从maven仓库获取,而是从本地文件系统拿,需要配合systemPath使用。
  4. runtime
    运行时才会依赖,编译时不会依赖。比如,在编译时不需要JDBC API的jar包,而在运行时才需要JDBC驱动包,就可以用runtime修饰。
  5. test
    mvn test时有效,编译和打包时都不会使用这个依赖。
  6. import
    maven多模块项目结构中,可以使用parent定义父项目,实现从父项目中继承依赖。但maven只能单继承,即一个项目只能使用一个parent标签只能使用一个parent标签定义父项目。
    maven2.9之后的版本引入了新的功能,可以实现依赖上的多重继承。就这个功能可以将依赖配置复杂的pom文件拆分成多个独立的pom文件。这样处理可以使得maven的pom配置更加简洁。现时可以复用这些依赖。比如,在开发spring boot项目的时候,pom中会有如下配置:
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.3.RELEASE</version>
</parent>

但是如果该项目是个maven子模块的话,就会出问题。由于maven类似java是单继承,不能有两个parent,现在<parent>标签已经用来引用父模块,现在又用来引用springboot,就会产生冲突。那怎么解决呢?
解决办法就是:使用dependencyManagemeng引入dependency,并且scope属性改为import。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

scope的import属性只能在<depencencyManagement>中使用,表示从其它的pom文件中导入dependency配置。

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_annocation</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.1.10</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> <!-- 选择 1.8.0 之前的版本 --> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.8</version> </dependency> </dependencies> </project>修改一下
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值