getResourceAsStream returns 在一种情况下为 null 但在另一种情况下不是,具有相同的类路径

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

我有同一个应用程序的两个派生版本,比如版本 (A) 和 (B)。它们各自包含项目: (1) test-data-war,其中有 target/test-classes/log4j.properties,以及 (2) test-kernel,其中有 target/test-classes/log4j.propertiestarget/test-classes/test.properties.

当我在 (1) 中 运行 一个特定的 jUnit 测试时,它调用 (2) 中的一个方法,该方法调用 Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);在(A)中,resourceName为"log4j.properties",结果不为空(1)中的路径,但resourceName为"test.properties"一片空白。在 (B) 中,resourceName 作为 "log4j.properties" 它在 (1) 中的路径不为空并且 resourceName 作为 "test.properties" 它在 ( 2).

为什么 (A) 中的 Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"); 为空?起初,我认为类路径可能不同,但它们对于 (1) 和 (2) 都是相同的。

编辑: 这是 (1) 的 .classpath 文件的样子:

<?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>

这是 (2) 的 .classpath 文件的样子:

<?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>

来自模块 test-kernel 中测试类路径的资源对于模块 test-data-war 不可见。当您将模块作为依赖项添加到另一个模块时,模块仅导出 src/main/resources 下的资源。

令人困惑的是,Eclipse 和 Maven 在这里不同意。在 Eclipse 中,模块的整个类路径是可见的(也是测试资源)。但是当你运行同样在Maven中测试时,测试资源会突然消失。这是因为 Eclipse 没有 "test classpath".

的概念

如果 mvn dependency:tree 显示出差异,那么您需要检查模块的文件 pom.xml

在 (B) 中,eclipse 对 (2) 有一个额外的依赖性,导致 Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties") 不 return null。它实际上应该 return 为空(即在 (2) 中找不到 test.properties)。