Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Eclipse和maven中的路径处理_Spring_Maven_Spring Test_Maven Surefire Plugin - Fatal编程技术网

Spring Eclipse和maven中的路径处理

Spring Eclipse和maven中的路径处理,spring,maven,spring-test,maven-surefire-plugin,Spring,Maven,Spring Test,Maven Surefire Plugin,我有一个多模块maven项目(ModuleA、ModuleB、ModuleC) 我有一个模块测试 @ContextConfiguration(locations = { "file:../ModuleB/src/test/resources/application-context-test.xml", "classpath:/mvc-dispatcher-servlet-test.xml" }) 从ModuleB中的文件加

我有一个多模块maven项目(ModuleA、ModuleB、ModuleC)

我有一个模块测试

@ContextConfiguration(locations = 
           { "file:../ModuleB/src/test/resources/application-context-test.xml",
             "classpath:/mvc-dispatcher-servlet-test.xml"
           })
从ModuleB中的文件加载上下文

当我在eclipse中运行测试时,它可以工作。但当测试由maven运行时就不是了

在eclipse中,currentpath设置为ModuleA,但在maven中,它设置在父文件夹中

所以我应该写:

文件:/ModuleB/src/test/resources/application-context-test.xml

插入

文件:/ModuleB/src/test/resources/application-context-test.xml

使其在maven中工作(但会破坏eclipse)

谢谢


我如何告诉maven(或surefire插件)相对于模块路径而不是父级运行单元测试?

添加此项而不是正斜杠-
“classpath:application context test.xml”


Maven将自动从资源中提取它

无论是什么方案,这都不是最好的做法

请按以下步骤进行:

首先,确定模块A是否与模块B分离?(我想是的)

如果是,请使用
classpath:/application context test.xml
,然后使用父目录下的mvn install来打包模块B并安装它。然后,您可以运行您的测试用例,它应该可以工作


如果没有,请将application-context-test.xml复制到模块A中,但我认为这很奇怪。通常,如果您想使用其他模块的文件,这是因为此模块依赖于它。

放置在
${project.basedir}/src/test/resources
中的文件会复制到
${project.build.testOutputDirectory}
,默认情况下它是“目标/测试类”。Maven在
生成testResources
阶段自动执行此操作

因此,您不希望在/src/test/resources目录中查找该文件,因为该文件不在类路径上。如果您在ModuleB中进行了一个查找文件的测试,您可以这样编写:

@ContextConfiguration(locations = 
       { "classpath:/application-context-test.xml",
         "classpath:/mvc-dispatcher-servlet-test.xml"
       })
 <dependency>
     <groupId>com.mycompany.theGroupId</groupId>
     <artifactId>moduleB</artifactId>
     <version>yourVersion</version>
     <type>test-jar</type>
    <scope>test</scope>
 </dependency>
您遇到的困难是您想要的文件存在于另一个项目中。因此,您试图解决的问题是,如何将另一个项目的资源放到这个项目的类路径上?您可以通过以下两种方式之一解决此问题:

一:在模块B中执行。这将获取ModuleB的
target/testclasses
目录中的所有类和资源,并将其打包到一个jar中。然后,您可以将测试jar作为依赖项添加到模块a的POM中,如下所示:

@ContextConfiguration(locations = 
       { "classpath:/application-context-test.xml",
         "classpath:/mvc-dispatcher-servlet-test.xml"
       })
 <dependency>
     <groupId>com.mycompany.theGroupId</groupId>
     <artifactId>moduleB</artifactId>
     <version>yourVersion</version>
     <type>test-jar</type>
    <scope>test</scope>
 </dependency>

com.mycompany.theGroupId
模B
你的版本
试验罐
测试

第二:您可以将模块B的测试资源移动到一个单独的项目中(在目录
newProject/src/main/resources
中),让Maven像往常一样构建jar,并让ModuleA和ModuleB将newProject添加为范围
test

的依赖项,为什么不使用
classpath
而不是
file
?我试过:“类路径:/application-context-test.xml“但是这个文件似乎不在类路径中。也许如果我把它从ModuleB/src/main/resources/移到classpath…Aggh。。。没有注意到它位于测试文件夹中。无论如何,让测试依赖于另一个模块测试不是一个好主意。我没有在maven中尝试,但在eclipse中找不到它,我需要一个解决方案,可以同时工作。复制文件是我将要结束的工作。但在最后的希望中,我做到了:将surefire插件从2.16更新到2.17,并删除0以获得默认值,现在它似乎可以工作了:|