java - 如何修复在springboot中损坏的favicon.ico(而子文件夹中的jpg-s不是)?
问题描述
我有一个 springboot mavenproject,其中包含文件夹中的图像资源
- src/main/resources/static/favicon.ico
- src/main/resources/static/img/cart.jpg
并且对应的maven脚本 pom.xml 包含
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
根据https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html filtering=true 替换占位符并因此破坏二进制图像。
mvn install
使用我的目标目录构建应用程序后包含这些文件
- 目标/类/静态/favicon.ico
- 目标/类/静态/img/cart.jpg
favicon.ico 已损坏(由于过滤)而 img/cart.jpg 未损坏。
知道保护 img/cart.jpg 的位置(并且可以扩展为 favicon.ico)吗?
我目前的解决方法。我有一个单独的资源文件夹“resource-bin”,仅用于 favicon.ico
- src/main/resources-bin/static/favicon.ico
使用这个 Maven 设置
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources-bin</directory>
</resource>
</resources>
解决方案
You can do so by defining two resource configurations. One with filtering enabled and exluding specific directories and/or files and the other one with filtering disabled and including the directory and/or files:
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<excludes>
<exclude>public/</exclude>
</excludes>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
<includes>
<include>public/</include>
</includes>
</resource>
</resources>
This should prevent filtering all resources inside the public directory. If you are using spring boot you may also need to configure the plugin with addResources false:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.2.RELEASE</version>
<configuration>
<addResources>false</addResources>
</configuration>
</plugin>
However, this disables the hot refreshing for resources.
推荐阅读
- php - 如何在使用 PHP 下载 CSV 时转换日文字符
- java - 在 Java 中定义 Avro 模式,如何处理时间戳或日期值?
- sql-server - SQL Server:CONCAT 与 CASE
- javascript - 无法使剪贴板 js 与 textarea 一起使用
- database - IMDB 到 SQLite:如何创建表并将 imdb 数据插入 sqlite 数据库?
- idris - 在确保终止的同时键入级别固定点
- javascript - 如何使用 Javascript 删除有序列表?
- python - 如何在 BS4 中为多个容器使用循环时从容器中提取多行?
- ruby-on-rails - 在 redis 中使用 ZINCRBY 存储哈希
- python - Pylint e1101:“reddit”类没有“subreddit”成员