【发布时间】:2015-03-26 17:34:12
【问题描述】:
我有同一个应用程序的两个衍生版本,比如版本 (A) 和 (B)。它们每个都包含项目:
(1) test-data-war,有target/test-classes/log4j.properties,(2) test-kernel,有target/test-classes/log4j.properties 和target/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) 中的路径不为空。
为什么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>
【问题讨论】:
-
您需要仔细检查所有断言,包括这些文件在两个 JAR 和类路径中是否存在。
-
我没有看到 (A) 或 (B) 的目标 JAR 中的文件,但不同版本的“.classpath”文件是相同的。
标签: java eclipse maven junit contextclassloader