2013-04-25 80 views
0

对我的同事工作的多模块项目不适合我。在引用文件的任何测试期间以及在引用文件时,弹簧配置文件中都会出现问题。引用永远不会包含子模块文件夹名称,或者完整的文件路径不能正确解析。子模块内使用的文件路径不正确

例如ParentProject是父项,ChildProject是子模块。

ParentProject 
    - ChildProject 
     - src/test/java/my/package/MyTest.java 
     - src/test/resources 
      - xml/myfile.xml 
      - myProperties.properties 
      - mySpringConfig.xml 

如果我有MyTest.java如下:

File file = new File("src/test/resources/xml/myfile.xml"); 
String absolutePath = file.getAbsolutePath(); 

然后absolutePathC:\ParentProject\src\test\resources\xml\myfile.xml因为它缺少ChildProject文件夹,所以它抛出一个FileNotFoundException这是不正确。

在spring配置文件中提及任何属性文件时都会遇到同样的问题,例如在mySpringConfig.xml我也行:

<context:property-placeholder location="classpath:myProperties.properties" system-properties-mode="OVERRIDE"/> 

但是在试图建立的项目,我得到:

IllegalStateException: Failed to load ApplicationContext 
Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: src\test\resources\myProperties.properties (The system cannot find the path specified) 

我感觉这是一个classpath的问题。我有maven 3.0.3安装和使用安装的java版本(验证与mvn -version)。 Java和javac版本从命令行都是正确的。

任何想法?

加满-X输出调试:

[25/04/13 13:45:21:812 BST] [main] ERROR org.springframework.test.context.TestContextManager: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]a166bd] to prepare test instance [[email protected]] 
java.lang.IllegalStateException: Failed to load ApplicationContext 
     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308) 
     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) 
     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301) 
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) 
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
     at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 
     at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103) 
     at $Proxy0.invoke(Unknown Source) 
     at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150) 
     at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91) 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) 
Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: src\test\resources\myProperties.properties (The system cannot find the path specified) 
     at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78) 
     at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663) 
     at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407) 
     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84) 
     at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1) 
     at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280) 
     at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304) 
     ... 32 more 
Caused by: java.io.FileNotFoundException: src\test\resources\myProperties.properties (The system cannot find the path specified) 
     at java.io.FileInputStream.open(Native Method) 
     at java.io.FileInputStream.<init>(FileInputStream.java:106) 
     at java.io.FileInputStream.<init>(FileInputStream.java:66) 
     at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70) 
     at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161) 
     at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:124) 
     at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181) 
     at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161) 
     at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69) 
     ... 39 more 

回答

0

什么是你正在阅读的文件中像什么原因呢?

我相信该文件捆绑在项目jar中。

在这种情况下,你可以把它读作一个类路径资源 - Thread.currentThread().getContextClassLoader().getResourceAsStream("xml/myfile.xml")

1

的代码在你的例子是正确的;在创建项目时,Maven通常会变成子目录,所以问题必须在别处。

当你看着absolutePath,你可以看到这不会发生。就好像“某人”试图从ParentProject执行模块ChildProject的代码。

要了解发生了什么,可以尝试运行带有-X的Maven来打开调试。还要确保在父POM中使用module元素正确引用了ChildProject

+0

在父pom中正确引用了子项目,并且它正在运行'mvn clean install'的父pom上(但是如果我直接在子模块中运行,仍然会得到相同的结果)。我将添加-X输出,但是我在我的文章中写过原始高级别错误消息。 – edwardmlyte 2013-04-25 12:48:10

+0

当您在子文件夹内运行'mvn clean install'时,错误*必须*消失。如果错误仍然存​​在,请从POM中移除内容。还可以尝试'mvn help:effective-pom'并在输出中搜索路径。当使用'-X'时,总是将输出传送给一个文件,然后用文本编辑器或者'grep'来检查。 – 2013-04-25 12:55:23

+2

这不是maven。这是我自己愚蠢的错误。我添加了一个快捷方式,使命令提示符默认为一个文件夹。不幸的是,一些正在运行的测试会调用ant脚本,这将重新打开具有不正确路径的命令提示符的新实例。因此失败。我自己的愚蠢的错误。非常节省时间的捷径。 – edwardmlyte 2013-04-26 12:59:47