1.分模块设计与开发

  • 将项目按照功能拆分成若干个子模块,方便项目的管理维护、扩展,也方便模块间的相互调用,资源共享

image-20230810235206838.png

  • 分模块开发

    • 创建maven模块,单独存放pojo实体类
    • 创建maven模块,单独存放工具类

image-20230810235212887.png

分模块开发需要先针对模块功能进行设计,再进行编码。不是先将工程开发完毕再拆分

2.继承与聚合

2.1、继承

  • 继承关系

image-20230810235220782.png

  • 概念
    • 继承描述的是两个工程间的关系,与java中的继承相似,子工程可以继承夫工程中的配置信息,常见于依赖关系的继承
  • 作用
    • 简化依赖配置、统一管理依赖
  • 实现
    • parent标签
  • 步骤

    • 创建maven模块 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.mhn</groupId>
        <artifactId>Demo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.10</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    </project>
    
    • 子工程的pom.xml文件当中,配置继承关系

    【pojo】

    <?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>
    
        <groupId>com.mhn</groupId>
        <artifactId>Demo-pojo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <groupId>org.mhn</groupId>
            <artifactId>Demo-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../Demo-parent/pom.xml</relativePath> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
        </properties>
    </project>
    

    【utils】

    <?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>
    
        <groupId>com.mhn</groupId>
        <artifactId>Demo-utils</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <groupId>org.mhn</groupId>
            <artifactId>Demo-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../Demo-parent/pom.xml</relativePath> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
        </properties>
        <dependencies>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>0.9.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.7.10</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>3.15.1</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.3.1</version>
            </dependency>
            <dependency>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
                <version>1.1.1</version>
            </dependency>
    
        </dependencies>
    </project>
    
    • 父工程中配置各个工程共有的依赖(子工程会自动继承父工程的依赖)
    <?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>
    
        <groupId>org.mhn</groupId>
        <artifactId>Demo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.10</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.26</version>
            </dependency>
        </dependencies>
    </project>
    
  • 版本锁定

    • 在maven中,可以在父工程的pom文件中,通过dependencyManagement标签来统一管理依赖版本

    【父工程】

    <dependencyManagement>
        <dependencies>
            <!--JWT令牌-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>0.9.1</version>
            </dependency> 
        </dependencies>
    </dependencyManagement>
    

    【子工程】

    <dependencies>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
    </dependencies>
    

注意:子工程引入依赖,无需执行version版本号,父工程统一管理。变更依赖版本,只需要在父工程中统一变更

  • 自定义属性/引用属性

image-20230810235237306.png

2.2、聚合

  • 聚合
    • 将多个模块组织成一个整体,同时进行项目的构建
  • 聚合工程
    • 一个不具有业务功能的“空”工程(有且仅有一个pom文件)
  • 作用

    • 快速构建项目(无需根据依赖关系手动构建,直接在聚合工程上构建即可)
  • maven中可以通过 modules 设置当前聚合工程所包含的子模块名称

【父工程(聚合工程)】

<!--聚合-->
<modules>
    <module>../tlias-pojo</module>
    <module>../tlias-utils</module>
    <module>../tlias-web-management</module>
</modules>

注意:聚合工程所包含的模块,在构建时,会自动根据模块间的依赖关系设置构建顺序,与聚合工程中模块的配置书写位置无关

【继承与聚合】

  • 作用
    • 聚合用于快速构建项目
    • 继承用于简化依赖配置、统一管理依赖
  • 相同点
    • 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
    • 聚合与继承均属于设计型模块,并无实际的模块内容
  • 不同点
    • 聚合是在聚合工程中配置关系,聚合可以知道参与聚合的模块有哪些
    • 继承是在子模块中配置关系,父模块无法知道哪些子模块继承了自己

3.私服

3.1、介绍

image-20230810235245465.png

  • 私服是一种特殊的远程仓库,它是假设在局域网内的仓库服务,用来代理位于外部的中央仓库,用于解决团队内部的资源共享与资源同步问题
  • 依赖查找顺序
    • 本地仓库 -》 私服 -》 中央仓库

注意:私服在企业项目开发中,一个项目/公司,只需要一台即可(会使用即可)

3.2、资源上传与下载

image-20230810235251900.png

  • 项目版本

    • RELEASE(发行版本):功能趋于稳定、当前更新停止,可以用于发型的版本,存储在私服中的RELEASE仓库中
    • SNAPSHOT(快照版本):功能不稳定、尚处于开发中的版本,即快照版本,存储在私服的SNAPSHOT仓库中
  • 设置私服的访问用户名/密码(setting.xml中的servers中配置)

    <server>
        <id>maven-releases</id>
        <username>admin</username>
        <password>admin</password>
    </server>
    <server>
        <id>maven-snapshots</id>
        <username>admin</username>
        <password>admin</password>
    </server>
    
  • IDEA的maven工程的pom文件配置上传(发布)地址

    <distributionManagement>
        <repository>
        <id>maven-releases</id>
        <url>http://192.168.150.101:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
        <id>maven-snapshots</id>
        <url>http://192.168.150.101:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    
  • 设置私服依赖下载的仓库组地址(setthing.xml中的mirrors、profiles中配置)

    <mirror>
        <id>maven-public</id>
        <mirrorOf>*</mirrorOf>
        <url>http://192.168.150.101:8081/repository/maven-public/</url>
    </mirror>
    
    <profile>
        <id>allow-snapshots</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <repositories>
            <repository>
                <id>maven-public</id>
                <url>http://192.168.150.101:8081/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>
    

results matching ""

    No results matching ""