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

Maven多模块项目添加application模块依赖后,index.html未被打包进module2的Jar包,求解决方案

解决Maven多模块中依赖模块静态资源未被包含的问题

别担心,我来帮你搞定这个问题——你遇到的情况是:module2依赖了application模块,但执行mvn clean install后,application里的index.html并没有出现在module2生成的Jar包中。我们一步步来排查解决:

第一步:先确认application模块是否正确打包了index.html

Maven默认只会把模块src/main/resources目录下的文件打包进Jar包(如果是Spring Boot项目,静态资源通常放在src/main/resources/static或src/main/resources/templates下)。你需要先做两个检查:

  • 确认application模块的index.html是否放在了正确的资源目录下;
  • 单独对application模块执行mvn clean package,然后解压生成的Jar包,看看里面确实包含index.html。

如果application模块的Jar里没有这个文件,那首先要调整它的目录结构,把index.html移到src/main/resources(或Spring Boot对应的静态资源目录),这样它自己的Jar才会包含这个文件,module2依赖后才有机会获取到。

第二步:配置module2的Maven插件,将依赖模块的资源合并到自身Jar

如果application模块的Jar已经包含了index.html,但module2的Jar里还是没有,这是因为Maven默认不会自动把依赖Jar中的资源解压并合并到当前模块的Jar中。这时你需要用maven-dependency-plugin来实现这个需求。

在module2的pom.xml的<build><plugins>节点中添加以下插件配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
        <execution>
            <id>unpack-application-index</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>sample.multimodule</groupId>
                        <artifactId>sample.multimodule.application</artifactId>
                        <version>1.4.0-SNAPSHOT</version>
                        <includes>**/index.html</includes>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${project.build.directory}/classes</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

配置说明:

  • 这个插件会在prepare-package阶段(打包前)执行,将application模块Jar中的index.html解压到module2的target/classes目录;
  • 因为target/classes是Maven打包时默认会包含的目录,所以最终module2的Jar就会包含这个index.html文件;
  • 如果你需要复制更多资源,可以修改<includes>的规则,比如**/*.html来包含所有HTML文件。

额外提示:Spring Boot运行时的资源访问

如果你的module2是Spring Boot应用,其实即使index.html不在module2的Jar里,只要application模块是依赖的Jar,Spring Boot在运行时也能从依赖Jar中加载静态资源。但如果你的需求是必须把index.html物理包含在module2的Jar中,那上面的插件配置就是必要的。

现在重新执行mvn clean install,然后解压module2的Jar包,应该就能看到index.html了!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 12:47:52