如何让maven使用测试资源

时间:2022-01-02 17:14:36

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 can find resources from my src/test/resources directory, but it looks in src/main/java first for the resource.

编辑:我使用Classloader来查找我的资源。类加载器可以从我的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. 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?

编辑2:我已经发现问题,它确实从测试/资源加载测试,但是我的测试涉及部署webapp(集成测试),当webapp访问资源时,它使用src / main / resources。有没有办法改变类路径的顺序,或者为一个部署在jetty容器中的WAR使一个在另一个之前?

2 个解决方案

#1


4  

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 ?

你不能给它另一个名字吗? Loader不知道你的resource.xml在src / main / resources或src / test / resources中,它只是一个寻找的路径。我认为这不是Maven的问题,这就是Loader应该如何工作。从Maven开始,您希望在进行测试阶段时丢弃src / main / resources中的所有类/资源吗?

#2


6  

Please check your target/test-classes directory for 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. If the file doesn't exists in the test-classes directory, you should configure resource management in the maven pom.xml. Use maven-resouce-plugin. Hope it helps.

请检查您的target / test-classes目录中的resource.xml。你的文件应该在这里。如果你找到这个文件,我认为同样的名字是问题。尝试将文件重命名为例如test-resource.xml。如果test-classes目录中不存在该文件,则应在maven pom.xml中配置资源管理。使用maven-resouce-plugin。希望能帮助到你。

#1


4  

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 ?

你不能给它另一个名字吗? Loader不知道你的resource.xml在src / main / resources或src / test / resources中,它只是一个寻找的路径。我认为这不是Maven的问题,这就是Loader应该如何工作。从Maven开始,您希望在进行测试阶段时丢弃src / main / resources中的所有类/资源吗?

#2


6  

Please check your target/test-classes directory for 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. If the file doesn't exists in the test-classes directory, you should configure resource management in the maven pom.xml. Use maven-resouce-plugin. Hope it helps.

请检查您的target / test-classes目录中的resource.xml。你的文件应该在这里。如果你找到这个文件,我认为同样的名字是问题。尝试将文件重命名为例如test-resource.xml。如果test-classes目录中不存在该文件,则应在maven pom.xml中配置资源管理。使用maven-resouce-plugin。希望能帮助到你。