使用 Maven 过滤 web.xml 会导致未加载字体

filtering web.xml with maven causes font not loaded

我使用 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中删除过滤器并且context-param是"hard-coded",一切工作正常...我在 Safari (Mac + iPhone) 和 Chrome.

上测试过

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

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

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

如其所说

To prevent corrupting your binary files when filtering is enabled, you can configure a list of file extensions that will not be filtered.

      <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 复制大部分文件。当一个文件被过滤时,它必须逐个字符地处理(根据给定的字符集),而当没有被过滤时,它只是简单地复制到一个文件系统上。

经过一些研究,我找到了解决我问题的其他方法。我post它,以防万一:

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