【问题标题】:Make Eclipse use src/test/resources instead of src/main/resources让 Eclipse 使用 src/test/resources 而不是 src/main/resources
【发布时间】:2011-02-24 03:30:45
【问题描述】:

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

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

你知道我是怎么做到的吗?

【问题讨论】:

  • 它会自动执行此操作。你读的怎么样?
  • 我使用

标签: java eclipse maven-2 resources properties


【解决方案1】:

试试这个:

  1. 转到“运行->运行配置...”(如果是调试“运行->调试配置...”)
  2. 打开您使用的运行(调试)配置
  3. 打开“类路径”选项卡
  4. 选择“用户条目”并点击右侧的“高级...”
  5. 在打开的窗口中选择“添加文件夹”,指向您的 src/test/resources
  6. 此文件夹将出现在“用户条目”下,然后您应该将其向上移动以使其成为类路径中的第一个

【讨论】:

  • src/main/resources 在类路径上位于 src/test/resources 之前。因此,您的 src/main/resources 中的 db.properties 已加载。
  • 感谢您的回答,它对我帮助很大!对我来说,确切的点击路径是(可能是较新版本的 eclipse?):菜单“运行”-> 选择“运行配置 ...”-> 选择选项卡“依赖项”-> 在树中选择“类路径条目”->单击按钮 [Advanced ...] -> 从单选组中选择“添加文件夹”-> 单击按钮 [Ok]。
  • 我对答案的使用告诉我,我确实在构建中缺少一个文件夹。但最终没有必要配置额外的文件夹。在测试文件夹中创建“资源”文件夹时我有一个错字,所以 maven 不知道我的文件夹命名错误。当我纠正错字后,maven 可以识别该文件夹,我可以使用:项目上的上下文菜单 -> "Maven" -> "Update project ..." 并保存:菜单 "Project" -> "Clean . ……”。这已经足够了,即使没有在运行配置中配置 axtra 文件夹。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    使用测试覆盖(例如 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"/>
    

    它不使用双*通配符,所以只会查看那个目录。

    我使用上述方法非常成功。

    【讨论】:

      【解决方案4】:

      在 eclipse 2021 中测试。转到 Run As Configuration(或调试)并转到 Classpath 选项卡。现在您可以选中或取消选中,这取决于您是要使用 /src/main/resources 还是 /src/test/resources,如下所示:

      最后,应用并运行你的程序

      【讨论】:

        猜你喜欢
        • 2015-03-22
        • 2013-05-11
        • 2021-04-23
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        相关资源
        最近更新 更多