Aggregation and Dependency of Maven Projects

Posted by Illusion on Thu, 25 Jun 2020 18:31:40 +0200

Aggregation and Dependency of Projects

1 Aggregation

When a project is multi-module, how to build multiple modules at once instead of executing Maven commands separately under multiple modules.

1.1 Parent-Child Structure

<!--Parent Module netsales-poss In packaging Must be pom-->
<packaging>pom</packaging>

<!--Parent Module netsales-poss In pom.xml Introducing Submodules-->
<modules>
    <module>poss-basic</module>
    <module>poss-core</module>
    <module>poss-openapi</module>
</modules>
1.2 Parallel Structure

<!--Parent Module netsales-parent In packaging Must be pom-->
<packaging>pom</packaging>

<!--Parent Module netsales-parent In pom.xml Introducing Submodules-->
<modules>
    <module>../netsales-framework</module>
    <module>../netsales-jobs</module>
</modules>

Paths in <modelu>can be understood as modulesPom.xmlRelative Path

When Maven's build command is executed in the parent module, Maven builds each module in turn based on the dependencies between the modules

2 Inheritance

Multiple subprojects require certain dependencies, so common dependencies of the subprojects can be extracted into the parent project, which can be easily managed through inheritance.

<!--poss-openapi Use in modules parent Label specifies parent module-->
<parent>
    <groupId>com.aster.netsales.poss</groupId>
    <artifactId>netsales-poss</artifactId>
    <version>1.1.0</version>
</parent>
2.1 Dependency Management Dependency Inheritance

The parent module uses the dependency management tag to manage dependencies, and the child projects do not inherit by default and can configure inheritance.

<dependencyManagement>
    <dependencies>
        <!-- son pom Inheritance can be configured -->
        <dependency>
            <groupId>org.projectlombok</groupId>
        	<artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <!-- son pom Not Inheritable -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.6</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</dependencyManagement>

When a child module configures a parent module's dependency in dependency management, it does not require a version and inherits from the parent project if the specified version represents the child pom's own, not the parent pom's

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <!-- Parent Project's swagger Is Inheritable -->
</dependencies>
2.2 dependencies Dependent Inheritance

The parent module uses the dependencies declared by <dependencies>and the child module inherits them all by default.

Topics: Java Maven xml Lombok