2012-01-12 87 views
2

我想共享2个模块A和B之间的测试资源。在测试资源中,我有一些文件的目录。像这样在模块之间共享测试资源问题

-dir 
----f1 
----f2 

我都根据Share test resources between maven projects完成。现在从B测试中,我可以使用如下语法访问资源:

this.getClass().getClassLoader().getResource("dir/f1") 

而且它工作得很好。但我不想核心所有文件名,如f1f2。我真正想要的是从目录中获取所有文件。像

File foo = new File(this.getClass().getClassLoader().getResource("dir").getFile()); 
assert foo.isDirectory() 
foo.list() 
... 

但是当我创造这样的方式FOO它甚至不存在(foo.exist()返回false)。

我该如何处理?

更新。替代的解决方案使用Ant

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.5</version> 
    <executions> 
     <execution> 
      <configuration> 
       <target> 
        <fileset id="d" dir="${basedir}/src/test/resources/dir" includes="*"/> 
        <pathconvert property="d" refid="d"> 
         <map from="${basedir}/src/test/resources/" to=""/> 
        </pathconvert> 
        <touch file="${basedir}/src/test/resources/d/listOfCharts.txt"/> 
        <echo file="${basedir}/src/test/resources/d/listOfCharts.txt" message="${charts}"/> 
       </target> 
      </configuration> 
      <phase>package</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

回答

1

你谈论的方法创建一个可以在以后的测试中使用的JAR。您无法以这种方式列出Jar的内容。您需要使用您需要的Jar阅读它作为一个Jar专门

How do I list the files inside a JAR file?

+0

谢谢,我会看看这个方法。 – 2012-01-12 15:08:10

+0

它看起来不错。但是我决定为它使用'maven-antrun-plugin'。如果您有兴趣,请参阅更新的答案。 – 2012-01-12 16:30:58