Table of contents
Maven
Course content
- First introduction to Maven
- Maven overview
- Maven model introduction
- Maven warehouse introduction
- Maven installation and configuration
- IDEA integrates Maven
- Dependency management
01. Maven course introduction
1.1 Course Arrangement
After learning the front-end web development technology, we are about to start learning the back-end web development technology. As a Java development engineer, back-end web development technology is the focus of our learning .
To learn back-end web development technology, we will first learn the construction tool for Java projects: Maven
1.2 First introduction to Maven
1.2.1 What is Maven
Maven is an open source project under Apache and a tool for managing and building Java projects.
Official website: https://maven.apache.org/
The Apache Software Foundation, established in July 1999, is currently the largest and most popular open source software foundation in the world. It is also a non-profit organization specifically designed to support open source projects.
Open source projects: https://www.apache.org/index.html#projects-list
1.2.2 The role of Maven
What can you do with Maven?
- Dependency management
- Unify project structure
- Project build
Dependency management :
- Conveniently and quickly manage the resources (jar packages) that the project depends on to avoid version conflicts.
When using maven for project dependency (jar package) management, it is very convenient to solve this problem. We only need to add a configuration as shown in the figure below in the pom.xml file of the maven project to achieve this.
**Unified project structure: **
- Provide a standard, unified project structure
In project development, when you use different development tools (such as Eclipse, Idea) to create project projects:
If we create a maven project, it can help us automatically generate a unified and standard project directory structure:
The specific unified structure is as follows:
Catalog description:
- src/main/java: java source code directory
- src/main/resources: Configuration file information
- src/test/java: test code
- src/test/resources: test configuration file information
Project build:
- Maven provides a standard, cross-platform (Linux, Windows, MacOS) automated project construction method
As shown in the figure above, we have developed a system. The code needs to be compiled, tested, packaged, and released. If these operations need to be repeated, it will be particularly troublesome. Maven provides a set of simple commands to complete the project construction.
To sum up, we can draw a conclusion: Maven is a tool for managing and building java projects
02. Maven Overview
2.1 Introduction to Maven
Apache Maven is a project management and construction tool. It is based on the concept of Project Object Model (POM) and manages the construction, reporting and documentation of the project through a small piece of description information.
Official website: https://maven.apache.org/
The role of Maven:
- Convenient dependency management
- Unified project structure
- Standard project construction process
2.2 Maven model
- Project Object Model
- Dependency management model (Dependency)
- Build lifecycle & phases
1). Build lifecycle & phases (Build lifecycle & phases)
The part framed in purple in the above picture is used to complete the standardized construction process. When we need to compile, Maven provides a compilation plug-in for us to use; when we need to package, Maven provides a packaging plug-in for us to use, etc.
2). Project Object Model
The part framed in purple in the above figure belongs to the project object model, which abstracts our own project into an object model with its own unique coordinates. As shown in the figure below, it is a Maven project:
Coordinates are the unique identifier of the resource (jar package). Through the coordinates, the location of the required resource (jar package) can be located.
3). Dependency management model (Dependency)
The part framed in purple in the above figure belongs to the dependency management model, which uses coordinates to describe which third-party jar packages the current project depends on.
When we needed a jar package in our project before, we directly copied the jar package to the lib directory under the project. But now how can we find the required jar package file based on the coordinates written in the pom.xml file?
Answer: Maven repository
2.3 Maven repository
Warehouse: used to store resources and manage various jar packages
The essence of a warehouse is a directory (folder), which is used to store all dependencies (jar packages) and plug-ins during development.
The Maven warehouse is divided into:
- Local warehouse: a directory on your computer (used to store jar packages)
- Central repository: the only one in the world maintained by the Maven team. Warehouse address: https://repo1.maven.org/maven2/
- Remote warehouse (private server): a private warehouse generally built by the company team
When the corresponding dependent jar package is introduced using coordinates in the project, it will first search whether there is a corresponding jar package in the local warehouse.
-
If so, reference it directly in the project
-
If not, go to the central warehouse to download the corresponding jar package to the local warehouse.
If you can also build a remote warehouse (private server), the search order for jar packages in the future will become: local warehouse --> remote warehouse --> central warehouse
2.4 Maven installation
After getting to know Maven, we will start using Maven, so first we need to download and install Maven.
2.4.1 Download
Download address: https://maven.apache.org/download.cgi
In the provided information, the downloaded installation package has been provided. as follows:
2.4.2 Installation steps
Maven installation and configuration steps:
- Unzip and install
- Configure warehouse
- Configure Maven environment variables
1. Unzip apache-maven-3.6.1-bin.zip (unzip and install)
It is recommended to decompress to a path without Chinese or special characters. For example, unzip it to the following in the course
E:\develop
.
The unzipped directory structure is as follows:
- bin directory: stores executable commands. (Focus on the mvn command)
- conf directory: stores Maven configuration files. (The settings.xml configuration file needs to be modified later)
- lib directory: stores jar packages that Maven depends on. (Maven is also developed using Java, so it also depends on other jar packages)
2. Configure the local warehouse
2.1. Create a new directory on your computer (local warehouse, used to store jar packages)
2.2. Enter the conf directory and modify the settings.xml configuration file.
1). Use Super Notepad software to open the settings.xml file and locate line 53.
2). Copy the label and paste it outside the comment (line 55)
3). Copy the previously created path to store the jar package and replace the tag body content.
3. Configure Alibaba Cloud private server
Since the central warehouse is abroad, downloading jar packages may be slow. Alibaba provides a remote warehouse, which basically contains jar packages for open source projects.
Enter the conf directory and modify the settings.xml configuration file:
1). Use Super Notepad software to open the settings.xml file and locate around line 160.
2). Add sub-tags under the tag with the following content:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
Pay attention to the location of the configuration and add the configuration in the middle of... As shown below:
Note: Only one can be configured (the other needs to be commented!), otherwise the two may conflict, causing the jar package to fail to download!!!
4. Configure environment variables
The configuration of Maven environment variables is similar to the configuration of JDK environment variables.
1). Create a new variable MAVEN_HOME in the system variable
- The value of the MAVEN_HOME environment variable is set to the maven decompression installation directory.
2). Configure in Path
- The value of the PATH environment variable is set to: %MAVEN_HOME%\bin
3). Open the DOS command prompt for verification. If the message shown in the figure appears, the installation is successful.
mvn -v