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();

次に、フォルダーが見つからないため間違っているabsolutePathため、 .C:\ParentProject\src\test\resources\xml\myfile.xmlChildProjectFileNotFoundException

たとえば、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)

クラスパスの問題だと感じます。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
4

2 に答える 2

1

あなたの例のコードは正しいです。Maven は通常、プロジェクトをビルドするときにサブディレクトリに変更するため、問題は別の場所にあるはずです。

を調べてみるとabsolutePath、そうではないことがわかります。あたかも「誰か」がChildProjectからモジュールのコードを実行しようとしているかのようですParentProject

何が起こるかを調べるために、Maven を実行し-Xてデバッグを有効にすることができます。また、要素ChildProjectを使用して親 POM で正しく参照されていることを確認してください。module

于 2013-04-25T09:48:44.083 に答える
0

そのようなファイルを読んでいる理由は何ですか?

ファイルはプロジェクト jar にバンドルされていると思います。

その場合、クラスパス リソースとして読み取ることができます -Thread.currentThread().getContextClassLoader().getResourceAsStream("xml/myfile.xml")

于 2013-04-25T09:45:20.470 に答える