![](/img/trans.png)
[英]Tell gradle to pick persistence.xml from src/test/resources instead of src/main/resources
[英]Make Eclipse use src/test/resources instead of src/main/resources
我正在 Eclipse 中編寫一個小的 Maven 應用程序。 我將一些屬性文件和我的應用程序上下文存儲在目錄 src/main/resources 中。
我現在想讓 Eclipse 使用目錄 src/test/resources 中的屬性。 所以當我在 Eclipse 中運行和調試程序時,應該使用這些測試屬性。
你知道我怎么能做到這一點嗎?
嘗試這個:
無論您使用Maven Eclipse Plugin還是m2eclipse , src/test/resources
在類路徑(更准確地說,它們的輸出目錄)上都位於src/main/resources
之前。 換句話說,沒有什么可做的,事情就像在命令行上一樣。
使用測試覆蓋(例如 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>
在您的測試中,請確保首先導入它:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:META-INF/spring/testOverrides.xml","classpath*:META-INF/spring/applicationContext.xml"})
public class SomeTest { ... }
現在將所有測試屬性放在src/test/resources/META-INF/spring/testproperties/
。
您還必須確保main
占位符配置器永遠不會看到testproperties
,例如這是我的:
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
它不使用雙 * 通配符,因此只會查看該目錄。
我使用上述方法取得了巨大成功。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.