简体   繁体   English

在 pom.xml 中为 Spring Boot 项目指定 main 和 test 资源目录的正确方法

[英]Correct way to specify main and test resource directories in pom.xml for Spring Boot project

I have 2 application.properties file in my small Spring Boot project.我的小型 Spring Boot 项目中有 2 个application.properties文件。 One is saved in /src/main/resources/application.properties and the other is in /src/test/resources/application.properties .一个保存在/src/main/resources/application.properties中,另一个保存在/src/test/resources/application.properties中。

Some values in main doesn't necessarily have to be present in test . main中的某些值不一定必须存在于test中。

Problem : When I do maven clean -> update project -> maven install in Spring Tool Suite IDE, I get multiple问题:当我在 Spring Tool Suite IDE 中执行 maven clean -> update project -> maven install 时,我得到了多个

Could not resolve placeholder 'JMS_URL' in value "${JMS_URL}"... and so on无法解析值“${JMS_URL}”中的占位符“JMS_URL”...等等

But if I delete the application.properties file in test /src/test/resources/application.properties the error goes away and I get a successful maven install build.但是,如果我删除test /src/test/resources/application.properties中的application.properties文件,错误就会消失,并且我会获得成功的 maven 安装版本。

I searched online and found that you need to specify the resources directory in pom.xml if you get that error.我在网上搜索,发现如果遇到这个错误,需要在 pom.xml 中指定资源目录。

Like so :像这样:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>

In my case, adding that <resource> block still did not resolve my problem because it's only specifying the directory of application.properties in src/main/resources but not for test在我的情况下,添加该<resource>块仍然没有解决我的问题,因为它只是在src/main/resources中指定application.properties的目录,而不是用于测试

What is the correct way of handling this kind of situation where you have 2 application.properties file in main and test resource folder?资源文件夹和测试资源文件夹中有 2 个application.properties文件的情况下,处理这种情况的正确方法是什么?

I'd appreciate any comment.我会很感激任何评论。

Thank you.谢谢你。

The correct way of handling this kind of situation is naming the test properties file differently, like application-test.properties .处理这种情况的正确方法是以不同的方式命名测试属性文件,例如application-test.properties Then you either run your tests with the test profile active or explicitly specify然后,您可以在激活test配置文件的情况下运行测试,或者明确指定

@TestPropertySource("classpath:application-test.properties")

on your @SpringBootTest .在您的@SpringBootTest上。 Both ways you'll have all your properties from the main application.properties and application-test.properties active with properties from the latter having a higher priority.这两种方式都可以使主application.propertiesapplication-test.properties中的所有属性都处于活动状态,而后者的属性具有更高的优先级。

Leaving the test properties file with the default name ( application.properties ) completely overrides the main properties file as you might have noticed.您可能已经注意到,使用默认名称 ( application.properties ) 保留测试属性文件会完全覆盖主属性文件。 This way the test properties file has to have all the same required properties as the main one for the tests to work.这样,测试属性文件必须具有与测试工作的主要属性相同的所有必需属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM