Maven Dependency Management

Source: Internet
Author: User

1 Concept Introduction

As we said before, MAVEN coordinates can determine a project. In other words, we can use it to resolve dependencies. In POM, dependencies are defined in the Dependencies section. In the above POM example, we define a dependency on JUnit with dependencies:

[HTML]View PlainCopyPrint?
  1. < Dependencies >
  2. < Dependency >
  3. < groupId >junit</groupId>
  4. < Artifactid >junit</artifactid>
  5. < version >3.8.1</version>
  6. < Scope >Test</scope>
  7. </ Dependency >
  8. </ Dependencies >
<dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId> junit</artifactid>       <version>3.8.1</version>       <scope>test</scope>     < /dependency>   

We rely on the class library, we can go to Maven's central warehouse to find, for example, to find Hibernate core package: Mvnrepository.com/search.html?query=hibernate

    


The above example is simple, but in real development we have a much more complex dependency, because the dependent JAR file has its own dependencies. So do we need to define the jar files that are indirectly dependent on the POM? The answer is no, because MAVEN provides the characteristics of transitive dependencies.

The so-called transitive dependency is that Maven examines the dependent jar file, incorporating its dependencies into the final resolved dependency chain. For the JUnit dependencies above, if you look at Maven's local library you will find that Maven not only downloaded the Junit-3.8.1.jar, but also downloaded its POM file. MAVEN will then be able to check the dependencies of JUnit and include the dependencies it needs.

In the Dependencies section of the POM, scope determines the scope of the dependency relationship. We can also specify scope as provided, which means that the JDK or container will provide the required jar files. For example, when doing Web application development, we need the servlet API jar file when compiling, but we don't need to put the jar file in the WAR when we are packing, because the servlet container or the application server will provide it.

The default value of scope is compile, which is included in the classpath at any time and is included in the package.


2 range of Dependencies (scope)

There are several:

Test :

means that the test scope is valid, and that dependency is not used when compiling and packaging.

Compile : (default value)

The compilation scope is valid, compile and run (package) will be stored in the dependencies

provided :

The test and compilation scopes are valid and will not be added when the WAR package is generated, for example: Servlet-api, which is required when compiling, but does not need to be hit in the war when it is packaged, because the servlet container or application server will provide the file. There will be a conflict in the fight.

Runtime :

Compile-time independent, dependent when running (packaged)

    


3 dependent delivery (indirect dependency)

First, dependencies can be passed.

    


When the dependency level is the same, the same thing will be used, the near priority. When the dependency hierarchy is not the same, the hierarchy (shortest) is near the priority.

    


4 excluding Dependencies

When a dependency package conflicts, we can use the exclusion tag to exclude dependencies

    



From for notes (Wiz)

Maven Dependency Management

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.