【java】【重构二】分模块开发版本锁定以及耦合(打包)实战

目录

一、创建dependencyManagement标签

[二、 将需要版本控制的依赖版本进行标签设置](#二、 将需要版本控制的依赖版本进行标签设置)

三、将需要版本控制的依赖从各子模块迁移到此处

四、将父模块的依赖版本控制

五、删除子模块的全部版本

1、bocai-web-management模块

2、bocai-utils模块

六、打包

1、确定代码都可执行,无错误

2、bocai-parent加入标签

3、完整的bocai-parent的pom

3、打包

七、使用jar启动服务


前言:结合上篇《【java】【重构一】分模块开发设计实战》,进行版本锁定,明确dependencyManagement与dependencies区别

一、创建dependencyManagement标签

在父模块创建dependencyManagement以及dependencies

 <!--  7、 版本锁定   -->
    <dependencyManagement>
        <dependencies>

       

        </dependencies>
    </dependencyManagement>

二、 将需要版本控制的依赖版本进行标签设置

springboot项目无需控制

 <properties>
        <java.version>1.8</java.version>
        <!--  8、自定义属性  -->
        <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>
        <!-- 无需配置,因为springboot已经版本控制了  -->
        <!--
        <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>

三、将需要版本控制的依赖从各子模块迁移到此处

使用第二步设置的标签进行版本控制

    <!--  7、 版本锁定   -->
    <dependencyManagement>
        <dependencies>

            <!--        阿里云OSS依赖-->
            <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令牌-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jjwt.version}</version>
            </dependency>


            <!--   mybatis起步依赖 -->

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



            <!-- PageHelper分页插件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、 无需配置,因为在springboot当中已经版本控制了  -->

            <!--   web起步依赖
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot-starter-web.version}</version>
            </dependency>
            -->

            <!--   springboot单元测试
            <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>

四、将父模块的依赖版本控制

    <dependencies>
        <!--  6、 加入的公共依赖  -->
        <!--   lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <!--  4、注释以下依赖  -->
        <!--
        <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>

五、删除子模块的全部版本

1、bocai-web-management模块

  <!-- 3、加入 子模块依赖 -->
        <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、加入依赖 -->

        <!--   web起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--   mybatis起步依赖 -->

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

        <!--   springboot单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- PageHelper分页插件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模块

  <dependencies>

        <!-- 2、  注释 junit  -->
        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        -->

        <!-- 3、加入依赖 -->
        <!--   web起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--        阿里云OSS依赖-->
        <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令牌-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
    </dependencies>

六、打包

1、确定代码都可执行,无错误

2、bocai-parent加入<modules>标签

如果你是创建模块时选择了父Parent,是自动生成的

 <!--  8、 聚合 打包   这个是创建模块选择 父的时候自动创建的-->
    <modules>
        <module>../bocai-entity</module>
        <module>../bocai-utils</module>
        <module>../bocai-web-management</module>
    </modules>

3、完整的bocai-parent的pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- 1、修改为2.7.5 -->
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.bocai</groupId>
    <artifactId>bocai-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 2、新增设置打包模式为pom  -->
    <packaging>pom</packaging>
    <!--  8、 聚合 打包   这个是创建模块选择 父的时候自动创建的-->
    <modules>
        <module>../bocai-entity</module>
        <module>../bocai-utils</module>
        <module>../bocai-web-management</module>
    </modules>


    <!--   3、注释 name与description -->
    <!--<name>bocai-parent</name>
    <description>bocai-parent</description>-->
    <properties>
        <java.version>1.8</java.version>
        <!--  8、自定义属性  -->
        <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>
        <!-- 无需配置,因为springboot已经版本控制了  -->
        <!--
        <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、 加入的公共依赖  -->
        <!--   lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <!--  4、注释以下依赖  -->
        <!--
        <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、 版本锁定   -->
    <dependencyManagement>
        <dependencies>

            <!--        阿里云OSS依赖-->
            <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令牌-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jjwt.version}</version>
            </dependency>


            <!--   mybatis起步依赖 -->

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



            <!-- PageHelper分页插件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、 无需配置,因为在springboot当中已经版本控制了  -->

            <!--   web起步依赖
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot-starter-web.version}</version>
            </dependency>
            -->

            <!--   springboot单元测试
            <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>

    <!-- 5、注释 build   -->
    <!--
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
     -->

    </project>

3、打包

选择父bocai-parent 生命周期的package

七、使用jar启动服务

这里有个关键是bocai-web-management的pom需要build标签

<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窗口

java -jar  打包的jar包
相关推荐
雷电云手机6 小时前
重构阿塔提斯新手玩法攻略 重构阿塔提斯新手英雄推荐
重构
科研工作站7 小时前
【多时段】含sop的配电网重构【含分布式电源】【已更新视频讲解】
重构·配电网·二阶锥·分布式电源·33节点·多时段
天天进步20157 小时前
Vue项目重构实践:如何构建可维护的企业级应用
前端·vue.js·重构
H1007 小时前
重构(二)
android·重构
我自是年少韶华倾负3 天前
架构师应如何考虑重构
重构
Cooloooo10 天前
树的重构【东北大学oj数据结构7-4】C++
数据结构·c++·重构
C咖咖11 天前
第6章 第一组重构
重构
AI服务老曹13 天前
云、边、端分布式一体化计算架构,进行统一调度和统一监控的智慧物流开源了
人工智能·分布式·重构·架构·开源·音视频
第八学期14 天前
用Ansible Roles重构LNMP架构(Linux+Nginx+Mariadb+PHP)
linux·nginx·重构·架构·ansible·自动化运维
AI服务老曹18 天前
具备安全生产风险管控及评分等分析功能的名厨亮灶开源了
人工智能·安全·重构·开源·自动化·音视频