【问题标题】:How do I filter test resources in maven?如何在 Maven 中过滤测试资源?
【发布时间】:2014-08-02 07:20:43
【问题描述】:

我有以下pom.xml。当我运行mvn clean resources:testResources 时,我的测试资源不是filtered(在将资源中的占位符以修改后的形式放入输出文件夹之前替换它们)。为什么?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>foo</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

【问题讨论】:

  • 这对 op 来说可能很明显,但让我们说明什么是“过滤”:它是在将资源中的占位符(以修改后的形式)放入输出文件夹之前替换它们:Filtering
  • 完成。感谢您的反馈

标签: maven maven-resources-plugin


【解决方案1】:

resourcesresource 元素用于 resources:resources 目标。 resources:testResources 使用 testResourcestestResource 元素。

正确的pom.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>foo</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>
</project>

【讨论】:

  • @Stephan 分离资源和测试资源似乎是个好主意,不是吗?
  • @MichaelLaffargue 你是对的。然而,OP 在他的问题中尝试的方式更为明显。
  • 是否可以将 ${var} 更改为 @var@ 匹配器?
猜你喜欢
  • 2011-02-20
  • 2016-08-16
  • 1970-01-01
  • 2011-04-13
  • 2011-02-28
  • 2013-04-11
  • 2016-04-17
  • 1970-01-01
  • 2011-09-10
相关资源
最近更新 更多