spooky655
spooky655

Reputation: 99

getResourceAsStream returns null in one case but not another, with same classpaths

I have two derivatives of the same application, say versions (A) and (B). They each contain projects: (1) test-data-war, which has target/test-classes/log4j.properties, and (2) test-kernel, which has target/test-classes/log4j.properties and target/test-classes/test.properties.

When I run a specific jUnit test in (1), it calls a method in (2) which calls Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); In (A), with resourceName as "log4j.properties", the result is not null with a path in (1), but with resourceName as "test.properties" it is null. In (B), with resourceName as "log4j.properties" it is not null with a path in (1) and with resourceName as "test.properties" it is not null with a path in (2).

Why is Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"); null in (A)? At first, I thought the classpaths might be different, but they are the same for both (1) and (2).

EDIT: Here is what the .classpath file looks like for (1):

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes"    path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
     <classpathentry kind="src" output="target/test-classes"  path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
     </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes> 
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
             <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" pat h="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

Here is what the .classpath file looks like for (2):

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
     <classpathentry kind="src" output="target/test-classes" path="src/test/java">
         <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
          </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
         </attributes> 
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
     </classpathentry>
     <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
           <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes> 
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

Upvotes: 0

Views: 498

Answers (2)

spooky655
spooky655

Reputation: 99

In (B), there was had an additional dependency in eclipse on (2) that was causing Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties") to NOT return null. It was actually supposed to return null (i.e. not find test.properties in (2)).

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328556

Resources from the test classpath in module test-kernel aren't visible for module test-data-war. Modules only exports resources under src/main/resources when you add them as a dependency to another module.

The confusing thing is that Eclipse and Maven disagree here. In Eclipse, the whole classpath of a module is visible (also test resources). But when you run the same test in Maven, test resources will suddenly vanish. This is because Eclipse doesn't have a concept of "test classpath".

If mvn dependency:tree shows a difference, then you need to check the files pom.xml for the modules.

Upvotes: 1

Related Questions