2012-05-10 71 views
3

我有一個多模塊Maven項目。一個子項目託管XSL/XML資源文件。另一個項目託管需要在單元測試中使用這些文件的Java代碼。Maven:在測試之前提取相關資源

在依賴項的jar中,資源位於文件夾xml-resources

我發現這個example並試圖改變我的需要:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.4</version> 
    <executions> 
    <execution> 
     <id>resource-dependencies</id> 
     <phase>process-test-resources</phase> 
     <goals> 
     <goal>unpack-dependencies</goal> 
     </goals> 
     <configuration> 
     <classifier>xml-resources</classifier> 
     <outputDirectory>${project.build.directory}/classes/xml-resources</outputDirectory> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

當我運行的過程中,測試資源相這並不做任何事情。我確定那裏有一些錯誤 - 我沒有看到我可以在哪裏指定應該從哪個資源中獲取資源,<classifier>似乎沒有指定資源應從哪個源複製。

我迷路了,有人能告訴我該怎麼做嗎?

回答

6

嘗試這樣的事情

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.4</version> 
    <executions> 
    <execution> 
     <id>resource-dependencies</id> 
     <phase>process-test-resources</phase> 
     <goals> 
     <goal>unpack-dependencies</goal> 
     </goals> 
     <configuration> 
     <includeArtifactIds>my-artifact-id</includeArtifactIds> 
     <includes>foobar.txt, loremipsum.xml</includes> 
     <outputDirectory>${project.build.directory}/classes/xml-resources</outputDirectory> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

看一看的unpack-dependencies parameters進行詳細的解釋或進一步的信息。

+2

這種情況下的outputDirectory文件夾應該是../test-classes/ ..而不是../ classes/.... – khmarbaise

+1

@khmarbaise有效的異議 - 甚至可能更好$ {project.build.testOutputDirectory}/xml-resources'(參見[Super POM](http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Super_POM)) – FrVaBe

+0

感謝您的支持,現在複製作品。這些文件位於'target/test-classes/xml-resources'中。但是,我無法通過getClass()。getResourceAsStream(「/ xml-resources/file.xml」)將它們加載到我的測試代碼中。還有什麼我需要做的嗎? – flyx