maven test cannot load cross-module resources/properties ?

Posted by smallufo on Stack Overflow See other posts from Stack Overflow or by smallufo
Published on 2010-06-05T17:15:03Z Indexed on 2010/06/05 18:32 UTC
Read the original article Hit count: 247

Filed under:
|
|
|

I have a maven mantained project with some modules . One module contains one XML file and one parsing class.

Second module depends on the first module. There is a class that calls the parsing class in the first module , but maven seems cannot test the class in the second module. Maven test reports :

java.lang.NullPointerException
 at java.util.Properties.loadFromXML(Properties.java:851)
 at foo.firstModule.Parser.<init>(Parser.java:92)
 at foo.secondModule.Program.<init>(Program.java:84)

In "Parser.java" (in the first module) , it uses Properties and InputStream to read/parse an XML file :

InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
Properties properties = new Properties();
properties.loadFromXML(xmlStream);

The "data.xml" is located in first module's resources/foo/firstModule directory , and it tests OK in the first module.

It seems when testing the second module , maven cannot correctly load the Data.xml in the first module .

I thought I can solve the problem by using maven-dependency-plugin:unpack to solve it . In the second module's POM file , I add these snippets :

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-dependency-plugin</artifactId>
 <version>2.1</version>
  <executions>
    <execution>
      <id>data-copying</id>
      <phase>test-compile</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>foo</groupId>
            <artifactId>firstModule</artifactId>
            <type>jar</type>
            <includes>foo/firstModule/Data.xml</includes>
            <outputDirectory>${project.build.directory}/classes</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>        
  </executions>
</plugin>

In this POM file , I try to unpack the first module , and copy the Data.xml to classes/foo/firstModule/ directory , and then run tests.

And indeed , it is copied to the right directory , I can find the "Data.xml" file in "target/classes/foo/firstModule" directory. But maven test still complains it cannot read the File (Properties.loadFromXML() throws NPE).

I don't know how to solve this problem. I tried other output directory , such as ${project.build.directory}/resources , and ${project.build.directory}/test-classes , but all in vain...

Any advices now ? Thanks in advanced.

Environments : Maven 2.2.1 , eclipse , m2eclipse

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about testing