Summary of Maven related knowledge

Meet Maven

Insert picture description here
Maven is an automated build tool organized and maintained by the Apache Software Foundation. It focuses on project construction and dependency management of Java platform projects:
1: Project
construction is not a creation. Creating a project does not mean building a project. You can use Maven to manage the project. The entire life cycle of development
2: Dependency management
manages all the jar files that the project depends on. The Maven project depends on a library, and this library depends on other libraries. You only need to add directly dependent libraries, and indirect dependent libraries will also be added to the project. Medium
Insert picture description here
Assuming the above picture, after the introduction of spring-mvc, spring-core and other libraries that spring-mvc depends on will also be added to the project

Maven download and install

Maven is a Java-based tool, so the first thing to do is to install the JDK. To download and install, you only need to unzip the downloaded files to the specified directory, and then configure the environment variables:
1: Prepare the dependent JDK
2: Download and install
3: Configure environment variables
4: Test whether the installation is successful.
Environment variable configuration:
Insert picture description here
create a new system variable MAVEN_HOME, set the variable value to the decompressed maven directory
Insert picture description here
. Add %MAVEN_HOME%\bin under the path environment variable to
Insert picture description here
open cmd and enter mvn -v to verify whether it is configured success

What can Maven be used for

1: Add a third-party jar
2: Manage the dependencies between jar packages
3: Obtain a third-party jar package
4: Split the project into multiple engineering modules
If you don’t use maven, you need to download the corresponding packages on each framework website , And then introduced into the project, you need to check one by one if you encounter dependency conflicts
Simple example:
1: archetype:generate (interactive mode)
Use mvn archetype:generate command, you need to manually enter project-related information
Insert picture description here
2: archetype:generate (non-interactive mode)

mvn archetype:generate -DgroupId=com.companyname.bank -DartifactId=consumerBanking -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

No need to manually enter information, automatically create a project
3: Create a Maven project through the IDE

Maven core concepts

Development catalog

Conventional configuration:
Maven advocates the use of a common standard directory structure, and adheres to the principle that conventions are greater than configuration
Insert picture description here

Coordinates and warehouse

1:
The three-dimensional coordinates and physical addresses in the coordinate space can be understood as
the unique identifiers of coordinate Maven construction: groupId, artifactId, version
2: Warehouse
Local warehouse: When running Maven, any components needed are directly obtained from the local warehouse
Remote warehouse; central warehouse; private server other third-party libraries
Insert picture description here
First, Maven will locate the jar in the local warehouse through coordinates. If the local warehouse is not found, it will go to the private server to search for it, and finally to the central warehouse
3: Warehouse dependency sequence
local warehouse – Private server – Central warehouse

POM file

POM (project object model, project object model) is the basic unit of work of the Maven project. It is an XML file that contains the basic information of the project. It is used to describe how the project is built and declare the dependencies of the project and other
POM configurations:
1: Project dependency
2 : Plug-in
3: Execution Goal
4: Project build profile
5: Project version
6: Project developer list
7: Related mailing list information When
executing a task or goal:
1: Maven will find the POM in the current directory
2: Read the POM, Obtain the required configuration information, and then execute the target

POM file content

The following briefly introduces the content of the POM file:
POM file project basic information:

    <!--项目的全球唯一标识符-->
    <groupId>asia.banseon</groupId>
    <!-- 构件的标识符-->
    <artifactId>banseon-maven2</artifactId>
    <!--项目产生的构件类型,例如jar、war、ear、pom-->
    <packaging>jar</packaging>
    <!--项目当前版本,格式为:主版本.次版本.增量版本-限定版本号-->
    <version>1.0-SNAPSHOT</version>
    <!--项目的名称, Maven产生的文档用-->
    <name>banseon-maven</name>
    <!--父项目-->
    <parent>
        <!--被继承的父项目的构件标识符-->
        <artifactId/>
        <!--被继承的父项目的全球唯一标识符-->
        <groupId/>
        <!--被继承的父项目的版本-->
        <version/>
    </parent>

There are two types of warehouses in maven, snapshot warehouses and release warehouses. The snapshot repository is used to save the unstable version during the development process, and the release official repository is used to save the stable release version. The
snapshot will continuously deploy, and it will automatically load the latest version when it is used by other systems or individuals.

POM file plug-in information:

<build>
   <!-- 可使用的插件列表 -->
	<plugins>
	   <!--plugin元素包含描述插件所需要的信息。 -->
		<plugin>
		<!--插件定位坐标三元素:groupId + artifactId + version --
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-antrun-plugin</artifactId>
		<version>1.1</version>
		<executions> 
			<execution>
			</execution> 
		</executions>
		</plugin>
	</plugins>
</build>

POM file dependency information:

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
		<type></type>
		<optional></optional>
		<exclusions>
		<exclusion>
		</exclusion>
		</exclusions>
	</dependency>
</dependencies>

Maven dependency management

For multi-module projects (contains hundreds of modules or submodules), the dependencies between modules are very complex and difficult to manage. In response to this problem, Maven provides a core feature: dependency management
There are two main features of dependency management Part:
1: Transitivity of dependency
2: Scope of dependency
Transitive dependency: In
one case, for example: A depends on library B, and C depends on A, then project C also needs to use library B.
Maven reads the POM file to find out the differences between the projects. Dependencies, we only need to define direct dependencies in each project, and other dependencies will be automatically completed by Maven
Dependency conflict
Causes of dependency conflict: caused by dependency transfer
Resolve dependency conflicts
Insert picture description here
Dependency scope:
Transitive dependency discovery can be restricted by using the following dependency scope:
Insert picture description here

Build a life cycle

1: Build life cycle The
Maven build life cycle defines the process of building and publishing a project.
2: The working principle of the Maven life cycle. The work
of each step in the life cycle is done by plugins.
A plug-in usually completes one or more steps of work, and the work of each step corresponds to a goal of the plug-in. Different plug-ins can be combined to complete the construction of the entire project, such as: compiling plug-ins, compiler; testing plug-ins, surefire; packaging plug-ins, jar; installing plug-ins, install; deploying plug-ins, deploy
Three types of life cycle
Maven standard life cycle:
1: Default Lifecycle: Build and deploy the project
2: Clean Lifecycle: Clean up the project
3: Site Lifecycle: Generate project site
Steps of project construction
Insert picture description here
Maven project construction phase
Verification: Verify that the project is correct and all necessary information is available
Compilation: Compile the Java source program into a bytecode (.class) file
Test: Test the key points in the project to ensure that the project is in the iterative development process The correctness of the key points in the
package: package a project into a compressed file for installation or deployment
Check: check the result of the integration test to ensure that the quality meets the standard
Installation: specifically refers to the packaged result in the Maven environment-jar package Or install the war package to the local warehouse and deploy: Deploy the packaged result to a remote warehouse or deploy the war package to the running server. Running Maven can automatically implement all the operations in the above process.
Cycle phase
Clean clean up the project cycle stage
pre-clean: perform some work that needs to be completed before
clean clean: remove all files generated by the last build
post-clean: perform some work that needs to be completed immediately after clean

Site life cycle stage
pre-site: perform some work that needs to be completed before generating site documentation
site: generate project site documentation
post-site: perform some work that needs to be completed after site documentation is generated, and prepare for deployment
site-deploy : Deploy the generated site documents to a specific server

Build a multi-module system

Insert picture description here
What should I do if I need to split the above three parts and reuse them?
Insert picture description here

polymerization

Basic concept: Maven aggregation is to be able to build multiple modules with one command
. The packaging method of aggregation modules must be pom, otherwise the build cannot be completed.

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dragon</groupId>
    <artifactId>flowable</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>flowable-api</module>
        <module>flowable-core</module>
        <module>flowable-manager</module>
        <module>flowable-server</module>
        <module>flowable-parent</module>
    </modules>

inherit

Inheritance is to eliminate duplication. We extract many of the same configurations, for example: groupId, version, etc.
The packaging method of the parent module must be pom, otherwise the project cannot be built.
Parent module:

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
    </parent>
    <packaging>pom</packaging>
    <version>1.0</version>

Submodule:

 <parent>
        <groupId>com.dragon</groupId>
        <artifactId>flowable-parent</artifactId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>flowable-api</artifactId>
    <version>1.0-SNAPSHOT</version>

Aggregation and inheritance

One: The relationship between aggregation and inheritance
1: The aggregation module knows which modules it aggregates, but the aggregated module does not know the existence of the aggregation module
2: The parent module does not know the existence of the submodule, but the submodule must know its parent module who is it

Two: The role
of aggregation and inheritance Through the aggregation and inheritance features of Maven, you can manage and build projects more conveniently

Guess you like

Origin blog.csdn.net/qq_34365173/article/details/107144167