Maven pom. XML and settings XML explanation
pom. XML and settings xml
pom. XML and setting XML, which can be said to be the two most important configuration files in maven, determines the core functions of maven, although POM. XML has been mentioned in previous articles XML and settings The contents of XML have been briefly introduced, and the learning and research are not detailed. The purpose of this paper is to study these two Maven important configuration files in detail. From these two configuration files, we can draw a lot of Maven topics.
Maven coordinates
First, let's talk about why Maven coordinates are used.
Maven world has a huge number of components, that is, some jars, war and other files used in daily life. Before Maven introduces the concept of coordinates for these components, we can't use any way to uniquely identify all these components. Therefore, if you need to use spring dependencies, go to the spring official website; If you need to use log4j dependency, go to the Apache official website. And because of the different styles of various websites, a lot of time is spent on searching and browsing web pages. Without unified norms and rules, work cannot be automated, and repetitive work should have been handed over to machines.
Maven defines a set of rules: any component in the world can be uniquely identified by Maven coordinates. Maven coordinate elements include groupid, artifactid, version, packaging and classifier. Now Maven can find the corresponding component as long as we provide the correct element coordinates. As for where to download, Maven itself has a built-in address of the central warehouse“ http://repo1.maven.org/maven2 "The central warehouse contains most of the popular open source project components in the world. Mavne will download them there when necessary. Of course, it can also configure its own central warehouse address to download components from its own central warehouse.
For example, the context of spring:
Take a look at the elements of subordinates:
The concept of Maven coordinates is basically like this. Understanding Maven coordinates is a very important step in understanding Maven.
Transitive dependency
What is transitive dependency? Take spring as an example. When using spring, you will rely on other open source class libraries. At this time, you can do two things:
1. Download a big one Zip package, which contains all spring jars, but doing so often introduces many unnecessary dependencies
2. Download only spring related The zip package does not contain dependencies. In actual use, add other dependencies as needed according to the error information
Obviously, these two approaches are very troublesome, and Maven's Transitive dependency mechanism solves this problem well. Open spring-core-4.1 0. POM of release XML, I intercepted a key section:
For example, if project a relies on spring core and spring core relies on Commons codec and Commons logging, commons codec and Commons logging are a transitive dependency of project a. With the transitive dependency mechanism, when using spring core, you don't have to consider what it depends on or worry about introducing redundant dependencies. Maven will resolve the POMS of direct dependencies and introduce those necessary indirect dependencies into the current project in the form of transitive dependencies.
With the transitive dependency mechanism, on the one hand, the dependency declaration is greatly simplified and convenient. On the other hand, in most cases, we only need to care about the direct dependencies of the project without considering what transitive dependencies these direct dependencies will introduce. However, sometimes there are some problems with transitive dependencies, At this time, we need to know which path the transitive dependency is introduced from. This is called dependency mediation. Dependency mediation mainly has two principles:
1. A - > b - > C - > x (1.0), a - > D - > x (2.0). At this time, there are two versions of X on the two dependent paths. At this time, the nearest path takes precedence, so x (2.0) will be parsed and used
2. The dependency lengths of a - > b - > y (1.0), a - > C - > y (2.0), y (1.0) and Y (2.0) are the same, from Maven 2 Starting from 0.9, the first declaration takes precedence, that is, the dependency with the highest order takes precedence
Exclude dependencies
Transitive dependency will implicitly introduce many dependencies into the project, which greatly simplifies the management of project dependencies, but sometimes this feature will bring problems. For example, there is a case:
The current project depends on A. if a depends on the snapshot version of another class library for some reasons, this snapshot will become a transitive dependency of the current project. Second, the instability of snapshot will directly affect the current project. At this time, it is necessary to exclude the snapshot and declare a officially released version of the class library in the current project
It's easy to exclude dependencies. Let's look at the writing:
Here I introduce the dependency of rocketmq, but I don't want to rely on Apache Lang in rocketmq, but I want to introduce the dependency myself, so I exclude Apache Lang.
It should be noted here that only groupid and artifact ID are required when declaring an exclusion, rather than the version element, because only groupid and artifact ID are required to uniquely locate a dependency in the dependency graph. In other words, in the dependencies resolved by maven, it is impossible to have two dependencies with the same groupid and artifactid but different versions.
settings. xml
settings. XML is the basic configuration of Maven. There are many elements. Take a look one by one
1、proxy
Proxy represents Maven's proxy. Let's see how it is written:
Proxy is needed because many times your company requires you to use a proxy that has passed security authentication to access the Internet based on security considerations. In this case, you need to configure the HTTP proxy for Maven so that it can normally access the external warehouse to download the required resources. Multiple proxy elements can be configured under proxies. If multiple proxy elements are declared, the first activated proxy will take effect by default. Active = true indicates that the agent is activated, protocol indicates the agent protocol used, of course, the most important thing is to specify the correct host name (host) and port (port). If the proxy server needs authentication, configure username and password. The nonproxyhost element indicates to specify which host names do not need an agent. You can separate multiple host names with "|" and support the wildcard "*".
2、repository
Repository represents Maven's central warehouse, because although the components in the default remote warehouse are very large, there will always be times when our needs are not met, and other central warehouses will be used at this time. Look at the writing:
Multiple repositories can be declared. The ID must be unique. In particular, note that the ID used by Maven's own central warehouse is central. If other warehouses declare to use this ID, the configuration of the central warehouse will be overwritten. Releases and snapshots are more important. The former means to enable the release version download support of the warehouse, and the latter means to turn off the snapshot version download support of the warehouse. In this way, Maven will go to the warehouse to download the components of the release version instead of the components of the snapshot version.
3、server
Most remote warehouses can be accessed without authentication, but sometimes for security reasons, authentication information needs to be provided to access some remote warehouses. For security reasons, authentication information is generally only placed in settings In XML, server is the authentication element. Take a look at the configuration:
The key here is the ID, which must be completely consistent with the ID of the repository element to be authenticated. In other words, the formal ID connects the authentication information with the warehouse configuration.
4、mirror
If warehouse x can provide all the contents stored in warehouse y, warehouse x can be regarded as a mirror of warehouse y. in other words, any component that can be obtained from y can be obtained from X. for example“ http://maven.net.cn/content/groups/public/ "It's a central warehouse" http://repo1.maven.org/maven2/ "For images in China, due to geographical factors, the image can often provide faster services than the central warehouse, which is why we use mirror.
Take a look at the configuration of the mirror:
In this example, if the mirror is *, it means that it is configured as the image of all central warehouses, and any request for central warehouses will be transferred to the image. The other three elements ID, name and URL are the same as the general warehouse configuration, indicating the unique identifier, name and address of the image warehouse. Similarly, if the image needs authentication, warehouse authentication can also be configured based on the ID.
Thank you for reading, hope to help you, thank you for your support to this site!