从maven构建中排除文件

时间:2011-03-07 16:54:35

标签: maven

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

<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和build目录中打包(例如target / myapp-1.0-SNAPSHOT / META-INF / context.xml)。我做错了什么?

3 个答案:

答案 0 :(得分:18)

您可以尝试使用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>

要从构建中排除资源,问题中的第一个代码段看起来很好,但应指定resource directory的绝对路径。例如

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

答案 1 :(得分:3)

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

      <filtering>true</filtering>

在Maven,&#34;资源过滤&#34;并不代表你认为它意味着什么。它不是要包含/排除资源,而是应该处理它们以填充嵌入式变量引用。

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

答案 2 :(得分:0)

最近我遇到了与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-war-plugin 替换 maven-jar-plugin

相关问题