简体   繁体   English

如何让maven使用测试资源

[英]how to make maven use test resources

when I use the command: mvn test, maven uses the main resources instead of the test ones in src/test/resources.当我使用命令时:mvn test,maven 使用主要资源而不是 src/test/resources 中的测试资源。

How can i make maven use the test resources rather than the main resources?我怎样才能让 maven 使用测试资源而不是主要资源?

EDIT: I use Classloader to find my resources.编辑:我使用 Classloader 来查找我的资源。 Classloader can find resources from my src/test/resources directory, but it looks in src/main/java first for the resource.类加载器可以从我的 src/test/resources 目录中找到资源,但它首先在 src/main/java 中查找资源。

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream file =loader.getResourceAsStream("resource.xml");

Or is there any property I can use while running a maven command that will prioritize a classpath?或者在运行 maven 命令时我可以使用任何属性来确定类路径的优先级?

EDIT 2: I have figured out the problem, it does load from test/resources for the test, however my test involves deploying a webapp (integration testing) and from there when the webapp accesses the resource it uses src/main/resources.编辑2:我已经找到了问题,它确实从测试/资源加载进行测试,但是我的测试涉及部署一个webapp(集成测试),当webapp访问它使用src/main/resources的资源时,从那里开始。 Is there a way to change the order of the classpath or make one precede the other for a WAR deployed in a jetty container?对于部署在码头容器中的 WAR,有没有办法更改类路径的顺序或使一个优先于另一个?

Please check your target/test-classes directory for resource.xml .请检查您的目标/测试类目录中的resource.xml Your file should be here.你的文件应该在这里。 If you find this file, I think the same name is the problem.如果你找到这个文件,我认为同名是问题所在。 Try rename the file for example to test-resource.xml.尝试将文件重命名为 test-resource.xml。 If the file doesn't exists in the test-classes directory, you should configure resource management in the maven pom.xml.如果 test-classes 目录中不存在该文件,则应在 maven pom.xml 中配置资源管理。 Use maven-resouce-plugin .使用maven-resouce-plugin Hope it helps.希望能帮助到你。

Can't you give it another name?你不能给它另一个名字吗?
The Loader doesn't know that your resource.xml is in src/main/resources or src/test/resources , it's only a path to look for.加载器不知道您的resource.xml位于src/main/resourcessrc/test/resources中,它只是寻找的路径。 I think the problem it's not Maven, this is how the Loader should and it works.我认为问题不是 Maven,这就是 Loader 应该如何工作的。 From Maven would you expect to discard all classes/resources from src/main/resources when doing the test phase?从 Maven 开始,您是否希望在进行测试阶段时丢弃src/main/resources中的所有类/资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM