About Maven, do you really understand it?

Compilation software: IntelliJ IDEA 2019.2.4 x64
Operating system: win10 x64-bit Home Edition
Maven version: apache-maven-3.6.3



1. What are the core concepts of Maven?

1.1 POMs in Maven

Interpretation:

POM, the English full name is Project Object Model , which means the project object model , which is to encapsulate the project into an object model , which is convenient for developers to use Maven to manage and build projects

Common POM tags:

The code example is as follows

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     //设置父工程坐标    
    <parent>
        <artifactId>maven_test</artifactId>
        <groupId>com.bd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven_makefriend</artifactId>
	
	//引入依赖(子工程的jar包或第三方库库)的坐标	
    <dependencies>

        <dependency>
            <groupId>com.bd</groupId>
            <artifactId>maven_make</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.bd</groupId>
            <artifactId>maven_hellofriend</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>



        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            //定义依赖范围
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

1.2 The directory structure agreed by Maven

The detailed directory structure is as follows:

insert image description here

1.3 Maven life cycle

Maven life cycle:

The process of executing each command in a certain order

The Maven life cycle consists of the following three parts:

  • Clean LifeCycle: some cleanup before doing the real project build.

  • Default LifeCycle: The core steps of project construction include compilation, testing, packaging, installation, deployment and other steps.

insert image description here

Note:

When you click "install", it will be executed sequentially from "ccompile" to install [the steps in the red box
], install will execute the installation, it is not to click "install" to directly execute the installation steps. When clean is executed
, it will only clear the target directory under the maven module in IDEA, but will not delete the locally installed jar package, unless you delete it manually.

  • Site LifeCycle: Generate Project Reports, Sites, Publish Sites .

Note:

The above three life cycles do not interfere with each other and are independent of each other . Each lifecycle phase consists of one or more plugin goals. By binding plugins and goals to lifecycle phases in the pom.xml file, it is possible to automate all build tasks with a single command.

1.4 Maven plugins and goals

  • 插件: Its essence is composed of jar package and configuration file , as shown in the figure below

    For example, the plugin maven-clean-plugin is composed as follows:

    insert image description here

  • 目标: Each plugin can implement multiple functions, and each function is a plugin goal.

    For example: compile is a function of the plugin maven-compiler-plugin; pre-clean is
    a goal of the plugin maven-cean-plugin.

1.5 Warehouses in Maven

Classification of warehouses:

  • 本地仓库: Provide maven service for the current computer
  • 远程仓库: Maven service can also be provided for other computers
    • Private server : set up in the current LAN environment, and 当前局域网范围内serve all Maven projects.
    • Central warehouse : set up on the Internet, 全世界serving all Maven projects.
    • Mirror image of the central warehouse : set up on each continent to 中央仓库share traffic. Reduce the pressure on the central warehouse and respond to user requests faster.

File types stored in the warehouse ( mainly jar packages ):

  • Maven plugin
  • jar packages of third-party frameworks or tools
  • Self-developed projects or modules

insert image description here

1.6 Maven coordinates

effect:

Import jar package using coordinates

How are the coordinates formed?

made of gav

g --> groupld: The reverse order of the domain name of the company or organization + the name of the current project

a --> artifactld: the module name of the current project

v --> version: version of the current module

The relevant code examples in pom.xml are as follows:

<parent>
    <artifactId>maven_demo</artifactId>
    <groupId>com.bd</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

Notice:

g-a-v: The location of the jar package in the local warehouse
a-v : the full name of the jar package

insert image description here

How to find the coordinates of the corresponding jar package?

Search the Internet one by one by yourself? No no, the efficiency is too low!

We can go to https://mvnrepository.com or https://central.sonatype.com to find the coordinates of the corresponding jar package

How to use the above two websites to search?

Take the coordinates of searching for junit jar packages in https://mvnrepository.com as an example

①Input the name of the jar package you are looking for in the search bar, then click "Search", find the
column with the most "usages" in the search results (there are often resources we need), and then click to access it

insert image description here

②Find the coordinate reference resource of the jar package of the corresponding version, if not found, go back to step ① to find the next resource column corresponding to " usages "

insert image description here

③Find the jar package name of the corresponding version, and then click in (I take junit 5.9.2 version as an example), copy the code under the Maven column (coordinate reference content)

insert image description here

④ Paste the copied code into <dependencies></dependencies>the tag in the pop.xml file in the module you want to apply, and IEDA will automatically download the jar package resource of this version in the mirror server

insert image description here

insert image description here


2. What is Maven's dependency management?

What are dependencies in Maven?

In Maven, a dependency is an external JAR file or other library that a project requires . Dependency management refers to the process of defining and managing dependencies for a project in Maven.

Why Dependency Management?

By using dependency management, Maven can manage all required JARs and other libraries and ensure that they can be downloaded correctly and work with local projects. There may be scenarios where multiple dependencies depend on different versions of the same third-party library. At this time, it is necessary to select the appropriate dependency version according to the version required by the project to avoid problems caused by version conflicts .

2.1 Dependency scope

Dependency syntax:

<scope></scope>

The following values ​​can be used in <scope>the element :

  1. compile: The default value, indicating that the dependency is available during compilation, testing, and runtime.
  2. provided: Indicates that the dependency is available during compilation and testing, but is provided by the container or environment at runtime.
  3. runtime: Indicates that the dependency is available at runtime, but not required to be available at compile time and test time.
  4. test: Indicates that the dependency is only used in the test code and source code (that is, the src/test/ directory), excluding the main application code and resources.
  5. system: Indicates that the dependencies are similar to provided, but the dependencies are obtained from the local system file system instead of being downloaded from the Maven repository. <systemPath>Paths to dependencies need to be specified via .

Commonly used compile, test, and provided scopes:

  • compile: It is valid in the main directory, test directory, and Tomcat [server].
  • test: Only valid in the test directory.
  • provided: It is valid in the main and test directories, but invalid for Tomcat [server].

2.2 Transitivity of dependencies

What is transitivity of dependencies?

When multiple projects import dependencies, Maven will automatically handle the relationship between dependencies and ensure the smooth construction of the project. When defining dependencies in a project, it automatically finds the dependencies' dependencies and adds them to the build path. This is known as dependency transitivity, because it involves the automatic transfer between a series of interdependent libraries.

For example, if you use Spring framework in your project, and Spring Framework itself has multiple dependencies, such as Spring Core, Spring Beans and Spring Context, etc. However, you don't need to specify all of these dependencies individually in the Maven file - just provide declarations of dependencies on Spring Framework. Maven then automatically resolves the transitive nature of Spring Framework and all of its dependencies, bringing them into the project build path.

So how does Maven find the dependencies of the dependencies in the project, and then automatically introduce them into the project build path? Or according to what principle to deal with the relationship between dependencies to build the project?

There are two principles:

  1. The shortest path is first [proximity principle]

Case: Define three maven modules (maven_makefriend, maven_make and maven_hellofriend) in the maven_test project, where the module maven_hellofriend imports the jar package of log4j 1.2.14, maven_make imports the jar package of log4j 1.2.17, and the maven_makefriend module does not import the jar package of log4j; In addition, the module maven_hellofriend depends on the module maven_make, and the module maven_makefriend depends on the module maven_hellofriend. Observe and think about which version of the log4j
jar package will be used by the module maven_makefriend?

insert image description here

①The module maven_make imports the jar package of log4j 1.2.17 and installs it to Maven's local warehouse. The code example is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>maven_test</artifactId>
        <groupId>com.bd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven_make</artifactId>
    <dependencies>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>



        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

② Introduce the jar package of log4j 1.2.14 and the jar package of module maven_make in the module maven_hellofriend

The code example is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>maven_test</artifactId>
        <groupId>com.bd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven_hellofriend</artifactId>
    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
		
		//引入模块maven_make的jar包
        <dependency>
            <groupId>com.bd</groupId>
            <artifactId>maven_make</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


    </dependencies>

③ The module maven_makefriend depends on the module maven_hellofriend, and its jar package is introduced in pop.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>maven_test</artifactId>
        <groupId>com.bd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven_makefriend</artifactId>

    <dependencies>
		//引入模块maven_hellofriend的jar包
        <dependency>
            <groupId>com.bd</groupId>
            <artifactId>maven_hellofriend</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

insert image description here

  1. The first to declare takes precedence

Case: According to the above case scenario, other codes remain unchanged. If the module maven_makefriend imports the jar packages of modules maven_make and maven_hellofriend in pop.xml, which version of the log4j jar package will it use?

The module maven_makefriend introduces the jar packages of the modules maven_make and maven_hellofriend in pop.xml

The code example is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>maven_test</artifactId>
        <groupId>com.bd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven_makefriend</artifactId>

    <dependencies>
		//同时引入模块maven_make与maven_hellofriend的jar包
        <dependency>
            <groupId>com.bd</groupId>
            <artifactId>maven_make</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.bd</groupId>
            <artifactId>maven_hellofriend</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>



        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

insert image description here


3. How to uniformly manage the version number of dependencies in Maven?

grammar:

The sample code is as follows

<properties>
    <spring-version>5.3.17</spring-version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring-version]</version>
    </dependency>
</dependencies>

ps:

The advantage of this is that you only need to modify one place, that is, <properties></properties>the version number inside, and you can modify the values ​​in multiple places, that is, the values ​​of all applications ${spring-version}<dependencies></dependencies> inside . Define the version number on the outside, use it inside, and then manage it in a unified way, and modify the version number of the relevant jar package uniformly, thereby improving the coding efficiency.


4. What is inheritance in Maven?

4.1 Why is inheritance needed?

reason:

If multiple sub-projects use the same jar package, you can extract the jar package to the parent project and use it in the sub-project using [inheritance principle], similar to the subclass in Java inheriting the parent class

Notice:

父工程的打包方式【jar/war/pom】,必须是pom方式

4.2 Maven's inheritance method 1

usage:

You only need to import the coordinates of the specified jar package in pop.xml in the parent project, then all its sub-projects will be forced to import all jar packages in the parent project by default

Case: Create two sub-projects (day01_mavenTest and day01_mavenTest1) and one project day01_mavenHello in the parent project maven_demo, import two jar packages (junit 4.12 and junit-jupiter-api 5.9.2) into the pop.xml in the parent project, and
others The pop.xml in the project does not introduce any jar package coordinates, and demonstrates and observes the inheritance effect

The sample code is as follows:

Introduce the coordinates of two jar packages in pop.xml of the parent project (junit 4.12 and junit-jupiter-api 5.9.2)

//父工程的pop.xml 引入两个jar包的坐标(junit  4.12和junit-jupiter-api 5.9.2)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bd</groupId>
    <artifactId>maven_demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>day01_mavenTest</module>
        <module>day01_mavenTest1</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

insert image description here

there is a question?

Assuming that in the above case scenario, the subproject day01_mavenTest1’s pop.xml wants to import the junit 4.11 jar package coordinates, then its dependency directory will apply the junit 4.12 jar package inherited from the parent project, or in pop.xml The jar package of junit 4.11 that is going to be introduced?

Answer: The JUnit 4.11 jar package to be introduced in pop.xml will be applied to the dependency of the subproject day01_mavenTest1, because the shortest path is given priority according to the transitive rules of dependencies [principle of proximity].

As follows:

insert image description here

4.3 Maven's Inheritance Method 2

In this inheritance mode, sub-projects can freely choose to apply the jar package resources inherited from the parent project

usage:

① Put the imported jar package in the pop.xml in the parent project <dependencyManagement></dependencyManagement>.

The sample code is as follows:

//在父工程中的pop.xml里导入junit 4.12 的jar包,不过是放在
//dependencyManagement</dependencyManagement><dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

②Introduce the relevant jar package of the parent project into the pop.xml in the sub-project, and write it like this (as shown below)

The sample code is as follows:

<parent>
    <artifactId>maven_demo</artifactId>
    <groupId>com.atguigu</groupId>
    <version>1.0-SNAPSHOT</version>
    //加入下面的代码,要写父工程pop.xml的相对路径
    <relativePath>../pom.xml</relativePath>
</parent>
//注意不能加要引入jar包的版本号
 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
</dependencies>

Notice:绝对不能添加指定导入的jar包的版本号


Case: Create two sub-projects (day01_mavenTest and day01_mavenTest1) and one project day01_mavenHello in the parent project maven_demo, import two jar packages (junit 4.12 and junit-jupiter-api 5.9.2) into the pop.xml in the parent project, use The above syntax is introduced, and the pop.xml in the subproject day01_mavenTest uses the syntax of inheritance method 2 to import the coordinate reference of the jar package of the parent project junit 4.12 to demonstrate and observe the inheritance effect

①Import related jar packages in pop.xml in parent project maven_demo

The sample code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bd</groupId>
    <artifactId>maven_demo</artifactId>
    //规定父工程的打包方式必须是pom
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>day01_mavenTest</module>
        <module>day01_mavenTest1</module>
    </modules>

    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    </dependencyManagement>
</project>

②In the pop.xml of the sub-project day01_mavenTest, specify the jar package imported from the junit 4.12 in the parent project

The sample code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>maven_demo</artifactId>
        <groupId>com.bd</groupId>
        <version>1.0-SNAPSHOT</version>
        <!--  以相对路径引入父工程pop.xml      -->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>day01_mavenTest</artifactId>

    <!--  指定导入父工程中的junit 4.12 的jar包  -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

</project>

insert image description here


5. What is Maven's aggregation?

Interpretation:

In Maven, aggregation (Aggregation) is a special way to organize and build multiple projects together. This can make multiple projects into a single build, performing all build steps under one command.

Aggregations are often used to handle multiple projects with the same build requirements. For example, a large application may consist of multiple modules, each of which needs to be compiled, tested, packaged, and deployed as an independent library or component. In this case, all modules can be configured to be built using Maven aggregates, with a parent pom.xml file defining the relationship between them.

5.1 What happens if you don't use Maven's aggregates?

Without using aggregates, you can't manage multiple submodules in a project, and you can't build an entire codebase at once. Also, without aggregation, each module needs to be compiled, tested and deployed individually, adding to the workload and complexity. Therefore, using Maven's aggregation function can greatly simplify project management and improve development efficiency.

5.2 Benefits of using Maven aggregation

benefit:

As long as the sub-projects are aggregated into the parent project, the effect can be achieved (when the parent project is installed or cleared, the sub-projects will perform synchronous operations) . In other words, when you click install or clean of the parent project, while the parent project is installed, its sub-projects will also be installed at the same time, and they will be cleaned together when they are cleaned.

5.3 How to use Maven's aggregation?

Maven uses <modules>the element to manage all related subprojects (submodules). This element contains a series of <module>sub- elements, where each sub-element specifies the directory name of the Maven project to be aggregated

grammar:

The code example is as follows

<modules>
    <module>day01_mavenTest</module>
    <module>day01_mavenTest1</module>
</modules>

<modules>The element contains two sub-elements, namely day01_mavenTest and day01_mavenTest1. This means that the Maven project where this pop.xml is located will aggregate these two projects and perform their builds together under one command.

Note that a certain Maven project is to be used as an aggregated "container", which is responsible for aggregating and accommodating other Maven projects to be aggregated. It is written in pop.xml under the Maven project as an "aggregation container", similar to the abstraction in Java The concept of class and interface

Case: Demonstrate the function of Maven aggregation (click the "install" function to install the parent project, and install its sub-project at the same time)

Before installation:

insert image description here

After installation:

insert image description here

insert image description here

Notice:

Maven will automatically install subprojects in the order of dependencies

Guess you like

Origin blog.csdn.net/siaok/article/details/130413799
Recommended