多模块Springboot项目maven单独打包子模块

背景介绍

最近接手一个项目代号XXL,是一个多模块的Springboot项目,在解决了线上的bug之后,想单独给子模块打包上线部署,问题来了。如果整个工程一起mvn -X -DskipTests clean package,打包出来的XXL-web.jar是可以正常启动的,但是单独子模块执行打包的时候启动却缺少依赖。

项目结构如下:

我需要打包的子模块就是XXL-web模块。

问题描述

XXL-web模块是后端的接口模块,供前端调用,此模块引用了usr模块、database模块等,然后web引用了这些模块,如database模块中引用了jpa的包,所以XXL-web模块就没有引用jpa相关的包和数据库连接池相关的包。

当我整个项目打包的时候,项目正常启动。当我只使用子模块用mvn -X -DskipTests clean package打包的时候,启动却缺少了jpa的依赖和数据库连接池相关的依赖。

接下来我把父pom和XXL-web模块的pom的打包插件贴出来

复制代码
<!--父pom-->
<build>
        <finalName>${project.name}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                    <configuration>
                        <includeSystemScope>true</includeSystemScope>
                        <finalName>${project.build.finalName}</finalName>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>

<!--XXL-web的pom-->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <mainClass>com.***.***.WebApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

解决方案一:

缺少的包全揉进XXL-web模块,显然这种方式很智障。

解决方案二:

进入到XXL-web目录下执行子模块打包命令

java 复制代码
mvn install -pl my-submodule(XXL-web) -am

-pl 参数用于指定要构建的子模块,可以通过指定子模块的 artifactId 或者模块路径来指定。在命令中使用 -pl my-submodule 表示只构建名为 my-submodule 的子模块。

-am 参数用于自动构建依赖的模块。如果当前构建的模块依赖于其他模块,使用 -am 参数可以让 Maven 自动构建这些依赖的模块。在命令中使用 -am 表示构建所有依赖的模块。

因此,mvn install -pl my-submodule -am 命令表示构建当前项目以及依赖的模块,并且只构建名为 my-submodule 的子模块。

总结

其实造成我走歪路的原因就是maven指令的不熟悉,在疯狂google之后才找到了这个子模块的打包命令,革命尚未成功呐。

相关推荐
过期动态4 小时前
Java开发中的@EnableWebMvc注解和WebMvcConfigurer接口
java·开发语言·spring boot·spring·tomcat·maven·idea
A懿轩A5 小时前
【Maven 构建工具】从零到上手 Maven:安装配置 + IDEA 集成 + 第一个项目(保姆级教程)
java·maven·intellij-idea
野犬寒鸦5 小时前
从零起步学习并发编程 || 第一章:初步认识进程与线程
java·服务器·后端·学习
我爱娃哈哈5 小时前
SpringBoot + Flowable + 自定义节点:可视化工作流引擎,支持请假、报销、审批全场景
java·spring boot·后端
韩师学子--小倪6 小时前
SpringBoot 优雅停服
spring boot·tomcat
李梨同学丶7 小时前
0201好虫子周刊
后端
思想在飞肢体在追7 小时前
Springboot项目配置Nacos
java·spring boot·后端·nacos
JavaGuide9 小时前
推荐一个基于 Spring Boot 4.0 + Java 21 + Spring AI 2.0 的大模型项目!
java·spring boot·spring
Loo国昌9 小时前
【垂类模型数据工程】第四阶段:高性能 Embedding 实战:从双编码器架构到 InfoNCE 损失函数详解
人工智能·后端·深度学习·自然语言处理·架构·transformer·embedding
小马爱打代码10 小时前
Spring Boot :使用 Spring Cache 注解方式集成 Redis
spring boot·redis·spring