Maven - Introduction

Keywords: Java Maven

The notes of this article are from the dark horse video

1. Introduction to maven

(1) State analysis of traditional project management

(2) Maven introduction

Maven is essentially a project management tool, which abstracts the project development and management process into a project object model
(POM), where POM (Project Object Model) refers to the project object model.

(3) Maven's role

① Project construction: provide standard and cross platform automated project construction methods.
② Dependency management: manage project dependent resources (jar packages) conveniently and quickly to avoid version conflicts between resources.
③ Unified development structure: provide a standard and unified project structure.

2. Download and installation of maven

(1) Download Maven for Windows

① Maven's official website is

② Download the installation package and source code of Windows version.

Or go straight to Select the appropriate version for download. Here I download version 3.8.1 (as shown in the figure below).

(2) Install Maven

① Put the installation package downloaded above into the appropriate directory (the path name should preferably not have Chinese or other special characters), decompress it (Maven belongs to the green version software, decompress and install it), and then decompress it, as shown in the following figure.

② Configure environment variables (JDK needs to be installed and configured in advance, and the version is 1.7 or above)

③ To test whether Maven is successfully installed, you only need to enter the command mvn or mvn -v in the command line window. If the following information appears, Maven has been successfully installed.

3.Maven basic concept

(1) Warehouse

① Warehouse: used to store resources, including various jar packages
② Warehouse classification:

Local warehouseStore resources on your own computer and connect to the remote warehouse to obtain resources
Remote warehouseThe warehouse on a non local computer provides resources for the local warehouse

In addition, remote warehouses are divided into the following two types:

Central warehouseA warehouse maintained by the Maven team that stores all resources
Private serviceThe warehouse that stores resources within the Department / company and obtains resources from the central warehouse

③ Role of private server:
1) Save copyrighted resources, including purchased or self-developed jars (jars in the central warehouse are open source and cannot store copyrighted resources).
2) Share resources within a certain range, which is only open to the inside and not to the outside.

(2) Coordinates

① Coordinates are used to describe the location of resources in the warehouse (see website)
② Maven coordinates are mainly composed of:

groupldDefine the name of the organization to which the current Maven project belongs (usually the domain name is backfilled, for example: org.mybatis)
artifactldDefine the name of the current Maven project (usually the module name, such as CRM, SMS)
versionDefines the version number of the current project

③ Function of Maven coordinates:
The unique identification is used to uniquely locate the resource location. Through this identification, the resource identification and download can be completed by the machine.

(3) Local warehouse configuration

① After Maven starts, it will automatically save the downloaded resources to the local warehouse.
② The settings.xml file in the conf directory is Maven's global configuration file. The default location described in it is as follows:

<!-- The current directory location is under the directory where the login user name is located.m2 Folder -->

This location is located in Disk C, but generally speaking, the location of resource saving should be customized in other disks. For example, the location can be customized in the settings.xml file (the following configuration should be in the Settings tab):


(4) Remote warehouse configuration

The warehouse location of Maven's default connection is as follows:

		<name>Central Repository</name>
		<layout>default</ layout>

(5) Mirror warehouse configuration

However, since the default warehouse is located on a foreign server, if you access it in China, the speed will generally be very slow. Therefore, you can use a domestic image warehouse, such as Alibaba cloud image warehouse, and configure Alibaba cloud image warehouse in setting.xml:

	<!--Configure the download image of the specific warehouse-->
		<!--Unique identifier of this image to distinguish between different images mirror element-->
		<!--What kind of warehouse to mirror is simply to replace which warehouse-->
		<!--Image name-->
		<name>Nexus aliyun</name>
		<!--image URL-->
		<url>http://maven.aliyun. com/nexus/content/groups/public</url>

(6) Global setting and user setting

① Global setting defines the public configuration of Maven in the current calculator (settings.xml file in conf directory).
② User setting defines the current user's configuration (you can copy the settings.xml file in the conf directory, put it in other locations, and customize and modify it). When the user setting is inconsistent with the global setting, the former will overwrite the latter.

4.Maven project creation

(1) Create manually

Manually create a maven project according to the following Maven project directory structure (you can watch the video p6 for the specific creation process).

(2) Plug in creation

① Create project

mvn archetype:generate

② Create java project

mvn archetype:generate -DgroupId=com.itheima -DartifactId=java-project -
DarchetypeArtifactId=maven-archetype-quickstart -Dversion=0.0.1-snapshot -

③ Create web project

mvn archetype:generate -DgroupId=com.itheima -DartifactId=web-project -
DarchetypeArtifactId=maven-archetype-webapp -Dversion=0.0.1-snapshot -

(3) IDEA creation

① Configure Maven environment in IDEA

② Create Maven project

At this point, a maven project is created. In addition, if you want to change the resource type of the folder, you can also right-click and select Mark Directory as to modify it (generally keep Maven's default).

Introduce the required dependencies in pom.xml. Here, take Junit's jar as an example (you need to download it online during the initial introduction. If the code reports an error, you can click Maven on the right and select refresh in it). Then Junit's jar package will be imported into the project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



See the website for the representation of resource coordinates in pom.xml

③ Write test code

package com.itheima;

public class Demo {
    public String say(String name){
        System.out.println("Hello "+name);
        return "Hello "+name;
package com.itheima;

import org.junit.Assert;
import org.junit.Test;

public class DemoTest {
    //unit testing 
    public void testSay(){
        Demo d = new Demo();
        String ret = d.say("itheima");
        Assert.assertEquals("Hello itheima",ret);

④ Operating projects with Maven

Or use another method:

④ Creating projects using Maven templates

1) Create a normal Java project

Note: the resources directory needs to be added manually and marked as the same resource type as above.
2) Create a Java Web project

Configure pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" 

  <!--appoint pom Model version for-->
  <!--Packaging method, web The project is packaged as war,java The project is packaged as jar-->

  <!--organization id-->
  <!--project id-->
  <!--Version number:release,snapshot-->

  <!--Set all dependencies of the current project-->
    <!--Specific dependencies-->

    <!--Setting plug-ins-->
      <!--Specific plug-in configuration-->

Note: the missing directory needs to be added manually and marked as the same resource type as above.

5. Dependency management

(1) Dependency configuration

① Dependency refers to the jar s required for the current project to run. A project can set multiple dependencies.
② Format:

<!--Sets all the properties that the current project depends on jar-->
	<!--Set specific dependencies-->
		<!--Dependent group id-->
		<!--Dependent items id-->
		<!--Dependent version number-->

(2) Dependency delivery

① Dependency is transitive
1) Direct dependency: the dependency relationship established through dependency configuration in the current project.
2) Indirect dependency: if a resource is dependent on other resources, the current project indirectly depends on other resources.

② Dependency delivery conflict
Path priority: when the same resource appears in the dependency, the deeper the level, the lower the priority, and the shallower the level, the higher the priority.
Declaration priority: when resources are dependent at the same level, the top configuration order overrides the bottom configuration order.
Special priority: when different versions of the same resource are configured at the same level, the later configured version overrides the first configured version.
③ If project-1 wants to use project-2, it needs to configure project-2 as a resource in project-1, that is, copy the coordinates corresponding to project-2 (including groupId, artifactId and version) to pom.xml in project-1 in the form of dependency (i.e. using the dependency tag).

(3) Optional dependency

Optional dependency refers to hiding the currently dependent resources externally - opaque

	<artifactrd>junit</ artifactrd>

(4) Exclude dependencies

Excluding dependencies refers to actively disconnecting dependent resources. The excluded resources do not need to specify a version - No


(5) Dependency range

① Dependent jar s can be used anywhere by default, and their scope can be set through the scope tag
② Scope of action
1) The main program range is valid (within the main folder range)
2) The scope of test program is valid (within the scope of test folder)
3) Whether to participate in packaging (within the scope of package instruction)

③ Dependency scope transitivity
When a resource with a dependency scope is passed, the scope will be affected

6. Life cycle and plug-ins

(1) Life cycle

① The Maven build lifecycle describes how many events a build process goes through

② Maven divides the life cycle of project construction into the following three sets:

cleanClean up work
defaultCore work, such as compilation, testing, packaging, deployment, etc
siteGenerate reports, publish sites, etc

The life cycle of clean is as follows:

pre-cleanPerform some work that needs to be done before clean
cleanRemove all files generated by the last build
post-cleanPerform some work that needs to be done immediately after clean

The life cycle of the default build is as follows:

The life cycle of site construction is as follows:

pre-sitePerform some work that needs to be done before generating site documents
siteGenerate site documents for the project
post-siteDo some work that needs to be done after the site document is generated, and prepare for deployment
site-deployDeploy the generated site documents to a specific server

(2) Plug in

① The plug-in is bound to the stage in the life cycle and executes the corresponding plug-in function when it is executed to the corresponding life cycle.
② By default, maven is bound with preset functions in each life cycle.
③ Other functions can be customized through plug-ins.

	<!--Setting plug-ins-->
		<!--Specific plug-in configuration-->
			<!--Coordinates of the plug-in-->
			<!--Actions performed by the plug-in-->
				<!--Specific operations performed by the plug-in-->
                            jar:Package the source code
                            test-jar:Package test code

At this point, you should have a deeper understanding of the following figure.

7. Sub module development and design

First, you need to prepare a Maven based SSM project in advance (the function can be very simple), and then split the module according to the following figure.

(1) POJO module splitting

(2) Dao module splitting

(3) service module splitting

(4) controller module split

(5) Summary

(1) The module only contains the function classes and configuration files corresponding to the current module.
(2) spring core configuration is made independently according to different module functions.
(3) The modules that the current module depends on can be used only after they are added to the current module in the form of imported coordinates.
(4) web.xml needs to load all spring core configuration files.

8. Polymerization

(1) Function

Aggregation is used to quickly build maven projects and build multiple projects / modules at one time.

(2) Production method

① Create an empty module with the packaging type defined as pom


② Defines the names of other modules associated with the current module during the build operation


Note: the final execution order of the modules participating in the aggregation operation is related to the dependencies between modules, not the configuration order.

9. Succession

(1) Module dependency maintenance

(2) Inheritance function and production mode

① Function: through inheritance, the configuration in the parent project can be used in the child project (inheritance in Maven is similar to inheritance in java, and the inheritance relationship can be configured in the child project)
② Manufacturing method: declare the coordinates and corresponding positions of the parent project in the child project

<!--Define the parent project of the project-->
	<!--Fill in the parent project pom.xml File path-->

(3) Inheritance dependency definition and dependency usage

① Define dependency management in parent project

<!--Declare dependency management here-->
	<!--Specific dependencies-->
		<!--spring environment-->

② The dependency relationship is defined in the child project without declaring the dependent version. The version refers to the dependent version in the parent project

	<!--spring environment-->

In addition, it should be noted that the inherited resources are as follows:

(4) Comparison of inheritance and aggregation

effectFor quick configurationFor quick build projects
Same pointThe pom.xml files of aggregation and inheritance are packaged in POM, and the two relationships can be made into the same POM fileAggregation and inheritance belong to design modules, and there is no actual module content
differenceAggregation is to configure relationships in the current module. Aggregation can sense which modules participate in aggregationInheritance is to configure relationships in sub modules. The parent module cannot perceive which sub modules inherit itself

10. Attribute version management

(1) Attribute category

Custom attribute, built-in attribute, Setting attribute, Java system attribute, environment variable attribute

(2) Attribute category: custom attribute

① Function: equivalent to defining variables to facilitate unified maintenance
② Define format:

<!--Custom properties-->

③ Call format:


(3) Attribute category: built in attribute

① Function: use Maven built-in properties to quickly configure
② Call format:


(4) Attribute category: Setting attribute

① Function: use the tag attribute in Maven configuration file setting.xml for dynamic configuration
② Call format:


(5) Attribute category: Java system attributes

① Function: read Java system properties
② Call format:


③ System attribute query method:

mvn help;system

(6) Attribute category: environment variable attribute

① Function: use the label attribute of the system environment variable for dynamic configuration
② Call format:


③ Environment variable attribute query method:

mvn help;system

11. Version management

(1) Project version differentiation

(2) Project version classification

SNAPSHOT (SNAPSHOT version)In the process of project development, in order to facilitate the cooperation of team members and solve the problems of interdependence and constant updating between modules, when developers build each module, the output temporary version is called snapshot Version (test phase version). In addition, the snapshot version will be updated continuously with the progress of development.
RELEASE (RELEASE version)After the project development reaches the stage milestone, a relatively stable version is released to the outside of the team. The component file corresponding to this version is stable. Even if the subsequent development of functions will not change the content of the current release version, this version is called the release version.

(3) Project version number agreement

Agreed specifications:
① < major version >, < minor version >, < incremental version >, < milestone version >
② Major version: indicates major architectural changes of the project, such as the iteration of spring 5 compared with spring 4.
③ Minor version: indicates that there are major function additions and changes, or vulnerabilities are repaired comprehensively and systematically
④ Incremental version: indicates the repair of major vulnerabilities..
⑤ Milestone version: indicates the milestone of a version (internal version). Compared with the next formal version, this version is relatively unstable and needs more testing.

12. Resource allocation

(1) Function

Load the properties defined in the pom.xml file in any configuration file (take the database configuration file as an example here).

(2) Call format:


(3) Open the configuration file to load the properties in pom.xml

<!--Custom properties-->

    <!--Information corresponding to the configuration resource file-->
            <!--Set the location directory corresponding to the configuration file, and support the dynamic setting of the directory using attributes-->
            <!--Turn on resource loading filtering for configuration files-->

12. Multi environment development configuration

(1) Multi environment compatibility

(2) Multi environment configuration in pom.xml

<!--Create multi environment-->
    <!--Define specific environment: production environment-->
        <!--Defines the unique name of the environment-->
        <!--Define attribute values that are specific to the environment-->
        <!--Set default startup-->
    <!--Define specific environment: development environment-->
        <!--Defines the unique name of the environment-->
        <!--Define attribute values that are specific to the environment-->

(3) Load specified environment

① Function: load the specified environment configuration
② Call format:

mvn instructions -P Environment definition id

③ Example:

mvn install -P pro_env

13. Skip test

(1) Application scenarios that skip the test phase

① The overall module function is not developed
② A function in the module has not been developed
③ Single function update debugging leads to failure of other functions
⑤ Need quick packaging

(2) Skip test with command

① Command:

mvn instructions -D skipTests

② Note: the executed instruction life cycle must include a test phase

(3) Skip test using interface operation

(4) Skip test using configuration

		<!--Set skip test-->
		<!--Contains the specified test case-->
			<include>** /User*</include>
		<!--Exclude specified test cases-->

14. Private service

(1) Sub module development cooperation

(2) Download, install, start and configure Nexus

Nexus is a maven private service product of Sonatype company.

① Download

The download address is:

② Installation

After downloading, unzip it and rename it nexus.

③ Start

The startup command is: nexus /run nuxus

The access address is: http://localhost:8081/

④ Disposition

1) Modify basic configuration information
The file in the etc directory under the installation path stores the basic nexus configuration information, such as the default access port
2) Modify server running configuration information
The nexus.vmoptions file in the bin directory under the installation path stores the configuration information corresponding to the startup of the nexus server, such as the memory space occupied by default

(3) Obtain private server resources

(4) Warehouse classification

① Hosted warehouse hosted
Save resources that cannot be obtained from the central warehouse (generally from independent research and development or third-party non open source projects).
② Proxy warehouse proxy
Agent remote warehouse to access other public warehouses, such as central warehouse, through nexus.
③ Warehouse group
Several warehouses form a group to simplify the configuration. In addition, a warehouse group cannot save resources. It is a design warehouse.

(5) Resource upload and download in IDEA environment

(6) Access private server configuration

① Local warehouse access private server
1) Configure the permissions of the local warehouse to access the private server (setting.xml)


2) Configure local warehouse resource source (setting.xml)


② Project access server
1) Configure the save location (pom.xml) of the private server upload resources accessed by the current project


2) Command to publish resources to private server

mvn deploy

Posted by xfluous on Wed, 29 Sep 2021 11:13:44 -0700