0

我有同一个应用程序的两个衍生版本,比如版本 (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);In (A),resourceName作为“log4j.properties”,结果不是空的,具有 (1) 中的路径,而是resourceName作为“test.properties”,它为空。在 (B) 中,使用resourceName(1) 中的路径作为“log4j.properties”时它不为空,使用resourceName(2) 中的路径作为“test.properties”时它不为空。

为什么Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties");(A)中为空?起初,我认为类路径可能不同,但 (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>
4

2 回答 2

1

module 中测试类路径中的资源对 moduletest-kernel不可见test-data-warsrc/main/resources模块仅在您将它们作为依赖项添加到另一个模块时才会导出资源。

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

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

于 2015-03-27T13:48:35.310 回答
0

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

于 2015-03-27T19:00:16.137 回答