【问题标题】:exclude file from maven build从 Maven 构建中排除文件
【发布时间】:2011-03-07 16:54:34
【问题描述】:

我有一个带有文件 src/main/webapp/META-INF/context.xml 的 Web 应用程序,其中包含一些用于测试数据库的配置。在生产服务器上,此文件位于 $TOMCAT_HOME/conf/Catalina/localhost/ROOT.xml 中,我正在使用嵌入式 tomcat 进行测试,因此我不想打包此文件。我想从 Maven 构建中排除这个文件。我尝试了以下操作:

<build>
  ...
  <resources>
    <resource>
      <directory>src/main/webapp/META-INF</directory>
      <filtering>true</filtering>
      <excludes>
        <exclude>context.xml</exclude>
      </excludes>
    </resource>
  </resources>
</build>

还有以下:

<build>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <resources>
        <resource>
          <directory>src/main/webapp/META-INF</directory>
          <filtering>true</filtering>
          <excludes>
            <exclude>context.xml</exclude>
          </excludes>
        </resource>
      </resources>
    </configuration>
  </plugin>
</build>

但该文件仍被打包在 war 和构建目录中(例如 target/myapp-1.0-SNAPSHOT/META-INF/context.xml)。我做错了什么?

【问题讨论】:

    标签: maven


    【解决方案1】:

    您可以尝试使用packagingExcludes参数

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
            <packagingExcludes>META-INF/context.xml</packagingExcludes>
        </configuration>
    </plugin>
    

    要从构建中排除资源,问题中的第一个 sn-p 看起来不错,除了应该指定 resource directory 的绝对路径。比如

    <directory>${basedir}/src/main/webapp/META-INF</directory>
    

    【讨论】:

    • 像魅力一样工作。谢谢你。是否有可能将其完全排除在构建之外?我的意思也是来自构建目录(例如 target/myapp-1.0-SNAPSHOT)。如果不是,我想我可以忍受复制战争。
    • @woky。更新答案以响应附加查询
    • @Raghuram:不幸的是它不起作用。 (我的 Maven 版本:Apache Maven 3.0.2 (r1056850; 2011-01-09 01:58:10+0100))。
    • @Nishant:当然。我原本想将它完全排除在构建中。这就是为什么我还没有点击“已回答”。
    • 努力让它远离战争,但它仍然在目标目录中结束。
    【解决方案2】:

    其他人已经回答了主要问题,但我从您最初尝试的解决方案中注意到了另一个细节 -

          <filtering>true</filtering>
    

    在 Maven 中,“资源过滤”并不像您可能认为的那样。这不是包含/排除资源,而是是否应该处理它们以填充嵌入的变量引用。

    http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

    【讨论】:

      【解决方案3】:

      最近我在persistence.xml 上遇到了类似的问题。尝试将其放入您的 POM:

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <excludes>
                <exclude>META-INF/context.xml</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
      

      如果没有帮助,请尝试将 maven-jar-plugin 替换为 maven-war-plugin

      【讨论】:

        猜你喜欢
        • 2014-10-05
        • 2017-12-28
        • 2017-07-13
        • 1970-01-01
        • 2021-10-31
        • 2013-01-20
        • 2012-08-20
        • 2018-12-10
        相关资源
        最近更新 更多