首页 > 解决方案 > 如何修复在springboot中损坏的favicon.ico(而子文件夹中的jpg-s不是)?

问题描述

我有一个 springboot mavenproject,其中包含文件夹中的图像资源

并且对应的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 未损坏。

知道保护 img/cart.jpg 的位置(并且可以扩展为 favicon.ico)吗?

我目前的解决方法。我有一个单独的资源文件夹“resource-bin”,仅用于 favicon.ico

使用这个 Maven 设置

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources-bin</directory>
        </resource>
    </resources>

标签: javamavenspring-bootfavicon

解决方案


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.


推荐阅读