[java] [Refactoring 2] Practical combat of module development version locking and coupling (packaging)

Table of Contents

1. Create dependencyManagement tag

2. Set labels for dependent versions that require version control

3. Migrate dependencies that require version control from each submodule to here

4. Version control the dependencies of the parent module

5. Delete all versions of submodules

1. bocai-web-management module

2. bocai-utils module

6. Packing

1. Make sure the code is executable and there are no errors.

2. Add bocai-parent label

3. Complete bocai-parent pom

3. Packing

7. Use jar to start the service


Preface: Combined with the previous article “[java] [Refactoring 1] Practical Module Development and Design”, version locking is carried out to clarify the difference between dependencyManagement and dependencies.

1. Create dependencyManagement tag

Create dependencyManagement and dependencies in the parent module

 <!-- 7. Version locking -->
    <dependencyManagement>
        <dependencies>

       

        </dependencies>
    </dependencyManagement>

2. Set labels for dependent versions that require version control

Springboot project does not require control

 <properties>
        <java.version>1.8</java.version>
        <!-- 8. Custom attributes -->
        <lombok.version>1.18.24</lombok.version>
        <aliyun-sdk-oss.version>3.15.1</aliyun-sdk-oss.version>
        <jaxb-api.version>2.3.1</jaxb-api.version>
        <activation.version>1.1.1</activation.version>
        <jaxb-runtime.version>2.3.3</jaxb-runtime.version>
        <mybatis.version>2.3.1</mybatis.version>
        <mysql-connector-j.version>8.0.31</mysql-connector-j.version>
        <pagehelper.version>1.4.6</pagehelper.version>
        <fastjson.version>1.2.76</fastjson.version>
        <jjwt.version>0.9.0</jjwt.version>
        <!-- No configuration is required because springboot is already version controlled -->
        <!--
        <spring-boot-starter-test.version>2.7.5</spring-boot-starter-test.version>
        <spring-boot-starter-aop.version>2.7.5</spring-boot-starter-aop.version>
        <spring-boot-starter-web.version>2.7.5</spring-boot-starter-web.version>
        -->
    </properties>

3. Migrate dependencies that require version control from each sub-module to here

Use the tags set in the second step for version control

 <!-- 7. Version locking -->
    <dependencyManagement>
        <dependencies>

            <!-- Alibaba Cloud OSS dependencies -->
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>${aliyun-sdk-oss.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>${jaxb-api.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
                <version>${activation.version}</version>
            </dependency>
            <!-- no more than 2.3.3-->
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-runtime</artifactId>
                <version>${jaxb-runtime.version}</version>
            </dependency>


            <!--JWT token-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jjwt.version}</version>
            </dependency>


            <!-- mybatis starting dependency -->

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- mysql driver -->
            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <scope>runtime</scope>
                <version>${mysql-connector-j.version}</version>
            </dependency>



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


            <!--fastJSON-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- 100. No configuration is required because it is already version controlled in springboot -->

            <!-- web start-up dependencies
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot-starter-web.version}</version>
            </dependency>
            -->

            <!-- springboot unit test
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <version>${spring-boot-starter-test.version}</version>
            </dependency>
            -->
            <!--AOP
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
                <version>${spring-boot-starter-aop.version}</version>
            </dependency>
            -->

        </dependencies>
    </dependencyManagement>

4. Version control the dependencies of the parent module

 <dependencies>
        <!-- 6. Added public dependencies -->
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <!-- 4. Comment the following dependencies -->
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        -->
    </dependencies>

5. Delete all versions of submodules

1. bocai-web-management module

 <!-- 3. Add submodule dependencies -->
        <dependency>
            <groupId>com.bocai</groupId>
            <artifactId>bocai-entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.bocai</groupId>
            <artifactId>bocai-utils</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- 4. Add dependencies -->

        <!-- web startup dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- mybatis starting dependency -->

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- mysql driver -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- springboot unit test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- PageHelper paging plug-in https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>


        <!--fastJSON-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <!--AOP-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

    </dependencies>

2. bocai-utils module

 <dependencies>

        <!-- 2. Comment junit -->
        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        -->

        <!-- 3. Add dependencies -->
        <!-- web startup dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Alibaba Cloud OSS dependencies -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>

        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
        </dependency>
        <!-- no more than 2.3.3-->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>



        <!--JWT token-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
    </dependencies>

Six. Packing

1. Make sure the codes can be executed without errors

slightly

3. Complete bocai-parent pom



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        
        2.7.5
         
    

    com.bocai
    bocai-parent
    0.0.1-SNAPSHOT
    
    pom
    <!-- 8. Aggregation and packaging This is automatically created when creating a module and selecting the parent -->
    <modules>
        <module>../bocai-entity</module>
        <module>../bocai-utils</module>
        <module>../bocai-web-management</module>
    </modules>


    
    
    <properties>
        <java.version>1.8</java.version>
        <!-- 8. Custom attributes -->
        <lombok.version>1.18.24</lombok.version>
        <aliyun-sdk-oss.version>3.15.1</aliyun-sdk-oss.version>
        <jaxb-api.version>2.3.1</jaxb-api.version>
        <activation.version>1.1.1</activation.version>
        <jaxb-runtime.version>2.3.3</jaxb-runtime.version>
        <mybatis.version>2.3.1</mybatis.version>
        <mysql-connector-j.version>8.0.31</mysql-connector-j.version>
        <pagehelper.version>1.4.6</pagehelper.version>
        <fastjson.version>1.2.76</fastjson.version>
        <jjwt.version>0.9.0</jjwt.version>
        <!-- No configuration is required because springboot is already version controlled -->
        <!--
        <spring-boot-starter-test.version>2.7.5</spring-boot-starter-test.version>
        <spring-boot-starter-aop.version>2.7.5</spring-boot-starter-aop.version>
        <spring-boot-starter-web.version>2.7.5</spring-boot-starter-web.version>
        -->
    </properties>
    <dependencies>
        <!-- 6. Added public dependencies -->
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <!-- 4. Comment the following dependencies -->
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        -->
    </dependencies>

    <!-- 7. Version locking -->
    <dependencyManagement>
        <dependencies>

            <!-- Alibaba Cloud OSS dependencies -->
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>${aliyun-sdk-oss.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>${jaxb-api.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
                <version>${activation.version}</version>
            </dependency>
            <!-- no more than 2.3.3-->
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-runtime</artifactId>
                <version>${jaxb-runtime.version}</version>
            </dependency>


            <!--JWT token-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jjwt.version}</version>
            </dependency>


            <!-- mybatis starting dependency -->

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- mysql driver -->
            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <scope>runtime</scope>
                <version>${mysql-connector-j.version}</version>
            </dependency>



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


            <!--fastJSON-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- 100. No configuration is required because it is already version controlled in springboot -->

            <!-- web start-up dependencies
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot-starter-web.version}</version>
            </dependency>
            -->

            <!-- springboot unit test
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <version>${spring-boot-starter-test.version}</version>
            </dependency>
            -->
            <!--AOP
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
                <version>${spring-boot-starter-aop.version}</version>
            </dependency>
            -->

        </dependencies>
    </dependencyManagement>

    
    

    

3. Packaging

Select the package of the parent bocai-parent life cycle

Seven. Use jar to start the service

The key here is that the pom of bocai-web-management requires the build tag

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

#cmd window

java -jar packaged jar package