Java 让 Eclipse 使用 src/test/resources 而不是 src/main/resources

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:24:32  来源:igfitidea点击:

Make Eclipse use src/test/resources instead of src/main/resources

javaeclipsemaven-2resourcesproperties

提问by

I'm writing a little Maven application in Eclipse. I store some property files and my application context in the directory src/main/resources.

我正在 Eclipse 中编写一个小的 Maven 应用程序。我将一些属性文件和我的应用程序上下文存储在 src/main/resources 目录中。

I now want to make Eclipse use properties in the directory src/test/resources. So when I run and debug the program in Eclipse, these test properties should be used.

我现在想让 Eclipse 使用目录 src/test/resources 中的属性。所以当我在 Eclipse 中运行和调试程序时,应该使用这些测试属性。

Do you know how I could make that happen?

你知道我怎么能做到这一点吗?

采纳答案by Pascal Thivent

Whether you use the Maven Eclipse Pluginor m2eclipse, src/test/resourcesprecedes src/main/resourceson the classpath (more precisely, their output directories). In other words, there is nothing to do, things just works as on the command line.

无论您使用Maven Eclipse Plugin还是m2eclipse,都src/test/resources位于src/main/resources类路径(更准确地说,是它们的输出目录)之前。换句话说,没有什么可做的,事情就像在命令行上一样。

回答by yatskevich

Try this:

尝试这个:

  1. Go to "Run->Run configurations..." (in case of debug "Run->Debug configurations...")
  2. Open Run (Debug) configuration which you use
  3. Open "Classpath" tab
  4. Select "User Entries" and click "Advanced..." on the right
  5. In the opened window select "Add folder", point to your src/test/resources
  6. This folder will appear under "User entries", then you should move it up to make it the first on your classpath
  1. 转到“运行->运行配置...”(在调试“运行->调试配置...”的情况下)
  2. 您使用的打开运行(调试)配置
  3. 打开“类路径”选项卡
  4. 选择“用户条目”,然后单击右侧的“高级...”
  5. 在打开的窗口中选择“添加文件夹”,指向你的 src/test/resources
  6. 此文件夹将出现在“用户条目”下,然后您应该将其向上移动以使其成为类路径中的第一个

回答by opyate

Use a test override (e.g. 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 mainplaceholder configurer doesn't ever see testproperties, e.g. 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.

我使用上述方法取得了巨大成功。