1, Application scenario:
- When a project has many modules, it can help us manage the methods of construction, documentation, reporting, dependency, scms, publishing and distribution. It is convenient to compile code, manage dependencies, manage binary libraries, and so on.
- Because we have many modules, we have abstracted another layer and extracted a top mouse to manage the public dependencies of subprojects. For the correct operation of the project, all subprojects must use the unified version of dependencies, and the dependencies and versions of each applied project must be consistent to ensure that the tested and released results are the same.
- In the POM file at the top level of our project, we will see the dependency management element. The version of the jar package is managed through its element, and a dependent list version number is referenced in the subproject instead of being displayed. Maven will go up the parent-child hierarchy until it finds a project with a dependency management element, and then it will use the version number specified in the dependency management element.
2, How to judge the version number of jar package in the project
- If the dependency in the dependencies does not declare a version element, maven will go to the dependency management to find out whether the artifact ID and groupId have been declared. If they exist, they will inherit it. If not, an error will be reported. You must declare a < version > XX < / version > for the dependency.
- If the dependency in dependencies declares version, the declaration in dependency management is invalid.
Single module pom.xml:
//It only manages the version number and does not actually introduce jar s <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> //jar package identity qualification <artifactId>spring-core</artifactId> <version>3.2.7</version> //Declaration of version number </dependency> </dependencies> </dependencyManagement> //Will actually download the jar package <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> //If the version tag is not declared, it will inherit </dependency> </dependencies>
Multi module pom.xml
parent-module in pom.xml <properties> // Define all dependent version numbers in the properties tag <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <org.eclipse.persistence.jpa.version>1.2.6</org.eclipse.persistence.jpa.version> <developer.organization>xxx</developer.organization> <javaee-api.version>1.8</javaee-api.version> </properties> <dependencyManagement> //Defines the version number of the public dependency <dependencies> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> <version>${org.eclipse.persistence.jpa.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>${javaee-api.version}</version> </dependency> </dependencies> </dependencyManagement>
son-module1 Medium pom.xml <!--Inherit parent class--> <parent> <artifactId>parent-module</artifactId> //Declare the identity of the parent class <groupId>com.ppd</groupId> <version>0.0.1-SNAPSHOT</version> <relativePath>../parent-module/pom.xml</relativePath> //Declare the pom file path of the parent class </parent> <modelVersion>4.0.0</modelVersion> <artifactId>son-module</artifactId> <packaging>ejb</packaging> <!--Dependency--> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> //Inherit parent class </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> //Inherit parent class </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> //Inherit parent class <scope>provided</scope> </dependency> </dependencies>
3, The difference between dependency and dependency management
- All dependencies declared in the top-level pom dependencies will be automatically introduced and inherited by all subprojects by default.
- dependencies even if the dependency is not written in the child project, the child project will still inherit the dependency from the parent project (inherit all).
- Dependency management only declares the version number of the dependency, which will not be imported. Therefore, the subproject needs to display the dependency to be imported. If it is not declared, it will not be imported.
- If a child project declares a dependency and does not declare a version number and scope, it will inherit the version number and scope of the parent project, otherwise it will be overwritten.
reference material:
Original link: https://www.jianshu.com/p/ee15cda51d9d
Original link: https://blog.csdn.net/liutengteng130/article/details/46991829