编程知识 cdmana.com

Maven dependence

Maven What is it?

In the service of java Automatic building tools for the platform , Dependency management tool , And project information management tools .

structure

With “jave Source file ”,“ Framework configuration file ”、“JSP”、“HTML”、“ And other resources are raw materials ”, Go to “ production ”( compile 、 Deploy 、 build ) The process of a project that can be run

Dependency management

In this era of open source , Almost anything java Applications will borrow some third-party open source class libraries , These class libraries can be introduced into the project by dependency .

As dependence increases , Version inconsistency 、 Version conflict 、 Problems such as dependence and bloated are also coming .


Depend on the range

species :compile,test,provided
Concept :maven Different stages of the project are introduced to classpath The dependence in is different ; For program structure ,compile Scope dependency is valid for both the main program and the test program ,test Scope dependency only works with the test program . For the development phase ,compile Scope depends on development , Deploy , The operation phase is valid ,prorided Scope dependency is only valid in the development phase .

Use
Maven Use in scope To specify the dependency scope and transitivity of the current package . Common optional values are :compile, provided, runtime, test, system etc. .scope Mainly for pom.xml The dependency definition part of the file , for example :

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>RELEASE</version>
			<scope>provided</scope>
        </dependency>

compile:

The default value in the dependency definition is compile, This dependency range is for Compiled by 、 test 、 Run three classpath Are effective , A simple example , If there is spring-core Dependence , that spring-core Whether it's compiling , test , Or the operation will be used .

test:

As the name suggests, it's about testing , Use the dependency of this dependency scope , Only for testing classpath It works , When compiling the main code and the project package deployment , Will not be able to use the dependency , The most typical example is Junit, Components need to be tested , So it depends on testing .

provided:

Use the... Of the dependency scope maven rely on , Only for compiled and tested classpath It works , Not involved in packaging and deployment , A typical example is servlet-api, When compiling and testing the project, you need to rely on , But at runtime ,web Containers ( for example tomcat) The dependency that has been provided , So the runtime no longer needs this dependency .


jar Packet collision

1、provided Scope dependence leads to jar Packet collision
As mentioned above provided Dependence on scope , because web The container has been supplied with , So in the deployment run phase , The dependency is automatically ignored , I don't think so jar Bag to local warehouse , If the specified dependency range is not displayed , It becomes the default dependency scope compile, Then the local warehouse will have the jar package , Then the deployment may cause version conflicts when running , At this time, a null pointer exception may be reported .
 Insert picture description here
Following the error , Let's open number one 22 That's ok , It turns out that we have no way to start , Moreover, the null pointer exception is not our common type . If this happens , That is the jar Conflict .
 Insert picture description here
2、 Between module projects jar Packet collision
2.1 The shorter the path is, the better
 Insert picture description here
MakeFriend Engineering depends on HelloFriend engineering ,HelloFriend Engineering depends on Hello engineering ,HelloFriend Engineering depends on log4j yes 2.14,Hello The project depends on log4j yes 2.17, because MakeFriend give an example HelloFriend Engineering recently , therefore MakeFriend Rely on the log4j yes 2.14.
2.2 When the paths are the same, the one declared first takes precedence
 Insert picture description here
Makefriend Engineering also depends on HelloFriend Engineering and OurFriends engineering , But in dependency The first configuration in the tag is HelloFriend, therefore MakeFriend The project depends on log4j yes 2.14.


The transmission of dependence

Join in MakeFriend Project dependence HelloFriend project ,HelloFriend Project needs spring-core rely on ,spring-core Need to rely on common-logging. Then in execution MakeFriend Project time , Will automatically spring-core and commons-logging Download to MakeFriend Of jar Package file .
 Insert picture description here

The exclusion of dependence

Then the example in dependency passing , If commons-logging jar The bag is unstable ,MakeFriend Projects don't want to be added to the project , Then we need to exclude the dependency . The setting of dependency exclusion only needs to use exclusions Labels can be .
 Insert picture description here

版权声明
本文为[Wooden pine cat]所创,转载请带上原文链接,感谢

Scroll to Top