我有两个相同应用的衍生物,比如版本(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”,结果不为null,路径为(1),但resourceName为“test.properties”,则为null。 在(B)中, resourceName为“log4j.properties”,它不是null,路径在(1)中,而resourceName是“test.properties”,它不是null,路径在(2)中。
为什么是Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"); 在(A)中为null? 起初,我认为类路径可能不同,但它们对于(1)和(2)都是相同的。
编辑:这是.classpath文件的样子(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>这是(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>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>最满意答案
模块test-kernel中的测试类路径的资源对于模块test-data-war是不可见的。 当您将它们作为依赖项添加到另一个模块时,模块仅导出src/main/resources resources下的src/main/resources 。
令人困惑的是Eclipse和Maven在这里不同意。 在Eclipse中,模块的整个类路径是可见的(也是测试资源)。 但是当你在Maven中运行相同的测试时,测试资源会突然消失。 这是因为Eclipse没有“test classpath”的概念。
如果mvn dependency:tree显示差异,那么你需要检查模块的文件pom.xml 。
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)).
更多推荐
发布评论