Inheritance between Maven projects

Inheritance between Maven projects

Article directory

  • Inheritance between Maven projects
    • 1. Concept
    • 2. Function
    • 3. Examples
    • 4. Operation
      • ① Create a parent project
      • ② Create a module project
      • ③ View the parent project pom.xml with new content added
      • ④ Interpret the pom.xml of the sub-project
      • ⑤ Configure dependent unified management sub-projects in the parent project
      • ⑥Refer to the dependencies managed by the parent project in the sub-project
      • ⑦ Upgrade the version of dependent information in the parent project
      • ⑧ Declare custom properties in the parent project
    • 5. Practical significance
    • 6. Finally: thanks

1. Concept

Between Maven projects, project A inherits project B

  • Project B: parent project
  • Project A: Subproject

Essentially, the configuration in pom.xml of project A inherits the configuration of pom.xml in project B.

2. Function

Uniformly manage the dependency information in the project in the parent project, specifically, manage the version of the dependency information.

Its background is:

  • A relatively large project has been split into modules.
  • Under a project, many modules are created.
  • Each module needs to configure its own dependency information.

The requirements behind it are:

  • Maintaining their own dependency information in each module is prone to discrepancy, and it is not easy to manage in a unified manner.
  • Different jar packages in the same framework should be of the same version, so the framework version used in the entire project needs to be unified.
  • The combination of jar packages (or dependent information combinations) required for using the framework requires long-term exploration and repeated debugging to finally determine a usable combination. This solution that has been summed up after a lot of energy should not be re-explored in new projects.

By maintaining the combination of dependent information for the entire project in the parent project, it not only ensures that the entire project uses a standardized and accurate jar package; it also saves time and energy by accumulating past experience.

3. Example

Rely on multiple Spring jar packages in one project

[INFO] + - org.springframework:spring-core:jar:4.0.0.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] + - org.springframework:spring-beans:jar:4.0.0.RELEASE:compile
[INFO] + - org.springframework:spring-context:jar:4.0.0.RELEASE:compile
[INFO] + - org.springframework:spring-expression:jar:4.0.0.RELEASE:compile
[INFO] + - org.springframework:spring-aop:jar:4.0.0.RELEASE:compile
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile

When using Spring, it is required that the versions of all Spring’s own jar packages must be consistent. In order to manage the versions of these jar packages in a unified way, we use the mechanism of inheritance, to manage all version information in the parent project.

Notes on inherited versioning:

  1. The package method of the inherited parent project: must be: pom to manage other projects.
  2. The dependent jar in the subproject does not specify the version, so that it will inherit the version information configuration in the parent project. If the sub-project specifies the version of the corresponding jar, it will not inherit the version information configuration of the parent project, but will be configured according to the version information of the sub-project itself.

4. Operation

①Create parent project

The creation process is the same as the previous creation of pro01-maven-java.

Project name: pro03-maven-parent

as follows:

E:\Maven_workspane\spaceVideo>mvn archetype:generate

After the project is created, it needs to be modified in the pom.xml configuration file. Its packaging method: pom can be used as the parent project to manage other projects.

<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.rainbowSea.maven</groupId>
  <artifactId>pro03-maven-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--The current project is the parent project, to manage other projects: the packaging method must be pom -->
  <packaging>pom</packaging>

  <name>pro03-maven-parent</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Only Maven projects whose packaging method is pom can manage other Maven projects. No business code is written in the Maven project whose packaging method is pom. It is a project dedicated to managing other Maven projects.

②Create a module project

The module project is similar to the module in IDEA, so you need to enter the root directory of the pro03-maven-parent project, and then run the mvn archetype:generate command to create the module project.

It is to use pr03-maven-parent as the parent project, and create its sub-projects under it:

Suppose, we create three module projects:

Here we simply create two sub-projects: respectively: pro04-maven-module, pro05-maven-module

Execute the following command to create a Mavne project

mvn archetype:generate

③ View the parent project pom.xml with new content added

When we created two sub-projects under the pro03-maven-parent parent project.

We go back to the parent project of pro03-maven-parent to see what changes have occurred in its pom.xml.

We can see that there are more additions in the pom.xml configuration file under the pro03-maven-parent parent project, as follows: Information:

The following modules and module tags are the configuration of the aggregation function

<modules>
    <module>pro04-maven-module</module>
    <module>pro05-maven-module</module>
</modules>

④ Interpret the pom.xml of the sub-project

And let’s take a look at what kind of changes exist in the configuration file information of pom.xml of pro04-maven-module and pro05-maven-module.

<!-- Use the parent tag to specify the parent project of the current project -->
<parent>
  <!-- Find the parent project by specifying the coordinates of the parent project -->
  <groupId>com.rainbowSea.maven</groupId>
  <artifactId>pro03-maven-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>

<!-- The coordinates of the subproject -->
<!-- If the groupId and version in the subproject coordinates are consistent with the parent project, then they can be omitted -->
<!-- <groupId>com.rainbowSEa.maven</groupId> -->

<artifactId>pro05-maven-module</artifactId>

<!-- <version>1.0-SNAPSHOT</version> -->

⑤ Configure dependent unified management sub-projects in the parent project

**Note: **Even if the parent project is configured with dependency management, when the sub-project needs to use dependencies, it is still necessary to specify/write which specific dependency configuration, instead of writing the dependency configuration, it is just possible There is no need to write the version number of the dependency. Otherwise, the version of the supported dependencies in the sub-project is inconsistent with the version of the parent project, and the sub-project still needs to indicate the corresponding version number that can be supported, because the version number inherited from the parent project is not supported by its own sub-project. So you need to write it yourself.

<!-- Use the dependencyManagement tag to configure dependency management -->
<!-- Managed dependencies are not actually introduced into the project
Note: Even if the parent project configures dependency management, when the sub-project needs to use dependencies, it is still necessary to specify/write which specific dependency configuration, instead of writing dependencies. -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
  </dependencies>
</dependencyManagement>

All information of pom.xml under pro03-maven-parent parent project

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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.rainbowSea.maven</groupId>
  <artifactId>pro03-maven-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--The current project is the parent project, to manage other projects: the packaging method must be pom -->
  <packaging>pom</packaging>

  <name>pro03-maven-parent</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<!-- Use the dependencyManagement tag to configure dependency management -->
<!-- Managed dependencies are not actually introduced into the project
Note: Even if the parent project configures dependency management, when the subproject needs to use dependencies,
It is still necessary to specify/write the specific configuration of which dependency, instead of not having to write the dependency. -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.0.0.RELEASE</version>
    </dependency>
  </dependencies>
 </dependencyManagement>
  
  <modules>
    <module>pro04-maven-module</module>
    <module>pro05-maven-module</module>
  </modules>
</project>

⑥The dependencies managed by the parent project are referenced in the subproject

Key point: Omit the version number. If the version number is not omitted, the version number under the parent project will not be inherited, but the version number information configured by its own sub-project will be used.

**Note: **Even if the dependency management is configured in the parent project, the sub-project still needs to configure the dependency information to be used by itself, but the version number is omitted. Others still need to be configured by the sub-project itself, the parent project Dependencies are not inherited by subprojects

The following is: the configuration of pr04-maven-module:

<!-- The version number can be removed when the child project references the dependency information in the parent project. -->
<!-- Removing the version number means that the version of this dependency in the subproject is determined by the parent project. -->
<!-- Specifically, it is determined by the <dependencyManagement> of the parent project. -->
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven .apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.rainbowSea.maven</groupId>
    <artifactId>pro03-maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.rainbowSea.maven</groupId>
  <artifactId>pro04-maven-module</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>pro04-maven-module</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

<!--Even if the dependency management is configured in the parent project, the subproject still needs to configure the dependency information to be used by itself, but it is omitted
It’s just the version number. Others need to be configured by the sub-project itself. The dependencies of the parent project will not be inherited by the sub-project -->
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
    </dependency>

  </dependencies>
</project>

The following is the configuration of pr05-maven-module:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven .apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.rainbowSea.maven</groupId>
    <artifactId>pro03-maven-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>com.rainbowSea.maven</groupId>

  <artifactId>pro05-maven-module</artifactId>

  <version>1.0-SNAPSHOT</version>

  <name>pro05-maven-module</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
    </dependency>
  </dependencies>
</project>

Run: mvn dependency:list command to view: dependency information configuration of parent project and child project.

The following is: the parent project

The following is: pro04-maven-module subproject

The following is: pro05-maven-module subproject:

⑦Upgrade the version of dependent information in the parent project

Change the version to: 4.1.4 The version is running and see:

……
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.4.RELEASE</version>
      </dependency>
...

Under the parent project:

Under the subproject:

Then run mvn dependency:list in the subproject, the effect is as follows:

[INFO] org.springframework:spring-aop:jar:4.1.4.RELEASE:compile
[INFO] org.springframework:spring-core:jar:4.1.4.RELEASE:compile
[INFO] org.springframework:spring-context:jar:4.1.4.RELEASE:compile
[INFO] org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
[INFO] org.springframework:spring-expression:jar:4.1.4.RELEASE:compile

⑧Declaring custom properties in the parent project

We can declare custom properties in the parent project: truly realize “modify in one place, take effect everywhere”.

It is to configure each version information of the parent project as a variable: Use: In the tag, configure a custom tag.

There are no special requirements for custom tags: do not use Chinese, try to be related to which jar you configure, so that you can see the version information of which jar package it depends on. As follows: custom tags, maintain Spring version data

<!-- Through custom attributes, uniformly specify the version of Spring -->
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  
  <!-- Custom tags, maintain Spring version data -->
  <rainbowSea.spring.version>4.0.0.RELEASE</rainbowSea.spring.version>
</properties>

Where the version information is used, use the form of ${} to refer to the custom attribute name:

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${rainbowSea.spring.version}</version>
      </dependency>

Truly realize “modify once, take effect everywhere”.

Run: mvn dependency:list command to view: dependency information configuration of parent project and child project.

Under the parent project:

5. Practical significance

It is not easy to write a set of dependencies that meet the requirements and develop various functions to work properly. If someone in the company has already summed up a mature combination plan, then when developing a new project, if the original accumulation is not used, but a lot of time is wasted, a lot of time will be wasted. In order to improve efficiency, we can use the engineering inheritance mechanism to preserve the mature dependency combination scheme.

As shown in the figure above, the company-level parent project manages a mature dependency combination solution, and each new project and subsystem can take what it needs.

6. Finally: Thanks

This article refers to the following blogger’s sharing. Here again, we sincerely thank the bloggers for their enthusiastic sharing of their technology.

Thanks to the following bloggers for sharing

[1]: Weapon | Code Rework