Dependence and transitive dependencies maven

This article is for learning record "maven real" book key knowledge points, too flawed or not the description of the proposed purchase to read the original book

First posted some elements common interpretation of a pom

<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>com.example</groupId>
    <artifactId>my-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-demo</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId></artifactId>
            <version>版本</version>
            <type>依赖的类型,对应于项目的packing,默认是jar</type>
            <scope>依赖范围</scope>
            <systemPath>配合 scope=system时使用</systemPath>
            <optional>标记是否为可选依赖</optional>
            <exclusions>
                用来排除传递性依赖
                <exclusion>

                </exclusion>
            </exclusions>

        </dependency>
    </dependencies>

</project>

Previous statement to depend on the type of coordinates, should be easier to understand, here we start from dependence range from the introduction

Dependent range

Maven range dependent in a total of several

  • compile default, are valid for compile, test, run three states
  • test definition, test execution only for the code
  • provided effective when compiled and tested, but invalid operation, typically when the servlet-API , provided by the runtime container
  • runtime for effective testing and runtime, but invalid compile time
  • system provided with the same range, but the specified file system must rely on display, through <systemPath>to specify, with native binding, it is largely unused
  • 3 Total import will not have a real impact, can only be used in dependencyManagement

A table to represent, as follows

scope Compile test run
compile Y Y Y
test Y
provided Y Y
runtime Y Y
system Y Y

Transitive dependencies

Our engineering, in most cases used, not only as a dependency, such as a dependent on b, we a-> b represent, then, a-> b, b-> c, then a is to b a first dependency, b to c is dependent on the second, while for c is a transitive dependencies

Transitive dependent scope passing rules, the first dependency and the second dependency related to the following table the first column denotes a first dependent, dependent on the first line indicates the second

compile test provided runtime
compile compile runtime
test test test
provided provided provided provided
runtime runtie runtime

From the table we can easily get information points

- 第二依赖为complie不改变第一依赖
- 第二依赖test不传递依赖
- 第二依赖provided只传递provided
- 第二依赖runtime对compile第一依赖的传递依赖是runtime

Dependent regulation

Problems often encountered is that there are different versions of the package, they all rely on the presence of transfer, as follows
A-> B-> the C-> X (1.0)
A-> B-> X (2.0)
then the time, according to maven dependent regulation of the first principle of the shortest path rule, x package version 2.0 is used, if and when two different versions of the package depend on how to do the same? This time started the second principle , that is, the order declared pom, who should be declared, who first strategy to select the package.

Optional dependency

Suppose a-> b, b-> x and b-> optional value of y is true, then a dependence of x and y is not transmitted, if a packet using desired x or y, then the need to in a re-dependent

Guess you like

Origin www.cnblogs.com/westlin/p/10988207.html