[英]Make Eclipse use src/test/resources instead of src/main/resources
I'm writing a little Maven application in Eclipse.我正在 Eclipse 中编写一个小的 Maven 应用程序。 I store some property files and my application context in the directory src/main/resources.
我将一些属性文件和我的应用程序上下文存储在目录 src/main/resources 中。
I now want to make Eclipse use properties in the directory src/test/resources.我现在想让 Eclipse 使用目录 src/test/resources 中的属性。 So when I run and debug the program in Eclipse, these test properties should be used.
所以当我在 Eclipse 中运行和调试程序时,应该使用这些测试属性。
Do you know how I could make that happen?你知道我怎么能做到这一点吗?
Try this:尝试这个:
Whether you use the Maven Eclipse Plugin or m2eclipse , src/test/resources
precedes src/main/resources
on the classpath (more precisely, their output directories).无论您使用Maven Eclipse Plugin还是m2eclipse ,
src/test/resources
在类路径(更准确地说,它们的输出目录)上都位于src/main/resources
之前。 In other words, there is nothing to do, things just works as on the command line.换句话说,没有什么可做的,事情就像在命令行上一样。
Use a test override (eg testOverrides.xml):使用测试覆盖(例如 testOverrides.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- this file need to be imported before other contexts to ensure the test properties take effect -->
<context:property-placeholder location="classpath*:META-INF/spring/testproperties/*.properties"/>
</beans>
In your tests, make sure it's imported first:在您的测试中,请确保首先导入它:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:META-INF/spring/testOverrides.xml","classpath*:META-INF/spring/applicationContext.xml"})
public class SomeTest { ... }
Now put all your test properties in src/test/resources/META-INF/spring/testproperties/
.现在将所有测试属性放在
src/test/resources/META-INF/spring/testproperties/
。
You have to also make sure that the main
placeholder configurer doesn't ever see testproperties
, eg here's mine:您还必须确保
main
占位符配置器永远不会看到testproperties
,例如这是我的:
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
It doesn't use the double-* wildcard, so will only look at that one directory.它不使用双 * 通配符,因此只会查看该目录。
I use the above method with great success.我使用上述方法取得了巨大成功。
Tested in eclipse 2021. Go to Run As Configuration (or debug) and go to Classpath tab.在 eclipse 2021 中测试。转到 Run As Configuration(或调试)并转到 Classpath 选项卡。 Now you could check or uncheck, depends if you want to use /src/main/resources or /src/test/resources, like so:
现在您可以选中或取消选中,这取决于您是否要使用 /src/main/resources 或 /src/test/resources,如下所示:
Finaly, apply and run your program最后,应用并运行你的程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.