You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven多模块Spring Boot:将core模块配置整合至其他模块可执行Jar

当然可以搞定这个问题!我来分享几种实用的方案,帮你把core模块里的通用配置整合到其他模块的可执行Jar中,顺便解释下当前问题的根源。

为什么当前构建的Jar不包含其他模块的资源?

首先得理清原因:spring-boot-maven-plugin默认只会打包当前模块src/main/resources下的资源,以及把依赖模块的完整Jar包放进可执行Jar的BOOT-INF/lib目录里,但不会自动提取依赖模块的资源到主模块的BOOT-INF/classes下。不过其实Spring Boot启动时会扫描整个classpath(包括BOOT-INF/lib里的Jar),如果你的core模块已经正确打包了配置文件,主模块依赖后其实是能加载到的——如果这不符合你的需求(比如希望配置直接出现在主模块的classes目录),可以试试下面的方案:

方案1:通过Maven资源插件复制core配置到主模块

在需要构建可执行Jar的模块(比如web模块)的pom.xml中,配置maven-resources-plugin,把core模块的通用配置复制到当前模块的资源目录,这样构建时就会被打包到可执行Jar的BOOT-INF/classes下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <executions>
                <execution>
                    <id>copy-core-properties</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/../core/src/main/resources</directory>
                                <includes>
                                    <include>application*.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注意调整../core为你的core模块相对当前模块的实际路径。

方案2:用Spring Boot的配置导入机制

如果不想复制文件,而是直接加载core模块里的配置,可以给core模块的通用配置改个名(比如application-core.properties,避免和主模块配置冲突),然后在主模块的application.properties里添加:

spring.config.import=classpath:/application-core.properties

这样Spring Boot启动时会主动加载位于core模块Jar中的这个配置文件。

方案3:让spring-boot-maven-plugin提取依赖模块资源

如果你想把core模块的所有资源(甚至class文件)都合并到主模块的可执行Jar中,可以配置spring-boot-maven-plugin提取依赖模块的内容到BOOT-INF/classes:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <includes>
                    <include>
                        <groupId>你的core模块groupId</groupId>
                        <artifactId>你的core模块artifactId</artifactId>
                    </include>
                </includes>
                <extractDependencies>true</extractDependencies>
            </configuration>
        </plugin>
    </plugins>
</build>

这个方式要注意同名文件的覆盖问题——主模块的文件会优先覆盖core模块的。

内容的提问来源于stack exchange,提问作者hotzen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:09:41