Maven 依赖范围、依赖传递、排除依赖

依赖范围

回顾下maven构建坐标的构成,如下

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

看上面的xml片段,会发现一个新东西scope
这个标签的意思就是当前插件依赖的范围
在开发中我们把第三方jar放入到classpath中,就能够调用第三方的类和方法。
maven有3中classpath分别是
1. 编译
2. 测试
3. 运行

上面的xml片段中scope的值为test,也就是说这个构建只存在于测试环境中。
所以scope的值就是控制与三种classpath的关系。

scope的值

官方解释

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. This scope is not transitive.
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 supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

中文解释
1. compile 默认的范围,编译测试运行都有效。
2. provided 编译和测试时有效,最后是在运行的时候不会被加入。官方举了一个例子。比如在JavaEE web项目中我们需要使用servlet的API,但是呢Tomcat中已经提供这个jar,我们在编译和测试的时候需要使用这个api,但是部署到tomcat的时候,如果还加入servlet构建就会产生冲突,这个时候就可以使用provided。
3. runtime 在测试和运行时有效。
4. test 在测试时有效。
5. system 与本机系统相关联,可移植性差。编译和测试时有效。
6. import 导入的范围,它只在使用dependencyManagement中,表示从其他pom中导入dependecy的配置。

举个import栗子

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven</groupId>
  <artifactId>B</artifactId>
  <packaging>pom</packaging>
  <name>B</name>
  <version>1.0</version>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>maven</groupId>
        <artifactId>A</artifactId>
        <version>1.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

解释:就是把A中的构建导入到B中。

依赖传递

在正常开发中,我们也会封装maven构建供小伙伴们使用,在我们使用自定义的构建中,那么我们自定义的构建中依赖的构建,也会依赖传递过来。

实例

先建立第一个项目project01
建立第二个项目project02
建立第二个项目project03
这里写图片描述
我们把project01打包安装到本地仓库中。
clean install
在project02中依赖project01
这里写图片描述

把project02 打包安装在本地仓库
在project03中依赖project02

这里写图片描述

然后编译项目project03 会发现project01也会在project03中
这里写图片描述

这就是传递依赖。

排除依赖

上面演示了传递依赖,但是如果我们只需要依赖project02 并不想依赖进来project01怎么办呢?这个时候就要是用exclusions 排除依赖列表

project03排除依赖project01pom.xml配置如下

<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>zzq.mavenProject</groupId>
    <artifactId>project03</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>project03</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>zzq.mavenProject</groupId>
            <artifactId>project02</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!--排除依赖传递列表  -->
            <exclusions>
                <exclusion>
                    <groupId>zzq.mavenProject</groupId>
                    <artifactId>project01</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>

这样在project03中就不会依赖进来project01

这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_25926481/article/details/76924780