[JaveWeb Tutorial] (11) An article teaches you how to easily install and configure Maven (the most detailed)

Insert image description here

Maven

Course content

  1. First introduction to Maven
  2. Maven overview
    • Maven model introduction
    • Maven warehouse introduction
    • Maven installation and configuration
  3. IDEA integrates Maven
  4. 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 .

Insert image description here

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?

  1. Dependency management
  2. Unify project structure
  3. Project build

Dependency management :

  • Conveniently and quickly manage the resources (jar packages) that the project depends on to avoid version conflicts.

Insert image description here

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.

Insert image description here

**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:

Insert image description here

If we create a maven project, it can help us automatically generate a unified and standard project directory structure:

Insert image description here

The specific unified structure is as follows:

Insert image description here

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

Insert image description here

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.

Insert image description here

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:

  1. Convenient dependency management
  2. Unified project structure
  3. 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)

Insert image description here

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

Insert image description here

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:

Insert image description here

Coordinates are the unique identifier of the resource (jar package). Through the coordinates, the location of the required resource (jar package) can be located.

Insert image description here

3). Dependency management model (Dependency)

Insert image description here

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.

Insert image description here

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

Insert image description here

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:

Insert image description here

2.4.2 Installation steps

Maven installation and configuration steps:

  1. Unzip and install
  2. Configure warehouse
  3. 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.

Insert image description here

The unzipped directory structure is as follows:

Insert image description here

  • 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)

Insert image description here

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.

Insert image description here

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>

Insert image description here

Pay attention to the location of the configuration and add the configuration in the middle of... As shown below:

Insert image description here

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.

Insert image description here

2). Configure in Path

  • The value of the PATH environment variable is set to: %MAVEN_HOME%\bin

Insert image description here

3). Open the DOS command prompt for verification. If the message shown in the figure appears, the installation is successful.

mvn -v

Insert image description here

Guess you like

Origin blog.csdn.net/shenchengyv/article/details/135447028