子模块中使用的文件路径不正确

时间:2013-04-25 09:37:00

标签: java spring maven

对我的同事有用的多模块项目失败了。在引用文件的任何测试期间以及引用文件时弹簧配置文件中出现问题。引用永远不会包含子模块文件夹名称,或者无法正确解析完整文件路径。

例如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

在弹簧配置文件中提及属性文件时遇到同样的问题,例如在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)

我觉得这是一个类路径问题。我有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.springframework.test.context.support.DependencyInjectionTestExecutionListener@a166bd] to prepare test instance [my.package.MyTest@60029d]
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

2 个答案:

答案 0 :(得分:1)

示例中的代码是正确的; Maven通常会在构建项目时更改为子目录,因此问题必须在其他地方。

当您查看absolutePath时,您会发现这不会发生。好像“某人”试图从ChildProject执行模块ParentProject的代码。

要了解会发生什么,您可以尝试使用-X运行Maven以启用调试。另外,请确保使用ChildProject元素在父POM中正确引用module

答案 1 :(得分:0)

你正在阅读那样的文件是什么原因?

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

在这种情况下,您可以将其作为类路径资源 - Thread.currentThread().getContextClassLoader().getResourceAsStream("xml/myfile.xml")

进行阅读