Review of Maven basics
Maven is introduced
Maven is a project management tool for dependency management and project construction of Java projects during project development. Dependency management: is the management of JAR packages. By importing Maven coordinates, you import jars from the repository into the current project. Project build: Clean up, compile, test, report, package, and deploy projects with a single command from Maven.
Maven repository type
1. Local repository 2. Remote repository ① Maven central repository (address: repo2.maven.org/maven2/) ② Maven private server (Intranet repository, need to build their own) ③ Other public remote repository (such as apache remote repository, address: Repo.maven.apache.org/maven2/) local repository – “maven private servers -” the maven central repository
Maven commands
Clean, compile, test, package, install
Maven coordinate writing specification
Maven’s dependency passing
What is dependency passing
In Maven, dependencies can be passed, assuming there are three projects: Project A, Project B, and project C. Assuming that C depends on B and B depends on A, we can deduce that project C also depends on A according to the characteristics of Maven project dependencies.
As you can see from the figure above, our Web project relies directly on Spring-WebMVC, which in turn relies on SpingAOP, Spring-Beans, and so on. The end result is indirect dependencies on Spring-AOP, spring-Beans, and so on in our Web project.
Rely on the conflict
Spring-webmvc relies on sprng-beans-5.1.5 and spring-aop relies on spring-beans-5.1.6, but spirng-beans-5.1.5 was added to the project. Spring-webmvc relies on spring-beans-5.1.5 and spring-aop relies on spring-beans-5.1.6. We want to add Spring-beans-5.1.6 to the project. This creates dependency conflicts.
How can dependency conflicts be resolved
- 1. Use dependency mediation principles provided by Maven
- The first person to declare priority
- The shortest path is first
- 2. Eliminate dependencies
- 3. Lock the version
The principle of dependency regulation – first declarant first
Define the dependencies in the POM file, whichever is declared first. It is a dependency on which coordinates are passed in according to the order in which they are imported.
Conclusion: As you can see from the above figure, both spring-AOP and spring-webMVC are passed over to spring-beans, but since spring-AOP comes first, the final use of spring-beans is passed over from spring-aop. Spring-beans passed by Spring-WebMVC are ignored.
The principle of dependence regulation – closest path first
Conclusion: Direct dependency is greater than dependency pass
Eliminate dependence on
You can use the EXCLusions tag to exclude passed dependencies.
Version of the lock
The method of directly locking the version of the dependent JAR package is used to determine the version of the dependent JAR package. After the version is locked, the locked version is added to the project regardless of the declaration sequence or path of the dependency. This method is often used in enterprise development. In the dependencies TAB, specify maven coordinates to import. In the dependencies TAB, specify maven coordinates to import
② Declare the Maven coordinates to import in the Dependencies TAB
Use of the properties tag
<properties>
<spring.version>5.1.5. RELEASE</spring.version>
<springmvc.version>5.1.5. RELEASE</springmvc.version>
<mybatis.version>3.5.1 track of</mybatis.version>
</properties>
<! Lock jar version -->
<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>
Copy the code
Maven Aggregation Project (modules)
In real life, when automobile manufacturers produce automobiles, because the whole production process is very complicated and tedious, and the workload is very large, they will produce the whole automobile parts separately, and then assemble the produced parts to form a complete automobile.
Maven project analysis by module construction
In enterprise project development, due to the large scale, complex business and large number of people involved, a large project is generally divided into N small modules for development through reasonable module splitting. And split out by other modules of the module can be very easy to reuse common split means has two kinds: the first: take apart according to the business module, each module into a maven project, a project can be divided into user modules, for example, order modules, shopping cart module, each module is a maven project The second: Maven is divided by layers, such as the persistence layer, business layer, and presentation layer. Each layer corresponds to a maven project. Regardless of the above method of splitting, maven usually provides a parent project to extract some common code and configuration into the parent project for unified management and configuration.
Maven project inheritance
In the Java language, classes can inherit from one another, allowing subclasses to reference non-private properties and methods of their parent class. Similarly, maven projects can inherit from one another, and child projects can use dependencies introduced in the parent project when they inherit from the parent project. The purpose of inheritance is to eliminate duplicate code.
Aggregation of Maven projects
In the Maven project pom.xml file, you can use tags to aggregate other Maven projects together for unified operation.
For example, there are multiple Maven projects after splitting. If you want to package each project, you need to execute the package command separately, which is very complicated. These projects can then be aggregated into the Maven parent project using tags. When you need to package, you only need to execute the package command once in this project and the aggregated projects will be packaged.