单体项目修改成多module项目时遇到的问题

95 阅读3分钟

记录一下将单体项目修改成多module项目时遇到的问题

起因:

​ 因为做项目之前没有说要把其他项目也并入进来,所以就当springboot小单体去做了,结果后面说要把其他功能合并进来当作一整个大项目的统一平台。但是后面打包的时候,部分环境下又需要分开部署,这时候单体项目就无法满足需求了(其实也可以丢两份一样的,但是实在不够优雅= =)

这是原先的目录树,就是一个小单体:

image-20241227135408820.png

这是修改之后的目录树:

image-20241227135453398.png

后续并过来的模块正常添加就行,十分舒服。

注意事项:

1.父类pom要设置为<packaging>pom</packaging>如果不显式指定的话,

默认是<packaging>jar</packaging>

2父类pom.<dependencies>标签下的所有依赖都会传递到其子依赖,只用放一些通用的依赖即可:

image-20241227140109797.png

<dependencyManagement>下的的依赖则需要子pom引入之后才可以正常使用,只是统一在父pom下统一管理版本号,子项目按需要引入

3.配置<profiles>标签

在项目的启动类模块中配置,可以根据环境动态切换yml配置,也可以动态根据需要的模块勾选后打包

3.1动态修改application-yml配置

首先在启动类模块的pom下添加标签

 <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

配置完之后再修改application.yml中的配置

spring:
  profiles:
    active: @spring.profiles.active@

用占位符的形式在打包时动态读取配置,这时候右侧maven栏下会出现选项:

image-20241227140814684.png

根据你的环境,有多少可以配置多少,打包时根据环境勾选对应的配置,以后就不用在yml中每次去修改配置了

3.2根据配置动态选择打包时的模块

一样也是使用<profiles>标签配置

首先,在父pom中配置出打包时的情况,比如all-module,和实际只会用一个的module

<modules>
    <module>yjs-application</module>
    <module>yjs-common</module>
    <module>yjs-config</module>

</modules>

<profiles>
    <profile>
        <id>all-module</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
            <module>yjs-common</module>
            <module>yjs-config</module>
            <module>yjs-sftj</module>

        </modules>
    </profile>

    <profile>
        <id>yjs-sftj</id>
        <modules>
            <module>yjs-sftj</module>
        </modules>
    </profile>

    <profile>
        <id>yjs-dzzz</id>
        <modules>
            <module>yjs-dzzz</module>
        </modules>
    </profile>

</profiles>

我的all-module和modules中都没有配置新添加的这个dzzz模块,所以这时候目录树长这样:

image-20241227141526261.png

可以看到是没有被识别的,但是这时候右侧的maven工具中是有勾选栏的,如果勾选上:

image-20241227141626212.png

image-20241227141634382.png

这时候dzzz模块是被maven所识别的。

到这一步,还没有到动态打包的效果,因为这里只是maven识别,视觉效果看起来没问题,但是打包时这个模块是没有被打进去的。

还需要在父类的<dependencyManagement>中添加这个依赖,然后在启动类的module中去配置maven配合使用

父类pom:

<dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>com.fjdci</groupId>
            <artifactId>yjs-dzzz</artifactId>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<!--最下方的build-->
    <build>
        <plugins>
            <!-- 打包跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

启动类pom:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
    </profile>

    <profile>
        <id>all-module</id>
        <dependencies>
            <dependency>
                <groupId>com.fjdci</groupId>
                <artifactId>yjs-common</artifactId>
            </dependency>

            <dependency>
                <groupId>com.fjdci</groupId>
                <artifactId>yjs-config</artifactId>
            </dependency>

            <dependency>
                <groupId>com.fjdci</groupId>
                <artifactId>yjs-sftj</artifactId>
            </dependency>

        </dependencies>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <profile>
        <id>yjs-dzzz</id>
        <dependencies>
            <dependency>
                <groupId>com.fjdci</groupId>
                <artifactId>yjs-dzzz</artifactId>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <id>yjs-sftj</id>
        <dependencies>
            <dependency>
                <groupId>com.fjdci</groupId>
                <artifactId>yjs-sftj</artifactId>
            </dependency>
        </dependencies>
    </profile>

</profiles>


<!--最下方的build-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--                    <includeSystemScope>true</includeSystemScope>-->
                    <fork>false</fork>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

打包后的效果:

image-20241227142520005.png

从目录结构可以看到,这时候打包是正确打进去的。

到这里基本就结束了,但是在我弄完之后碰到一个问题

不管怎么配置profile,某个模块始终在项目中并且一直能被识别

image-20241227142656275.png

image-20241227142707706.png

明明没有勾选,all-module也没有包含,但是模块依旧是被引用的。

直到我注意到maven的依赖位置有点不对:

image-20241227142815007.png

你小子怎么是单挂在外面的,跟引入其他项目时一样

image-20241227142906093.png

解决方式就是直接右键maven减掉就好了,因为这里添加进去了一直被idea识别到= =,profiles的配置是没问题的

另外基础的一些模块,比如base,common什么的,可以直接在启动类的pom中配置

完整的启动类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>
    <parent>
        <groupId>com.fjdci</groupId>
        <artifactId>fjdci-bdcyjs-server</artifactId>
        <version>0.0.2-SNAPSHOT</version>
    </parent>


    <packaging>jar</packaging>

    <artifactId>yjs-application</artifactId>
    <description>不动产一件事启动模块</description>


    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>

        <profile>
            <id>all-module</id>
            <dependencies>
                <dependency>
                    <groupId>com.fjdci</groupId>
                    <artifactId>yjs-common</artifactId>
                </dependency>

                <dependency>
                    <groupId>com.fjdci</groupId>
                    <artifactId>yjs-config</artifactId>
                </dependency>

                <dependency>
                    <groupId>com.fjdci</groupId>
                    <artifactId>yjs-sftj</artifactId>
                </dependency>

            </dependencies>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>yjs-dzzz</id>
            <dependencies>
                <dependency>
                    <groupId>com.fjdci</groupId>
                    <artifactId>yjs-dzzz</artifactId>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>yjs-sftj</id>
            <dependencies>
                <dependency>
                    <groupId>com.fjdci</groupId>
                    <artifactId>yjs-sftj</artifactId>
                </dependency>
            </dependencies>
        </profile>

    </profiles>


    <dependencies>

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

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fjdci</groupId>
            <artifactId>yjs-config</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fjdci</groupId>
            <artifactId>yjs-common</artifactId>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--                    <includeSystemScope>true</includeSystemScope>-->
                    <fork>false</fork>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>