2

I want to share test resources between 2 modules A and B. In A test resources I have directory with some files. Like this

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

I've done all according to Share test resources between maven projects . Now from B test I can access to resources using syntax like:

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

And it's works perfectly fine. But I don't want hardcore all file names like f1 or f2. What I really want is getting all files from directory. Like

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

But when I create foo in such way it even doesn't exist (foo.exist() returns false).

How can I deal with it?

Update. Alternative solution using 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>
Community
  • 1
  • 1
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132

1 Answers1

1

The approach you talk about creates a JAR which can be used in later tests. You can't list the contents of a Jar this way. You need to use Jar you need to read it as a Jar specially

How do I list the files inside a JAR file?

Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130