【问题标题】:Why is maven-war-plugin ignoring my configured filters for web resources?为什么 maven-war-plugin 会忽略我为 Web 资源配置的过滤器?
【发布时间】:2018-01-09 21:49:22
【问题描述】:

我在src/main/filters/base.properties 中有一个过滤器文件,内容如下:

testProperty=testValue

我的 POM 定义了资源、过滤和其他 Web 资源(使用 maven-war-plugin)。它还覆盖了默认的过滤分隔符。

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <filters>
        <filter>src/main/filters/base.properties</filter>
    </filters>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <overwrite>true</overwrite>
                <useDefaultDelimiters>false</useDefaultDelimiters>
                <delimiters>
                    <delimiter>@</delimiter>
                </delimiters>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/more-web-resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

所有过滤都适用于位于src/main/resources 的文件。但是,位于src/main/more-web-resources 的配置文件没有被正确过滤。因此,如果我有一个文件src/main/more-web-resources/test.properties,其内容为:

finalTestProperty=@testProperty@

最终的src/main/webapp/test.properties 文件如下所示:

finalTestProperty=@testProperty@

相对于:

finalTestProperty=testValue

因此,过滤对使用 maven-war-plugin 指定的其他 Web 资源不起作用。我错过了什么?

【问题讨论】:

  • more-web-resources 的内容可能应该在src/main/resources 之下。
  • 不是,它就是这样设计的。拥有另一个 Web 资源目录允许我直接在 Web 应用程序文件夹下使用更多文件来进行不同的构建配置。将它们放在src/main/resources 下意味着它们最终会在WEB-INF/classes 下,这是我不想要的。我希望他们直接在 WAR 中。
  • 过滤也在war插件的配置部分中配置。如果我没记错的话,pom 中的另一部分只是资源插件。插件文档中有一个很好的例子:maven.apache.org/plugins/maven-war-plugin/examples/…
  • 尝试再定义另一个资源目录:&lt;resource&gt;&lt;directory&gt;src/main/more-web-resources&lt;/directory&gt;&lt;/resource&gt;
  • 我真的必须在war插件配置中复制过滤器部分吗?我还使用 maven-properties-plugin 来拉入额外的过滤器文件,所以我看不出在插件内重新指定过滤器的情况下如何工作。

标签: java maven maven-war-plugin maven-resources-plugin


【解决方案1】:

好吧,经过一夜的睡眠,我今天早上回到了它,已构建,并且正在正确过滤属性。我确保我的 POM 和项目结构与我的问题完全匹配,并且确实如此。我可能正在查看原始的 test.properties 而不是构建的 test.properties

为了清楚起见,为了解决问题中的 cmets,我不必在 &lt;resources/&gt; 部分中为 more-web-resources 目录定义额外的资源,我不必(重新)指定过滤器在 maven-war-plugin 配置内的另一个 &lt;filters/&gt; 部分中,我不必(重新)指定 maven-war-plugin 内的 @ 分隔符。通过使用 POM 的标准部分中定义的资源和过滤器以及 maven-resources-plugin,这一切都按我最初的预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多