Maven - detailed explanation of aggregation module/parent-child project

1. Maven aggregation module

The aggregation module is the top level of the project directory, and other modules exist as aggregation module subdirectories. The purpose is to build multiple project modules at once.

The project structure is as follows:
Insert picture description here

Generally, the aggregated module can be understood as the parent project, and the following modules are sub-modules.
Function 1 : The parent project is a pom project, which is usually just a tool to help its sub-modules build, and has no substantial content. The specific code of each project is still written in the generated project. All sub-modules are inherited from the parent module, and the parent module is used for version control of the entire project, that is, the versions of all jar packages to be used in the project are centrally managed by the parent project. In this way, you do not need to write the version number when writing maven dependencies in other project pom files.
Usually a large project will divide the project into multiple modules. For example, as shown in the figure above, there will be no dependencies between sub-modules, but there are many things in common between them, such as many similar configurations and many similar jars. Packages and so on, at this time the advantages of the parent project and the sub-project can be reflected. We know that the pom.xml of the maven parent project and the subproject has an inheritance relationship, that is to say, the same parts of each module can be configured in the pom.xml file of the parent project, so that the pom.xml in the subproject only puts itself Personality stuff is fine, which greatly reduces the workload. In addition, other stages such as compilation and packaging can all be carried out in the parent project. Maven will automatically operate the sub-projects to improve efficiency. As shown below:
Insert picture description here

Introduce two sub-modules in the pom file of the parent module:

<modules>
<module>Model1</module>
<module>Model2</module>
</modules>

At this time, Model1 and Model2 are sub-modules of ParentProject. At this time, Model1 and Model2 will inherit the configuration of the pom.xml file in ParentProject.

2. Pom inheritance

Pom inheritance is to extract repeated configuration, usually configured in the parent module, to provide use for the sub-module, so that "one declaration, use everywhere" can be achieved.
Insert picture description here

At this time, the pom files of Model1, Model2, and Model3 are inherited from Parent.
Commonly used pom inherited elements:

groupId :项目组 ID ,项目坐标的核心元素;    
version :项目版本,项目坐标的核心元素;    
description :项目的描述信息;    
properties :自定义的 Maven 属性;    
dependencies :项目的依赖配置;    
dependencyManagement :醒目的依赖管理配置;    
repositories :项目的仓库配置;    
build :包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等;

For specific pom.xml file configuration, please refer to the following:
https://blog.csdn.net/u012152619/article/details/51485297

as follows:

Parent project pom:

<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.sohu.train</groupId>  
  <artifactId>maven-aggregate</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>pom</packaging>  
  
  <!-- 子模块 -->  
  <modules>  
    <module>../maven-01</module>  
    <module>../maven-02</module>  
    <module>../maven-03</module>  
  </modules>   
  <!-- 统一配置构件的版本号 -->  
  <properties>  
    <junit.version>3.8.1</junit.version>  
  </properties>  
    
  <!-- 依赖管理 -->  
  <dependencyManagement>  
    <dependencies>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>${junit.version}</version>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>     
  </dependencyManagement>  
</project>  

Subproject pom file:

<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>  
    <!-- 指定父pom的坐标及pom位置 -->  
    <parent>  
        <groupId>com.sohu.train</groupId>  
        <artifactId>maven-aggregate</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
        <relativePath>../maven-aggregate/pom.xml</relativePath>  
    </parent>  
    <artifactId>maven-03</artifactId>  
    <packaging>jar</packaging>  

        <!-- 添加对junit依赖,这样公用配置只需要在maven-aggregate中去配置 -->  
       因为在父工程中添加同一依赖,这一部分可以不要。 
				    <dependencies>  
				        <dependency>  
				            <groupId>junit</groupId>  
				            <artifactId>junit</artifactId>  
				        </dependency>  
				    </dependencies>  
</project>  

Guess you like

Origin blog.csdn.net/weixin_44556968/article/details/109891424