创建一个多模块应用

在Spring Boot中,多模块应用是一种将应用程序分割成多个独立模块的开发方式。每个模块都是一个独立的Maven或Gradle项目,可以拥有自己的依赖、资源和配置。这种分割有助于代码的复用、维护以及团队协作。

创建父模块

新建一个空的 maven 项目,只保留 pom.xml

修改父模块 pom 文件,打包方式改为 pom,增加公共依赖,spring-boot-starter,spring-boot-starter-web

<?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.shore</groupId>
    <artifactId>UserAuth</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>18</java.version>
        <spring.boot.version>3.3.5</spring.boot.version>
        <lombok.version>1.18.34</lombok.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

执行 mvn clean install ,将父模块下载到本地仓库。

创建子模块

父模块上新建一个空的模块,如果新模块是 SpringBoot 项目的话,只保留 src 目录和 pom 文件,其他全部删掉。

修改子 pom ,parent 改为 项目父模块

<parent>
  <groupId>com.shore</groupId>
  <artifactId>UserAuth</artifactId>
  <version>1.0-SNAPSHOT</version>
  <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>

修改父模块 pom,增加子模块 module

<modules>
  <module>web</module>
</modules>

以此类推,创建多个子模块。

启动类

如果要增加一个启动类的子模块,则需要额外指定启动类位置

<build>
        <finalName>${project.parent.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                    <mainClass>com.shore.modelu_a.ModeluAApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
<!--                        <configuration>-->
<!--                            <classifiere>exec</classifiere>-->
<!--                        </configuration>-->
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

配置文件

SpringBoot 默认会读取模块 resources 目录下的 application.properties 或者 application.yaml 的配置,如果你有多个模块,并且每个模块都有自己的配置文件,确保它们在各自的资源目录中,如果你使用了特定的配置文件(如application-dev.yaml),你需要在启动应用时指定活动的配置文件,例如通过spring.profiles.active=dev

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值