使用 Maven 从默认的 src/main/resources 位置选择某些资源文件到 WAR 中

发布于 2024-09-15 23:34:39 字数 1328 浏览 8 评论 0 原文

我试图控制哪些文件进入由 mvn package 目标创建的 WAR 包。具体来说,我想从每个包的默认 src/main/resources 文件夹中排除一些文件(我正在尝试针对不同的环境进行构建/包)。

我尝试使用 maven-war-plugin 但失败了。如果我添加此配置(用于测试):

<webResources>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>WEB-INF/classes</targetPath>
        <excludes>
            <exclude>*.xml</exclude>
        </excludes>
    </resource>
</webResources>

...我的 WEB-INF/classes 仍将包含 XML 文件。这是因为 webResources 参数似乎重复了复制过程(上述配置实际上有效,文件没有被复制......但它们在某些其他进程中被复制) 。

Maven-war-plugin 文档 状态:

所有 Maven 2 项目的默认资源目录是 src/main/resources ,它将最终位于 WAR 中的 target/classes 和 WEB-INF/classes 中。在此过程中将保留目录结构。

WAR 插件还能够通过 webResources 参数包含在默认资源目录中找不到的资源。

这有点令人困惑。这是否意味着:

  • webResources 参数是 maven-war-plugin 中的一项功能,允许src/main/resources 外部包含文件代码> 文件夹?如果是这样,我们如何从 src/main/resources 内部更改复制的文件?
  • webResources 参数是 maven-war-plugin 中的一项功能,允许包含来自 src/main/resources 文件夹之外的文件。如果是这样,如何配置才能做到这一点?
  • 还有其他选择吗?

I am trying to control which files make into the WAR package that is created by mvn package goal. Specifically, I want to exclude some files from the default src/main/resources folder for each package (I am trying to do builds/package for different environments).

I tried using maven-war-plugin but failed. If I add this configuration (for testing):

<webResources>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>WEB-INF/classes</targetPath>
        <excludes>
            <exclude>*.xml</exclude>
        </excludes>
    </resource>
</webResources>

...my WEB-INF/classes will still contain the XML files. This is because webResources parameter seems to duplicate the copying process (the above configuration actually works, an files are not copied... but they get copied in some other process instead).

Maven-war-plugin documentation states:

The default resource directory for all Maven 2 projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved in the process.

The WAR Plugin is also capable of including resources not found in the default resource directory through the webResources parameter.

This is a bit confusing. Does it mean that:

  • The webResources parameter is a feature in maven-war-plugin that allows files to be included only from outside src/main/resources folder? If so, how can we alter the copied files from inside src/main/resources?
  • The webResources parameter is a feature in maven-war-plugin that allows files to be included also from outside src/main/resources folder? If so, how can it be configured to do this?
  • Some other option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-09-22 23:34:39

我试图控制哪些文件进入由 mvn package goal 创建的 WAR 包。具体来说,我想从每个包的默认 src/main/resources 文件夹中排除一些文件

资源目录 (src/main/resources) 中的资源被复制到构建输出目录 (target/classes) 在 流程资源阶段。然后,在 package 阶段,将 target/classes 的内容打包到分布式存档中,如 WAR,并最终位于 WEB-INF 中在这种情况下,/classes

因此,如果您想控制哪些资源最终出现在 WEB-INF/classes 中,您需要控制哪些资源最终出现在 target/classes 中,即以某种方式改变绑定到进程的目标的行为-resources 阶段,特别是 Maven 资源插件

为此,(这可能不直观),您可以在 pom 的 resources 元素内声明 excludeinclude 元素,如图所示在包含和排除文件和目录中。应用于默认资源目录:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <excludes>
        <exclude>**/*.xml</exclude>
      </excludes>
    </resource>
  </resources>
</build>

如果您想针对不同环境使用自定义排除规则,请将其与 个人资料。例如:

<project>
  ...
  <profiles>
    <profile>
      <id>env-uat</id>
      <activation>
        <property>
          <name>env</name>
          <value>uat</value>
        </property>
      </activation>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <excludes>
              <exclude>**/*.xml</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
</project>

使用此配置文件时,xml 文件不会出现在 target/classes 中,因此它们不会出现在 WEB-INF/classes 中最后的战争。

我尝试使用 maven-war-plugin 但失败了。如果我添加此配置(用于测试)(...)我的 WEB-INF/classes 仍将包含 XML 文件

您在这里所做的是添加一个附加资源目录,该目录似乎是已包含的默认资源目录。因此,无论您要排除什么,这都不会产生任何影响,因为文件无论如何都会在 process-resources 期间复制到 target/classes ,因此最终仍然在 中WEB-INF/类

换句话说,当您想要添加不属于默认资源目录的额外资源时,请使用 webResources

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>extra-resources</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

但我不认为这就是您在这里寻找的内容,建议使用上面建议的方法。

I am trying to control which files make into the WAR package that is created by mvn package goal. Specifically, I want to exclude some files from the default src/main/resources folder for each package

Resources from the default resource directory (src/main/resources) are copied into the build output directory (target/classes) during the process-resources phase. Then, during the package phase, the content of target/classes is taken and packaged into a distributed archive, like a WAR, and end up in WEB-INF/classes in that case.

So, if you want to control what resources end up in WEB-INF/classes, you need to control what resources will end up in target/classes i.e to somehow alter the behavior of the goal bound to the process-resources phase, specifically the resources:resources goal of the Maven Resources Plugin.

And to do so, (this is probably not intuitive), you can declare exclude or include elements inside resources element of the pom, as shown in Including and excluding files and directories. Applied to the default resource directory:

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <excludes>
        <exclude>**/*.xml</exclude>
      </excludes>
    </resource>
  </resources>
</build>

And if you want to do use custom exclusion rules for different environments, combine this with profiles. For example:

<project>
  ...
  <profiles>
    <profile>
      <id>env-uat</id>
      <activation>
        <property>
          <name>env</name>
          <value>uat</value>
        </property>
      </activation>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <excludes>
              <exclude>**/*.xml</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
    </profile>
  </profiles>
</project>

And when using this profile, the xml files won't end up in target/classes and consequently they won't end up in WEB-INF/classes in the final war.

I tried using maven-war-plugin but failed. If I add this configuration (for testing) (...) my WEB-INF/classes will still contain the XML files

What you're doing here is adding an additional resources directory, which appears to be the already included default resource directory. So, whatever you'll exclude, this has no effect since the files get copied to target/classes during process-resources anyway and thus still end-up in WEB-INF/classes.

In other words, use webResources when you want to add extra resources that are not part of the default resource directory:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>extra-resources</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

But I don't think that this is what you're looking for here and suggest using the approach suggested above.

故乡的云 2024-09-22 23:34:39

请参阅 帕斯卡的回答正确的配置和对过程的良好解释。我在这里直接回答这个问题:

  • webResources 参数是 maven-war-plugin 中的一项功能,允许仅包含 src/main/resources 文件夹外部的文件?如果是这样,我们如何从 src/main/resources 中更改复制的文件?
  • webResources 参数是 maven-war-plugin 中的一项功能,允许从 src/main/resources 文件夹外部包含文件?如果是这样,如何配置才能做到这一点?

是前者,maven-war-plugin 中的 webResources 参数只允许包含来自 src/main/ 外部的额外资源资源。它不允许允许为src/main/resources本身配置包含/排除。

See Pascal's answer for right configuration and good explanation of the process. I just give a direct answer to this question here:

  • The webResources parameter is a feature in maven-war-plugin that allows files to be included only from outside src/main/resources folder? If so, how can we alter the copied files from inside src/main/resources?
  • The webResources parameter is a feature in maven-war-plugin that allows files to be included also from outside src/main/resources folder? If so, how can it be configured to do this?

It's the former, the webResources parameter in maven-war-plugin only allows one to include extra resources from outside src/main/resources. It does not allow one to configure includes/excludes for src/main/resources itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文