Maven polymerization project

Posted by BhA on Sun, 30 Jan 2022 22:41:28 +0100

1.1 review of Maven basic knowledge

2.1.1 introduction to maven

maven is a project management tool, which is mainly used to manage and build Java projects in the project development stage.

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

Project construction: the whole process of project cleaning, compiling, testing, reporting, packaging and deployment can be completed through a command of maven.

2.1.2 warehouse type of maven

  1. Local warehouse
  2. Remote warehouse
    1. maven central warehouse (address) http://repo2.maven.org/maven2/)
    2. maven private server (the warehouse in the company's LAN needs to be built by yourself)
    3. Other public remote warehouses (such as the remote warehouse provided by apache, address: http://repo.maven.apache.org/maven2/)

Search order: local warehouse = > Maven private server = > Maven central warehouse

2.2 maven dependency transfer

2.2.1 what is dependency passing

As can be seen from the above figure, our web project directly relies on spring webmvc, while spring webmvc relies on spring AOP, spring beans, etc. the final result is that our web project indirectly relies on spring AOP, spring beans, etc.

2.2.2 dependency conflict

Due to the existence of dependency transfer, spring webmvc relies on sping-beans-5.1.5 and spring AOP relies on spring-beans-5.1.6. However, it is found that sping-beans-5.1.5 is added to the project, and we hope spring-beans-5.1.6 is added to the project, which leads to dependency conflict.

2.2.3 how to resolve dependency conflicts

Dependent regulation principle

Principle of first declaration first

Define dependency in pom file, and the dependency declared earlier shall prevail.

Principle of first priority for those who are close to the route

Direct dependency is greater than dependency transmission (that is, indirect dependency is introduced according to demand and becomes direct dependency).

Exclude dependencies

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

Locked Version (most commonly used)

The method of directly locking the version is adopted to determine the version that depends on the jar package. After the version is locked, the dependent declaration order or dependent path will not be considered, and the locked version will prevail to be added to the project. This method is often used in enterprise development.

Usage of version locking:

pom.xml

Step 1: lock the dependent version in the dependency management tab

Step 2: declare the maven coordinates to be imported in the dependencies tab

Use of properties tag

Usage scenario: the jar packages are upgraded uniformly. If it takes too much time to modify the versions in dependency management in turn, you can extract the version information into properties.

<properties>
    <spring.version>5.1.5.RELEASE</spring.version>
    <springmvc.version>5.1.5.RELEASE</springmvc.version>
    <mybatis.version>3.5.1</mybatis.version>
</properties>

<!--locking jar edition-->
<dependencyManagement>
    <dependencies>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- springMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springmvc.version}</version>
    	</dependency>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

2.3 maven polymerization project (sub module)

2.3.1 analysis of sub module construction maven project

In enterprise project development, due to the large scale of the project, complex business and large number of participants, a large project is generally divided into N small modules for development through reasonable module splitting. And the separated modules can be easily reused by other modules.

There are two common splitting methods:

The first method is to split according to business modules. Each module is divided into a maven project. For example, a project is divided into user module, order module, shopping cart module, etc. each module corresponds to a maven project.

The second is to split according to layers, such as persistence layer, business layer and presentation layer. Each layer corresponds to a maven project.

Regardless of the above splitting method, a parent project is usually provided to extract some public code and configuration into the parent project for unified management and configuration.

2.3.2 inheritance of Maven project

In the Java language, classes can inherit. Through inheritance, subclasses can reference non private properties and methods in the parent class. Similarly, you can inherit between maven projects. After the child project inherits the parent project, you can use the dependencies introduced in the parent project. The purpose of inheritance is to eliminate duplication of code.

2.3.3 aggregation of Maven project

POM in maven project The < modules > tag can be used in the XML file to aggregate other maven projects. The purpose of aggregation is to carry out unified operation.

For example, there are multiple maven projects after splitting. If you want to package, you need to execute the packaging command for each project, which is very cumbersome. At this time, you can use tags to aggregate these projects into maven parent projects. When packaging is needed, you only need to execute the packaging command in this project once, and the aggregated projects under it will be packaged.

You can aggregate without a common parent class, but you should pay attention to writing the directory correctly.

Topics: Maven Project management