Maven study Notes-coordinates and dependencies (in)

Source: Internet
Author: User
Maven study Notes-coordinates and dependencies (in)
1. Transmission dependency
1.1 What is transmission dependency

A jar package is often introduced in the project, and other jar packages related to the package are also introduced. If you search for the jar package by yourself, pay attention to a lot of issues, such as version Issues. Maven will parse and parse the POM directly dependent on each which indirect dependencies are introduced to the project in the form of passing dependencies.

The dependency scope can not only control the relationship between dependencies and the three classpath types, but also affect the transmission of dependencies.

Suppose A depends on B and B on C. We say that A is the first direct dependency on B, B is the second direct dependency on C, and A is the transmission dependency on C. The scope of the first direct dependency and the range of the second direct dependency determine the scope of the passed dependency. For example, the leftmost column indicates the first direct dependency range, and the top row indicates the second direct dependency range, the cross cell in the middle indicates the scope of the transmission dependency.

 

Compile

Test

Provided

Runtime

Compile

Compile

--

--

Runtime

Test

Test

--

--

Test

Rpovided

Provided

--

Provided

Provided

Runtime

Runtime

--

--

Runtime


2. Dependency adjustment:

The passed dependency mechanism introduced by maven greatly simplifies and facilitates dependency declaration. In most cases, we only need to care about the direct dependency of the project, you don't have to worry about the transfer dependency introduced by these direct dependencies. However, sometimes, when there is a problem with the pass-through dependency, we need to clearly know the dependency path from which the pass-through dependency is introduced.

 

For example, Project A has such dependency: A --> B --> C --> X (1.0), A --> D --> X (2.0 ), X is the pass-through dependency of A, but there are two versions of X on the two dependency paths. Which X will be used by maven parsing? Parsing both versions is obviously incorrect, because it will cause duplicate dependencies, so you must select one. The first principle of maven dependency mediation: The path closest is preferred. In this example, the path length of X (1.0) is 3, and the path length of X (2.0) is 2. Therefore, X (2.0) is parsed.

 

The first principle of dependency mediation cannot solve all problems. For example, A --> B --> Y (1.0), A --> C --> Y (2.0 ), the length of the dependency paths of Y (1.0) and Y (2.0) is the same, both of which are 2. Who will be parsed and used? In maven2.0.8 and earlier versions, this is not certain, but at the beginning of maven2.0.9, in order to avoid building uncertainty as much as possible, maven defines the second principle of dependency mediation: The first speaker is preferred. When the length of the dependency path is equal, the order of dependency declaration in POM determines who will be parsed and used. The dependency with the highest order is superior.


3 optional dependencies

An element in the preceding dependency scope: optional.

If there is such A dependency, Project A depends on Project B, Project B depends on Project X and Y, and B's dependency on X and Y is optional: A --> B, B --> X (optional), B --> Y (optional ). According to the definition of the passed dependency, if the scope of all three dependencies is compile, then X and Y are the compile range passed dependency of. However, because X and Y are optional dependencies, dependencies will not be passed. In other words, X and Y will not have any effect on.

Why do we need to use the optional dependency feature? Project B may implement two features, one of which is dependent on feature X, and the other is dependent on Y. The two features are mutually exclusive and users cannot use both features at the same time. For example, B is a persistent layer isolation toolkit that supports a variety of databases, including MySQL and PostgreSQL. When building this toolkit, the drivers of these two databases are required, however, when using this toolkit, it only depends on one database.

One thing to note about the optional dependency is that the optional dependency should not be used ideally. We can see that the reason for the use of optional dependencies is that a project has implemented multiple features and has a single responsibility principle in object-oriented design, it means that a class should have only one responsibility, rather than combining too many functions. This principle also applies when planning maven projects. In the above example, it is better to create a maven project for mysql and postgresql respectively, and assign different artifactId based on the same groupId.


Maven study Notes-coordinates and dependencies (in)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.