如何让maven使用测试资源 [英] how to make maven use test resources
问题描述
当我使用命令:mvn test时,maven使用主要资源而不是src / test / resources中的测试资源。
when I use the command: mvn test, maven uses the main resources instead of the test ones in src/test/resources.
我如何使用maven测试资源而不是主要资源?
How can i make maven use the test resources rather than the main resources?
编辑:我使用Classloader查找我的资源。类加载器可以从我的src / test / resources目录中找到资源,但它首先在src / main / java中查找资源。
I use Classloader to find my resources. Classloader can find resources from my src/test/resources directory, but it looks in src/main/java first for the resource.
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream file =loader.getResourceAsStream("resource.xml");
或者在运行maven命令时是否可以使用任何属性来优先处理类路径?
Or is there any property I can use while running a maven command that will prioritize a classpath?
编辑2:我已经发现问题,它确实从测试/资源加载测试,但是我的测试涉及部署webapp(集成测试),并从那里开始webapp访问它使用src / main / resources的资源。有没有办法改变类路径的顺序,或者为一个部署在jetty容器中的WAR使一个先于另一个?
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. 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?
推荐答案
你不能给它另一个名字吗?
Loader不知道您的 resource.xml 位于 src / main / resources
或 src / test / resources
,它只是一个寻找的路径。我认为这不是Maven的问题,这就是Loader应该如何工作。在进行测试阶段时,您是否希望从 src / main / resources
中丢弃所有类/资源?
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. I think the problem it's not Maven, this is how the Loader should and it works. From Maven would you expect to discard all classes/resources from src/main/resources
when doing the test phase ?
这篇关于如何让maven使用测试资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!