Setup and introduction of maven

1. Maven overview

Maven is a project management tool, including: project object model (POM, Project Object Model), standard collection, project lifecycle (Project Lifecycle), dependency management system (Dependency Management System), and used to run the definition in the life cycle stage ( The logic of the plugin goal in phase.
Insert picture description here

When maven imports the jar package, it does not directly import the jar package into the project, but establishes a reference for storing the jar package. When the jar package needs to be used, add the dependent jar coordinates in the pom.xml file. Maven will find the jar in the warehouse according to the dependency coordinates added in pom.xml. The following also briefly describes the maven dependency management process.

Insert picture description here

As can be seen from the above figure, maven warehouses play an important role in maven projects, and maven warehouses can be divided into three categories: local warehouses , remote warehouses and central warehouses .

The local warehouse refers to the warehouse where the jar package is stored on the machine. When maven searches for the jar package, it will first start the search from the local warehouse. If the local warehouse cannot find the required jar package, it will download the jar package from the remote warehouse. The jar package is still stored in the local warehouse.

  • The default address of the local warehouse is f C:\Users\acer\.m2\repository, which can be modified in the conf/setting.xml file. Generally, it is best to modify it to a path that is easy to find. Note that the path cannot contain Chinese characters.

    Insert picture description here

  • Both remote warehouses and central warehouses are common to the Internet. If you can't find the jar package, Maven will go back to the Internet to download the corresponding jar package. You can also modify and add it in the conf/setting.xml file.

    Insert picture description here

    <!-- 阿里云仓库 -->
    <mirror>
        <id>alimaven</id>
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
    
    <!-- 中央仓库1 -->
    <mirror>
        <id>repo1</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo1.maven.org/maven2/</url>
    </mirror>
    
    <!-- 中央仓库2 -->
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</url>
    </mirror>
    

2. Maven environment variable configuration

Go directly to the official website of maven to download and install the software, and then unzip it to a path without Chinese, you can get the following files

Insert picture description here

Maven commands are stored under bin

Some bootloaders of maven are stored under boot, such as class loader, etc.

Maven configuration files are stored under conf, such as setting.xml

Stored under lib is the dependent jar package that comes with maven

After decompressing the file, you need to set the environment variable, set the system variable M2_HOME, and add the bin path under the path to the path

Insert picture description here

Insert picture description here

Check the configuration after setting, enter under cmdmvn -v

Insert picture description here

3. Maven's IDEA configuration

When using maven on idea, you must first configure maven

Insert picture description here

Insert picture description here

After the maven file is created, a pom.xml file will be automatically generated. According to the needs of the project, you need to add dependencies in pom.xml. For example, if the project depends on hadoop 2.7.5, you need to add the following configuration in the pom

<dependency>
    <!-- jar包所属项目名称 -->
    <groupId>org.apache.hadoop</groupId>
    <!-- jar包名称 -->
    <artifactId>hadoop-common</artifactId>
    <!-- 版本 -->
    <version>2.7.5</version>
</dependency>

If you don’t know the coordinates of the jar package, you can search from the Internet, search for the corresponding jar package on https://mvnrepository.com/, you can generate the corresponding coordinates

Insert picture description here

If after configuring maven, if there is a jar package in the local warehouse, the pom still reports an error

Check whether the suffix of the jar package of the local warehouse is correct. .

Insert picture description here
If the warehouse suffix is ​​as shown in the picture, it needs to be changed tojar suffix

Guess you like

Origin blog.csdn.net/qq_24852439/article/details/104413478