【问题标题】:filtering web.xml with maven causes font not loaded使用 maven 过滤 web.xml 导致字体未加载
【发布时间】:2015-12-22 20:03:40
【问题描述】:

我使用 JSF2 和 Maven 3 开发了一个 Web 应用程序。 在我的应用程序中,我在我的 css 中使用带有以下代码的自定义字体:

@font-face {
font-family: 'another_shabby';
src: url("#{resource['fonts:anothershabby_trial-webfont.eot']}");
src: url("#{resource['fonts:anothershabby_trial-webfont.eot']}?#iefix")
    format('embedded-opentype'),
    url("#{resource['fonts:anothershabby_trial-webfont.woff']}")
    format('woff'),
    url("#{resource['fonts:anothershabby_trial-webfont.ttf']}")
    format('truetype'),
    url("#{resource['fonts:anothershabby_trial-webfont.svg']}#WebSymbolsRegular")
    format('svg');
font-weight: normal;
font-style: normal;}

字体文件在我的resources文件夹中,目录:webapp/resources/fonts

现在,我遇到了一个问题,似乎是由 Maven 引起的......

我在 Maven 中添加了一个过滤器来解析 web.xml,以便根据 Maven 配置文件在开发和生产之间动态切换上下文参数 javax.faces.PROJECT_STAGE。

这是我的 pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <webResources>
            <resource>
                <directory>src/main/webapp</directory>
                <filtering>true</filtering>
            </resource>
        </webResources>
    </configuration>
</plugin>

<profile>
    <id>dev</id>
    <properties>
        <jsfProjectStage>Development</jsfProjectStage>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

web.xml:

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>${jsfProjectStage}</param-value>
</context-param>

当我使用此配置时,字体未正确加载,但如果我删除 pom 中的过滤器并且上下文参数是 “硬编码”,一切正常...我在 Safari (Mac + iPhone) 和 Chrome 上进行了测试。

我的配置有问题吗?我错过了什么?这是一个已知问题吗?...

【问题讨论】:

  • 过滤后你的css是什么样子的?而且您可以排除资源被“过滤”...这可能是解决方案

标签: maven jsf-2 font-face


【解决方案1】:

在做了一些研究之后,我找到了解决我问题的其他方法。我贴出来,以防万一:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
</plugin>

【讨论】:

  • 是的,但我的“声誉”分数目前还不够高,无法投票给答案……抱歉
【解决方案2】:

您正在过滤二进制文件(即 font 文件)。

查看指南如何排除/仅包含选定资源https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html

正如它所说的

为防止在启用过滤时损坏二进制文件,您可以配置一个不会被过滤的文件扩展名列表。

做

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>            
        <configuration>
          <!-- the default value is the filter list under build -->
          <!-- specifying a filter will override the filter list under build -->
          <filters>
            <filter>properties/config.prop</filter>
          </filters>
          <nonFilteredFileExtensions>
            <!-- default value contains jpg,jpeg,gif,bmp,png -->
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
          </nonFilteredFileExtensions>
          <webResources>
            <resource>
              <directory>resource2</directory>
              <!-- it's not a good idea to filter binary files -->
              <filtering>false</filtering>
            </resource>
            <resource>
              <directory>configurations</directory>
              <!-- enable filtering -->
              <filtering>true</filtering>
              <excludes>
                <exclude>**/properties</exclude>
              </excludes>
            </resource>
          </webResources>
        </configuration>
      </plugin>

根据经验,过滤尽可能少的文件(以获得最佳性能)并让 maven 复制大部分文件。当一个文件被过滤时,它必须逐个字符地处理(根据给定的字符集),而当不过滤时,它只是简单地复制到文件系统上。

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2017-01-10
    • 2015-03-17
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多