pom依赖中的scope属性

scope一共包括:compile、runtime、test、system、provided、import

<dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version>4.4.1</version>
     <scope>compile</scope>
</dependency>
compile是默认值,当我们引入依赖时,如果标签没有指定,那么默认就是complie。
compile表示被依赖项目需要参与当前项目的编译,包括后续的测试,运行周期也参与其中,同时打包的时候也会包含进去。是最常用的,所以也是默认的


<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.46</version>
   <scope>runtime</scope>
 </dependency>
runtime表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过编译而已。
数据库的驱动包一般都是runtime,因为具体的实现是通过反射生成(如mysql驱动),实在运行时生效的,所以这类jar包无需参与项目的编译

<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
</dependency>
test表示只会在测试阶段使用,在src/main/java里面的代码是无法使用这些api的,并且项目打包时,也不会将"test"标记的打入"jar"包或者"war"包。
备注:mvn打包springboot时也可跳过springboot-test
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>


<dependency>
    <groupId>com.mytest</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/test-1.0.jar</systemPath>
</dependency>
system依赖不是由maven仓库,而是本地的jar包,因此必须配合systemPath标签来指定本地的jar包所在全路径。
这类jar包默认会参与编译、测试、运行,但是不会被参与打包阶段。
备注:mvn打包
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <!--设置为true,以便把本地的system的jar也包括进来-->
        <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>4.0.1</version>
   <scope>provided</scope>
</dependency>
provided表示的是在编译和测试的时候有效,在执行(mvn package)进行打包成war、jar包的时候不会加入,
比如:servlet-api,因为servlet-api,tomcat等web服务器中已经存在,如果在打包进去,那么包之间就会冲突

<dependencyManagement>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.1.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
 </dependencies>
</dependencyManagement>
import比较特殊,他的作用是将其他模块定义好的 dependencyManagement 
导入当前 Maven 项目 pom 的 dependencyManagement 中,都是配合<type>pom</type>来进行的。所以import作用的是pom类型,不是jar包。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值