2.4.11 Maven advanced, dependency transfer/conflict/adjustment/exclusion, version locking, properties tag, aggregation project (sub-module), inheritance/aggregation

table of Contents

Maven

1. Maven basics (learned)

2. Maven advanced

2.1 Review of Maven basic knowledge

2.1.1 Introduction to maven

2.1.2 maven warehouse type

2.1.3 Maven commonly used commands

2.1.4 Maven coordinate writing specification

2.2 Maven's dependency transfer

2.2.1 What is dependency passing

2.2.2 How to resolve dependency conflicts

2.2.3 Relying on the principle of regulation-the principle of first declaring priority (understanding)

2.2.4 Relying on the principle of adjustment-the principle of having the closest path first

2.2.5 Exclude dependencies

2.2.6 Version Lock

2.2.7 Use of properties tag

2.3 Maven aggregation project (sub-module)

2.3.1 Analysis of sub-module construction of maven project

2.3.2 Inheritance of maven project

2.3.3 Maven project aggregation

2.3.3 Maven Aggregation Project_Building Lagou Education Background Management System


 

Maven

1. Maven basics (learned)

2. Maven advanced

2.1 Review of Maven basic knowledge

2.1.1 Introduction to maven

Maven is a project management tool whose main function is to perform dependency management and project construction on Java projects during the project development phase.

Dependency management: is the management of jar packages. By importing the maven coordinates, it is equivalent to importing the jar package in the warehouse into the current project.

Project construction: The entire process of cleaning, compiling, testing, reporting, packaging, and deployment of the project can be completed with a single command of maven.

 

2.1.2 maven warehouse type

1. Local warehouse

2. Remote warehouse
①maven central warehouse (address: http://repo2.maven.org/maven2/ )
②maven private service (a warehouse in the company's local area network, you need to build it yourself)
③ other public remote warehouses (such as the remote warehouse provided by Apache, address : Http://repo.maven.apache.org/maven2/ )

Local warehouse---"maven private server---"maven central warehouse

 

2.1.3 Maven commonly used commands

clean: clean
compile: compile
test: test
package: package
install: install

 

2.1.4 Maven coordinate writing specification

 

2.2 Maven's dependency transfer

2.2.1 What is dependency passing

In maven, dependencies can be passed, assuming there are three projects, namely project A, project B and project C. Assuming that C depends on B and B depends on A, then we can easily conclude that project C also depends on A according to the characteristics of the maven project.

As can be seen from the above figure, our web project directly depends on spring-webmvc, and spring-webmvc depends on sping-aop, spring-beans, etc. The end result is that we indirectly depend on spring-aop, spring-beans, etc. in our web project.

Dependency conflict

Due to the existence of dependency transfer phenomenon, spring-webmvc depends on spirng-beans-5.1.5 and spring-aop depends on spring-beans-5.1.6, but it is found that spirng-beans-5.1.5 has been added to the project, and we hope spring- beans-5.1.6 join the project. This creates dependency conflicts.

2.2.2 How to resolve dependency conflicts

1. Use the dependency mediation principle provided by maven

    The first statement is a plus in principle
       path recently, the principle of priority

2. Eliminate dependencies

3. Locked version

 

2.2.3 Relying on the principle of regulation-the principle of first declaring priority (understanding)

The dependency is defined in the pom file, and the dependency declared first shall prevail. In fact, it is to determine which of the passed dependencies is ultimately used according to the order of coordinate import.


Conclusion: As you can see from the above figure, spring-aop and spring-webmvc are passed spring-beans, but because spring-aop is in front, the final spring-beans used is passed by spring-aop, and spring -The spring-beans passed by webmvc are ignored.

 

2.2.4 Relying on the principle of adjustment-the principle of having the closest path first

Summary: Direct dependence is greater than dependence transfer

 

2.2.5 Exclude dependencies

You can use the exclusions tag to exclude the passed dependencies.

2.2.6 Version Lock

The method of directly locking the version is used to determine the version of the dependent jar package. After the version is locked, the declaration order of the dependency or the path of the dependency is not considered, and the locked version shall prevail. This method is often used in enterprise development.

How to use version lock:

Step 1: Lock the dependent version in the dependencyManagement tab

Step 2: Declare the maven coordinates that need to be imported in the dependencies tag

①Lock the dependent version in the dependencyManagement tab

②Declare the maven coordinates that need to be imported in the dependencies tag

2.2.7 Use of properties tag

Convenient to modify the versions of various related dependencies

<?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.lagou</groupId>  
  <artifactId>maven_advanced</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>war</packaging>

  <!--抽取版本号, 以后修改方便-->
  <properties> 
    <spring.version>5.1.7.RELEASE</spring.version> 
  </properties>

  <!--锁定jar包版本-->  
  <dependencyManagement> 
    <dependencies> 
      <dependency> 
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>${spring.version}</version> 
      </dependency>  
      <dependency> 
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>${spring.version}</version> 
      </dependency> 
    </dependencies> 
  </dependencyManagement>  
  <dependencies> 
    <!--  &lt;!&ndash;spring mvc&ndash;&gt;
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>5.1.5.RELEASE</version>
              <exclusions>
                  <exclusion>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-beans</artifactId>
                  </exclusion>
              </exclusions>

          </dependency>

          &lt;!&ndash;spring aop&ndash;&gt;
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aop</artifactId>
              <version>5.1.6.RELEASE</version>
          </dependency>-->

    <!--下面不用配置版本号, 因为上面已经锁定了-->
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-beans</artifactId> 
    </dependency> 
  </dependencies> 
</project>

 

 

2.3 Maven aggregation project (sub-module)

concept:

In real life, when car manufacturers produce cars, because the entire production process is very complicated and cumbersome, and the workload is very large, the manufacturers will separate the parts of the entire car to produce, and finally assemble the produced parts to form a unit. Complete car.

 

 

2.3.1 Analysis of sub-module construction of maven project

In the development of enterprise projects, due to the large scale of the project, the complexity of the business, and the large number of people involved, a large-scale project is generally divided into N multiple small modules through reasonable module splitting and developed separately. And the split modules can be reused by other modules very easily

Common split in two ways:
first: splits on business modules, each split into a maven project, for example, a project is divided into user module, set
a single module, shopping cart module, each Module correspondence is a maven project.
The second type: split according to layers, such as persistence layer, business layer, presentation layer, etc., each layer corresponds to a maven project

Regardless of the above split method, a parent project is usually provided to extract some common codes and configurations into the parent project for unified management and configuration.

 

2.3.2 Inheritance of maven project

In the Java language, classes can be inherited. Through inheritance, subclasses can reference non-private properties and methods in the parent class. Similarly, inheritance between maven projects is also possible. After the subproject inherits the parent project, the dependencies introduced in the parent project can be used. The purpose of inheritance is to eliminate duplicate code.

The parent project generally does not write code, it is only used for unified resource management 

 

2.3.3 Maven project aggregation

In the pom.xml file of the maven project, you can use tags to aggregate other maven projects together. The purpose of aggregation is to perform unified operations.

For example, there are multiple maven projects after splitting. If you want to package, you need to execute the packaging commands for each project, which is very cumbersome.

At this time, you can use the <modules> tag to aggregate these projects into the maven parent project. When you need to package, you only need to execute the packaging command once in this project, and the aggregated projects under it will be packaged.

 

2.3.3 Maven Aggregation Project_Building Lagou Education Background Management System

The overall structure of the project is as follows:

1) Lagou_edu_home_parent is the parent project, and the other projects are sub-projects, all inherit the parent project lagu_edu_home_parent

2) Lagou_edu_home_parent project aggregates all its subprojects

3) There are dependencies between sub-projects:

ssm_domain depends on ssm_utils

ssm_dao depends on ssm_domain

ssm_service depends on ssm_dao

ssm_web depends on ssm_service

Parent project

<?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.lagou</groupId>
    <artifactId>maven-parent</artifactId>
    <!--pom打包类型-->
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <!--聚合所有工程-->
    <modules>
        <module>maven-domain</module>
        <module>maven_dao</module>
        <module>maven-service</module>
        <module>maven-web</module>
    </modules>
</project>

Subproject

①domain layer

<?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-parent</artifactId>
        <groupId>com.lagou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven-domain</artifactId>
    
</project>

②dao layer

<?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-parent</artifactId>
        <groupId>com.lagou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven_dao</artifactId>

    <!--引入domain层的依赖-->
    <dependencies>
        <dependency>
            <groupId>com.lagou</groupId>
            <artifactId>maven-domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

③service layer

<?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-parent</artifactId>
        <groupId>com.lagou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven-service</artifactId>

    <!--引入dao层的依赖-->
    <dependencies>
        <dependency>
            <groupId>com.lagou</groupId>
            <artifactId>maven_dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

④web layer

<?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-parent</artifactId>  
    <groupId>com.lagou</groupId>  
    <version>1.0-SNAPSHOT</version> 
  </parent>  
  <modelVersion>4.0.0</modelVersion>  
  <artifactId>maven-web</artifactId>
  <packaging>war</packaging>

  <!--引入service层的依赖-->
  <dependencies>
    <dependency>
      <groupId>com.lagou</groupId>
      <artifactId>maven-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

Guess you like

Origin blog.csdn.net/chengh1993/article/details/110824395