Maven’s father-son project & version control & case practice, and expand the meaning of groupId and artifactId

Article directory

    • **`` tag**
    • **`` tag**
    • **`` & amp; `` difference**
    • **`` tag**
    • **`` tag**
    • **Version Control Priority**
    • Practical case:
      • Notice:
    • expand:

tag

Used for father-son engineering projects. What is father-son engineering?

As the name suggests, a maven parent-child project is a maven project with a parent project and many sub-projects under the parent project. Of course, sub-projects can also be added under the sub-projects to form a tree structure. So, what are the benefits of doing this? There are two points:

  • transitive dependency
  • Easy to pack

The parent project package needs to specify pom
Subprojects need to declare parent, otherwise they cannot use the dependencies of the parent pom.

tag

Import dependencies
Appears together with dependencyManagement. The specified version has higher priority and usually appears in the parent pom.

& amp; Difference

Usually parent is used for structure management, declaring that the sub-project inherits the properties of the parent project.
A depends on B. C inherits from B.

A = B’s dependency + B custom class
C = dependency of B
image

tag

Version control, only declares the version and does not import dependencies

tag

Execute the sub-project. If not specified, the sub-project cannot be identified.

Version control priority

The version of the sub-project has higher priority
subproject dependency
Subproject dependencyManagement
Parent project dependency
Parent project dependencyManagement

If there are the same pom and multiple different versions of the same dependency, the bottom one has the highest priority.

Practical case:

For example, we have the following project directory

The outermost pom.xml is the parent project and is generally used to control the sub-project version.

Parent pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
\t
    <groupId>cn.wolfcode</groupId>
    <artifactId>caro2o</artifactId>
    <version>3.8.3</version>

    <name>caro2o</name>
    <url>http://www.ruoyi.vip</url>
    <description>eDianbang o2o platform management system</description>
    
    <properties>
        <caro2o.version>3.8.3</caro2o.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <druid.version>1.2.11</druid.version>
        <bitwalker.version>1.21</bitwalker.version>
        <swagger.version>3.0.0</swagger.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <mybatis-spring-boot.version>2.2.2</mybatis-spring-boot.version>
        <pagehelper.boot.version>1.4.1</pagehelper.boot.version>
        <fastjson.version>2.0.8</fastjson.version>
        <oshi.version>6.1.6</oshi.version>
        <commons.io.version>2.11.0</commons.io.version>
        <commons.fileupload.version>1.4</commons.fileupload.version>
        <commons.collections.version>3.2.2</commons.collections.version>
        <poi.version>4.1.2</poi.version>
        <velocity.version>2.3</velocity.version>
        <jwt.version>0.9.1</jwt.version>
    </properties>
\t
    <!-- Dependency declaration -->
    <dependencyManagement>
        <dependencies>

            <!-- SpringBoot dependency configuration -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.14</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Alibaba database connection pool -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!-- Analyze client operating system, browser, etc. -->
            <dependency>
                <groupId>eu.bitwalker</groupId>
                <artifactId>UserAgentUtils</artifactId>
                <version>${bitwalker.version}</version>
            </dependency>

            <!-- SpringBoot integrates the mybatis framework -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot.version}</version>
            </dependency>

            <!-- pagehelper paging plug-in -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>

            <!-- Get system information -->
            <dependency>
                <groupId>com.github.oshi</groupId>
                <artifactId>oshi-core</artifactId>
                <version>${oshi.version}</version>
            </dependency>

            <!-- Swagger3 dependency -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <!-- Commonly used io tools -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>

            <!-- File upload tool class -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons.fileupload.version}</version>
            </dependency>

            <!-- excel tool -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!-- Velocity code generation uses templates -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>${velocity.version}</version>
            </dependency>

            <!-- collections tool class -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons.collections.version}</version>
            </dependency>

            <!-- Alibaba JSON parser -->
            <dependency>
                <groupId>com.alibaba.fastjson2</groupId>
                <artifactId>fastjson2</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- Token generation and analysis-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>

            <!-- Verification code -->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- Scheduled tasks -->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-quartz</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- Code generation -->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-generator</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- Core module -->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-framework</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- System module-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-system</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- General Tools-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-common</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!--Business module-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-business</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <modules>
        <module>caro2o-admin</module>
        <module>caro2o-framework</module>
        <module>caro2o-system</module>
        <module>caro2o-quartz</module>
        <module>caro2o-generator</module>
        <module>caro2o-common</module>
        <module>caro2o-business</module>
    </modules>
    <packaging>pom</packaging>


    <dependencies>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

When subprojects are iterated, they are generally iterated with the same version, so use 3.8.3 to uniformly control versions.

The dependencies written in the tag are dependencies that subprojects can choose to load. If a subproject wants to load the dependency, just add it in the dependencies in its own pom.xml. No need to add it again. Write the version number, because it is all managed by the parent project, and this avoids the problem of conflicts caused by different versions when different sub-projects reference the same dependency.

The dependencies written in the tag are dependencies that must be loaded by sub-projects, which are omitted below.

The pom.xml of the service startup entry sub-project caro2o-admin:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>caro2o</artifactId>
        <groupId>cn.wolfcode</groupId>
        <version>3.8.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>caro2o-admin</artifactId>

    <description>
        web service entrance
    </description>

    <dependencies>

        <!-- spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- Indicates that dependencies will not be passed -->
        </dependency>

        <!-- swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
        </dependency>

        <!-- Prevent type conversion errors from being reported when entering the swagger page, exclude references in 3.0.0, and manually add version 1.6.2 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.2</version>
        </dependency>

         <!-- Mysql driver package -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- Core module -->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-framework</artifactId>
        </dependency>

        <!-- Scheduled tasks -->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-quartz</artifactId>
        </dependency>

        <!-- Code generation -->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-generator</artifactId>
        </dependency>
        <!--Business module-->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-business</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.1.RELEASE</version>
                <configuration>
                    <fork>true</fork> <!-- Without this configuration, devtools will not take effect -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>${project.artifactId}</warName>
                </configuration>
           </plugin>
        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>

</project>

Note:

For example, if I want to add a new business sub-project named caro2o-business to a demo project built using the Royi framework, I need to add the business sub-project as a module dependency to the service startup entry sub-project. pom.xml of caro2o-admin, otherwise after the service is started, admin is not loaded into the code in the business module, and a series of errors such as 404 or 500 will appear.

Then add the dependencies of different modules such as tool subprojects in the pom.xml of the business subproject, such as:


Because the parent project already controls the version number, there is no need to write version here. In the situation in the figure, if the entire project is iterated as a whole and the unified version number is upgraded and changed in the parent project’s pom.xml, an error may be reported, because Maven first takes the dependencies in the subproject’s own pom.xml The version number will cause conflicts with other sub-project versions when referencing caro2o-system.

Expansion:

What do the groupId and artifactId of the maven project mean?

First of all, you need to know that groupId and artifactId exist to locate your project, so they act as coordinates.
groupId: group means group, so it is understood to be the organization ID and the company ID. It is usually written similarly to the company domain name. Generally divided into three sections, namely “domain.company name.subproject”, the domain refers to org, com, cn
Etc., similar to what we have learned in computer networks, cn refers to china, com business, org non-profit organization.
artifactId: a specific project, the naming method is decided by yourself.

Take the project structure of a large-scale shopping mall in a certain horse as an example. This project adopts a microservice architecture, so it uses an aggregation method, that is, several small projects under one large project (this is to facilitate version management):


For example:
com.leyou.parent< /groupId>, where com is the business identifier, leyou is the company name of this Leyou Mall project, parent is the project name, but because this project uses aggregation, there are many projects below, This is the top-level container, so it is named parent. leyou represents the overall name of this project is leyou.
The subproject below it is named com.leyou.user. The first two fields and company names are the same, but the specific subproject name is placed in the project.
The leyou-user used in leyou-user< /artifactId> indicates that the project is a user sub-project under the leyou project. This way the structure is clear and easy to understand.
After adopting this naming, the internal structure of the project usually corresponds.
For example:

This project is a separate service for each project under a large project. For those services that may be called by other services, it can be split into two. For example, the sub-project leyou-item here is not called by other services, so it is not called by other services. It is divided into two sub-projects: interface and service. The interface provides external service calls, and the service performs specific business operations.