1: What is Maven
Maven is a software project management tool based on the project object model (POM project object model), which can manage project construction, reporting and documentation through a small piece of descriptive information (configuration).
The above words are official words, and it means nothing to say, but my own understanding of Maven is that Maven is used to sort out the relationship between projects reasonably and properly. As we all know, whether we develop a project or write a small demo, we always You need to rely on jars, but these jars are actually a project, just type the project into jars. Since we need so many jars, it will be very cumbersome to add jars manually, so Maven is used. Maven is The jar package is obtained through the configuration of the pom.xml file, and there is no need to add it manually.
Speaking of this, then the question comes again, what is the pom.xml file, where does this file come from, and how to configure this file to obtain the jar package, please don't worry, let me tell you later!
All of the above are based on the premise of maven, so we must first download, install, and configure the maven environment!
2maven configuration and environment collocation
Anyone who is familiar with java knows that when installing jdk, you need to build JAVA_HOME in the environment variable. For the same reason, maven is also needed. So without further ado, let’s introduce the maven configuration and environment in detail.
First of all, you must download the Maven installation package. Go to https://maven.apache.org/download.cgi to download the latest version of the Maven program:
After downloading the latest version of the installation package, unzip it in the file, the result is as shown below
Copy the above path, create a new MAVEN_HOME in the environment variable, and fill in the path
Edit the path environment variable and append %MAVEN_HOME%\bin\; at the end
cmd enter the command line and enter mvn -v to check whether the configuration is successful
3 Get the jar through the pom.xml file
There will be this paragraph in our pom.xml, which means:
groupId: give yourself a unique identifier;
artifactId: project name
version: version number
Packaging: what package is packaged into after packaging
This is your own identification, so that you can be labeled as a jar package for others to use.
Let's talk about getting other jar packages in the pom.xml file
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>//It is usually the package name, which means the only one
<artifactId>mybatis-spring-boot-starter</artifactId>项目名
<version>1.2.0</version> version number
</dependency>
...
...
...
...
<dependencies>
So we pass
The three values of groupId, artifactId, and version can determine a jar package. (For details, please listen to the next decomposition)
4. Warehouse
At the third o'clock, I mentioned that the jar package can be obtained by using the pom.xml file, so where is the jar obtained? Generally, there are three places: local, private server, and central warehouse
local
We will collect all the jars we need and build a folder to store in the local computer, then we can get it through maven's pom.xml file
Private server, third-party server
Generally owned by the company LAN, connecting to private servers needs to be configured separately
central warehouse
Maven has a built-in remote public repository: http://repo1.maven.org/maven2
So how to get the local jar to make the pom.xml file go to the specified file to get the jar package (omission of private server)
In this folder there is this settings.xml file, in this file there is
<localRepository>/path/to/local/repo</localRepository>
This line of code is commented out by default, take it out, and change the value to the path where the jar package you specified is located
(It is recommended to keep the original, copy the original to the upper or lower folder)
So what is the order of fetching the jar package?
5 Create a project in eclipse
Open Window->Preferences->Maven->Installations, click Add on the right
home select the path of maven, name fill in the name and click finish, and finally select the maven you add in, apply
Click User Settings where User Settings is the path of the settings.xml file that is recommended to be copied to the upper or lower layer as mentioned above, and the Local Repository below is the path of the local warehouse
Create maven custom project using eclipse
java project
1. Select maven project, if right-click to create a new one, get it through other
2. Create a simple project (skip skeleton selection)
3. Set the project parameters and create a java project
4. Create java project results
Create maven web project
1. Same as above
2. Same as above
3. Set the project parameters, the others are the same, but the packaging method is different.
4. Create web project results
5. Possible error 1: pom.xml error
The result is as follows
Then you need to update the project, and no error will be reported.
6. Error 2, error when writing jsp
The error message is that the serlvet-jar and jsp-jar packages are missing
Solution: Use pom.xml to locate and import the corresponding jar package
Find the jsp-jar package. The role of scope is to indicate the scope of the jar package. Provided indicates that it is valid during compilation and testing, but not during runtime. Specifically, it will be explained later when pom.xml is explained.
Find the servle-jar package
Check and modify pom.xml
After the modification, it was found that both jar packages were loaded, the project was intact, and no error was reported.
eclipse maven operation
6--9 are all shortcuts
9 Test, equivalent to command line mvn test
8 Installation, equivalent to the command line mvn install Function: upload it to the local warehouse, see the above explanation for details
7 Associate the source code, this does not need to be explained, usually we use other jar packages to also associate the source code
6 clean, mvn clean
5 maven bulid executes the maven command, equivalent to mvn
4 maven build 5 quick action
Same as 5 if no operation
If the operation has been done once, the previous command of 5 will be executed directly
If the operation is multiple times, a select box will be provided
6.5, example, publish the maven web project to tomcat to run
Command: tomcat:run
It can be accessed through the URL, and the project will be uploaded to the local warehouse.
Explanation of dependencies of pom.xml (emphasis)
I have been using pom.xml to find the three most critical attributes of jar packages, groupId, artifactId, version, and I should have some impressions. I also know why the corresponding jar packages can be found through these three, but I did not elaborate on some of them. For small knowledge points, such as adding the dependencies of servlet-jar and jsp-jar above, some properties that appear are not well understood. Therefore, this chapter will comprehensively analyze the dependencies.
How to get the coordinates (that is, the three key attribute values)
Method 1: Use website search [take from central warehouse]
Step 1: Baidu search for the keyword "maven repository"
Step 2: Enter the keyword query to obtain the required content and determine the required version
Step 3. Get the coordinates
Method 2. Use the local warehouse to get the coordinates through eclipse
The above has introduced how to obtain the corresponding jar from the local warehouse, here is a brief description
Step 1: Add dependencies, right-click in the pom.xml file
Step 2: Get the coordinates
Common configuration of dependencies (coordinates)
In order to avoid not knowing which configuration properties are mentioned, look at the following figure to understand, that is, the property configuration under dependency, there are all 9, and 7 of them are mentioned.
groupId, artifactId, and version are the basic coordinates of dependencies, and none of them are necessary. These three can be ignored. We all know that the important thing is that we need to understand the configuration properties other than these three.
type: the type of dependency, such as jar package or war package, etc.
The default is jar, which means the dependent jar package
Note: <type>pom.lastUpdated</type> We encountered this when we added servlet-jar above. When we saw lastUpdated, it means to use update description information, placeholder function, in layman's terms, select this type, the jar package will not be loaded, but only some description information of the jar package will be loaded, so that other jar packages can see some relevant prompt information when referencing it, that's all, so he is an accountant bit character, just keep in mind that his jar package will not be loaded.
optional: Mark whether the dependency is optional. Default value false
For example, struts2 has a built-in log4j function, which is to embed log4j into the jar package of struts2, and it does not matter whether struts2 has log4j or not. The information prompted is relatively less, so at this time, you can perform optional operations on it, if you want it, if you don't want it, set it to false.
Exclusions: exclude transitive dependencies and resolve jar conflicts
Dependency transitivity means that project A depends on project B, and project B depends on project C. When project A is used, B will also be loaded, which is a transitive dependency, and so on, and C will be loaded accordingly.
This dependency transfer has advantages and disadvantages. The disadvantage is the conflict of jar packages. For example, A depends on B (the version of B is 1), and C depends on B (the version of B is 2). If a project needs both A and C , then both A and C will transfer dependencies to load B. The problem is here. The versions of the two Bs are different. Loading both of them will cause conflicts. At this time, you need to use the exclusions attribute configuration. Maven will also have a mechanism to avoid loading both. The default configuration of maven is used first, but we still need to use exclusions to configure more reasonably. Here we use spring beans and struts2 spring plugin to illustrate this problem and use exclusions solve this problem. (spring bean and struts2 spring plugin both need to depend on spring-core, but the versions are different)
Find these two jar packages from the local repository
maven's own solution is as follows
The default maven configuration is used first, and the following is the proof
Spring-beans is loaded first, so the version of spring-core that spring-beans depends on is loaded.
Load struts2-spring-plugin first, then load the version of spring-core it depends on
Use exclusions to configure
Even if struts2-spring-plugin is configured earlier, version 3.2.0 needs to be used. You need to exclude dependencies for struts2-spring-plugin (do not use 3.0.5 dependencies)
Note: In this way, the version of spring-core that struts2-spring-plugin depends on is excluded from the dependency, that is, the dependent spring-core will not be loaded, check the code to see if it meets the requirements, if not, you need to manual modification
Scope: Dependency scope, which means that the jar package loaded through pom.xml will take effect in what scope, including compile time, runtime, and test time
compile: the default value, if this value is selected, it means that the current jar is used for compilation, testing and running
test: Indicates that the current jar only takes effect during testing, and the jar package cannot be used in other scopes. For example: junit. It is not written here and no error is reported, because the default is compile, and the compile package expands the test
runtime, which means that the current jar is used during testing and runtime, and the jar package is not used during compilation. For example: JDBC driver. JDBC driver, at compile time (that is, when we write code, we use interface programming, and we don't use anything in the JDBC driver package at all, it is only used at runtime, so this is a typical use of the runtime value. example), do not write here and do not report an error, the reason is the same as above
provided, which means that the current jar is used when compiling and testing, and the jar is no longer used when running. For example: servlet-api, jsp-api, etc. 【Required to fill in】
What does that mean? When we created web projects and wrote servlets or jsp before, we never imported jar packages, because myeclipse or other IDEs provided us with these two jar packages, which were built-in, so we used both servlets during compilation and testing. The error of missing jar package will not be reported, and at runtime, leaving myeclipse or other IDEs is equivalent to missing these two jar packages, but at this time tomcat will provide us with these two jars, so that we do not It will report an error, so these two are very special. look at the picture
1. In the development stage (provided by MyEclipse), see the picture below to prove what we said
java web 5.0 project:
java web 6.0 project:
2. Running phase (provided by tomcat)
Therefore, according to this feature, if you use maven to develop a project, it is not a web project, then myeclipse will not provide us with these two jar packages, we must manually obtain it from the warehouse through coordinates, but for the above analysis , when running, tomcat will help us provide these two jar packages, so the jar packages we obtained from the warehouse cannot conflict with tomcat, then we can just set these two jars through the provided attribute The scope of action is to take effect during the mutation period and the testing period.
This example can explain the error and solution when creating maven web above.
system: Indicates that the jar package that we manually added does not belong to the maven warehouse (local, third-party, etc.), such a jar package that belongs to the unique class library, only takes effect during the compilation and testing period, and is invalid during runtime. generally not needed
7.3. Dependency regulation principle
This is how maven solves the problem of jar package conflicts when transitive dependencies are passed. According to two principles, one has been introduced above, which is the second principle below.
1. The first principle: the principle of priority for those who are closest to the path
A-->B-->C-->D-->X(1.6)
E-->D-->X(2.0)
Use X(2.0) because its path is closer
2. The second principle: the first declarant takes precedence. That is, if the paths are the same, the default maven configuration is used first.
A-->B --> X(1.6)
C-->D--> X(2.0)
This way the paths are the same, then if A is in the front and C is in the back, use X(1.6)
Maven will first choose according to the first principle. If the first principle fails, it will be processed according to the second principle.
<Reference>https://www.cnblogs.com/whgk/p/7112560.html<Reference>