2014-06-12 56 views
16

我有以下pom.xml。當我運行mvn clean resources:testResources時,我的測試資源未被過濾。爲什麼?如何過濾maven中的測試資源?

<?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> 

回答

37

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> 
+1

另一個Maven的subtility ... – Stephan

+3

@Stephan似乎相當不錯的主意分離資源和測試資源沒有? –

+0

@MichaelLaffargue你是對的。然而,OP在他的問題中嘗試的方式更爲明顯。 – Stephan