對我的同事工作的多模塊項目不適合我。在引用文件的任何測試期間以及在引用文件時,彈簧配置文件中都會出現問題。引用永遠不會包含子模塊文件夾名稱,或者完整的文件路徑不能正確解析。子模塊內使用的文件路徑不正確
例如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();
然後absolutePath
是C:\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
在父pom中正確引用了子項目,並且它正在運行'mvn clean install'的父pom上(但是如果我直接在子模塊中運行,仍然會得到相同的結果)。我將添加-X輸出,但是我在我的文章中寫過原始高級別錯誤消息。 – edwardmlyte 2013-04-25 12:48:10
當您在子文件夾內運行'mvn clean install'時,錯誤*必須*消失。如果錯誤仍然存在,請從POM中移除內容。還可以嘗試'mvn help:effective-pom'並在輸出中搜索路徑。當使用'-X'時,總是將輸出傳送給一個文件,然後用文本編輯器或者'grep'來檢查。 – 2013-04-25 12:55:23
這不是maven。這是我自己愚蠢的錯誤。我添加了一個快捷方式,使命令提示符默認爲一個文件夾。不幸的是,一些正在運行的測試會調用ant腳本,這將重新打開具有不正確路徑的命令提示符的新實例。因此失敗。我自己的愚蠢的錯誤。非常節省時間的捷徑。 – edwardmlyte 2013-04-26 12:59:47