【问题标题】:maven resource filtering skipping filesmaven资源过滤跳过文件
【发布时间】:2026-01-24 18:30:01
【问题描述】:

我有一个结构如下的 Vaadin 项目:

Project-Root
  src
    main
    resources
      properties.json
      META-INF
        persistance.xml
    webapp
      WEB-INF
        classes
          hibernate-dashboard.cfg.xml
          quartz.properties

到目前为止,我是从我的 IDE (IntelliJ) 构建我的项目

mvn vaadin:compile vaadin:compile-theme -P development

然后使用“Build”->“Build Artifacts...”打包 WAR,并部署 WAR。

我的资源过滤如下:

..
<build>
 <resources>
     <resource>
         <directory>src/main/webapp/WEB-INF/classes/</directory>
         <filtering>true</filtering>
     </resource>
     <resource>
         <directory>src/main/resources/</directory>
         <filtering>true</filtering>
     </resource>
     <resource>
         <directory>src/main/resources/META-INF</directory>
         <filtering>true</filtering>
     </resource>
 </resources>
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <version>2.6</version>
     <configuration>
         <encoding>${project.encoding}</encoding>
     </configuration>
 </plugin>

问题:当我尝试用 maven 构建整个项目时

mvn clean package -P development

webapp/WEB-INF/classes 下的资源被跳过,它们在target/Project/WEB-INF/classes 目录下,但是变量是${..}(hibernate-dashboard.cfg.xml 和quartz .properties)

剩余资源(properties.json 和persistance.xml)被正确过滤。

问题:如何过滤我的所有资源并使用 maven 构建/打包我的项目?

【问题讨论】:

  • 有什么问题/疑问?
  • 您将 maven-resource-plugin 的执行绑定到的目标是什么?
  • @NicoVanBelle 我得到的唯一目标是在 vaadin-maven-plugin 下(update-widgetset 和编译,尝试添加资源)。 Edwin 我更新了问题以更清楚

标签: java maven


【解决方案1】:

这里真正的问题是你正在做一些非常奇怪的事情,并且可能应该选择改变事情以便以不同的方式做事。目录:

src/main/webapp/WEB-INF/classes

不应该存在——你不应该在那个路径上有一个类目录。如果您希望将过滤后的文件放入target/.../WEB-INF/classes,然后将这些文件放入src/main/resources,maven 会将它们复制到适当的目标目录,如果您问得好,它会过滤它们(使用来自复制时的当前上下文)。

你真正需要的是:

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

如果你认为你需要一个 classes 目录,那么你可能犯了一个错误,解决这个问题的最快方法是不再做错误的事情,而不是尝试以这种方式配置 maven。

【讨论】:

  • 感谢您的澄清。我不知道。我移动了文件,它按预期工作。
【解决方案2】:

Maven 只处理src/main/resources 中的文件,所以我添加了

<configuration>
  <webResources>
      <resource>
          <directory>src/main/webapp/WEB-INF/classes</directory>
          <targetPath>WEB-INF/classes</targetPath>
          <filtering>true</filtering>
      </resource>
  </webResources>
</configuration>

到 maven-war-plugin,现在我得到了一个正确的打包 WAR 文件。

【讨论】:

  • Maven 默认使用src/main/resources。但是您正确配置了资源插件,因此它确实拾取了这些文件;抱歉,我认为您的答案不正确,它只是偶然起作用(您将过滤从 maven-resources-plugin 移至 maven-war-plugin