如何将Quarkus应用编译生成的静态资源打包至Jar的META-INF/resources?
解决方法:将静态HTML打包到Jar的META-INF/resources目录
有两种常用的Maven配置方式可以实现需求,具体如下:
方案一:通过maven-resources-plugin复制资源
在pom.xml中添加maven-resources-plugin的配置,将target/web-resources下的文件复制到编译输出目录的META-INF/resources下,Jar打包时会自动包含该目录内容:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>copy-web-resources</id> <phase>process-classes</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}/META-INF/resources</outputDirectory> <resources> <resource> <directory>${project.build.directory}/web-resources</directory> <includes> <include>**/*.html</include> <include>**/*.css</include> <include>**/*.js</include> <!-- 根据需要添加其他静态资源类型 --> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build>
phase设置为process-classes,确保在compile阶段生成静态文件后再执行复制操作outputDirectory指定资源要复制到的目标路径,对应Jar中的META-INF/resourcesincludes可以按需过滤要打包的文件类型,避免冗余
方案二:通过maven-jar-plugin直接指定打包资源
如果不想提前复制文件,也可以直接在maven-jar-plugin中配置额外的资源目录,打包时将target/web-resources的内容映射到Jar的META-INF/resources:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <includes> <include>**/*</include> </includes> <resources> <resource> <directory>${project.build.directory}/web-resources</directory> <targetPath>META-INF/resources</targetPath> </resource> </resources> </configuration> </plugin> </plugins> </build>
targetPath指定该目录下的文件在Jar中的存放路径,直接映射到META-INF/resources- 这种方式无需额外复制步骤,打包时直接读取目标目录的文件并放入指定位置
内容的提问来源于stack exchange,提问作者Tilo
相关产品推荐
相关产品推荐

