如何让IntelliJ运行与其构建的Maven配置文件匹配的Spring Boot应用程序?

huangapple go评论65阅读模式
英文:

How to make IntelliJ run a Spring Boot App with the Maven profile it's built with?

问题

这让我疯狂了 - 我有一个使用Maven作为构建工具的Spring Boot应用程序。在POM中设置了各种配置文件,这些配置文件指向项目中相应目录中的资源,所以如果你使用以下命令构建:

mvn clean install -PBrandADev

你会得到Brand A的开发配置,包括对应的application.properties和其他配置文件。如果你使用以下命令构建:

mvn clean install -PBrandBProd

你会得到Brand B的生产配置。这对我很重要,因为它控制着我连接到哪个CMS,因此我可以通过以Brand B Prod的方式构建,在调试中运行生产环境的实例,以便在出现错误时查看问题所在;或者构建开发配置,以便将其连接到开发CMS。

由于这是一个Spring Boot应用程序,在每个配置的resources目录中都有一个名为"banner.txt"的文件,其中包含"Brand A - Dev"、"Brand A - QA"或该配置对应的品牌和CMS信息。

然而最近,当我构建特定配置时,比如我想要Brand B的预览生产环境,我在启动集成测试时看到的是"Brand B - 预览生产",这是符合预期的。但是当我在屏幕顶部的“Run/Debug配置”小部件中使用应用程序配置项启动时,它却以"Brand A - Dev"启动。

我尝试过清理和重新安装应用程序。我尝试过重新导入Maven依赖。我尝试过清除缓存并重新启动IDE。我尝试过取消选择所有配置然后重新选择我需要的配置。但都没有效果。除了在今天稍早时候,它正常工作,然后我尝试切换回开发环境,结果却卡在了生产配置。有人知道我该怎么做才能强制它使用构建时的配置吗?这是IntelliJ的一个bug吗?

英文:

This has been driving me crazy - I have a Spring Boot app which uses Maven as its build tool. In the POM there are various profiles set up, which point to resources in a corresponding directory within the project, so that if you build, for example, with

mvn clean install -PBrandADev

you get Brand A's Dev profile, with the corresponding application.properties and other config files, and if you build it with

mvn clean install -PBrandBProd

you get Brand B's production profile. This is important to me because it controls which CMS I'm connecting to, and thus I'm able by building as Brand B Prod to run an instance of the Prod environment in debug & see what's going wrong when there's a bug, or otherwise to build the Dev profile for the development work I'm doing, to plug into the Dev CMS.

As it's a Spring Boot app, in the resources directory for each profile we have a banner.txt file that says "Brand A - Dev" or "Brand A - QA" or whatever the brand & CMS is for that profile.

Just lately though, I'm building for a particular profile, e.g. I want Brand B's Preview-Prod environment, and I see when it starts up for its integration tests "Brand B - Preview-Prod", which is as should be expected, but then when I start it up using the Application config item in the Run/Debug configurations widget at the top of the screen, it starts up saying "Brand A - Dev"

I've tried cleaning & reinstalling the app. I've tried re-importing the maven dependencies. I've tried invalidating the cache and restarting the IDE. I've tried deselecting all the profiles and reselecting the one I need. No joy. Except, just earlier today, it worked, and then I tried to swap back to the Dev environment & it got stuck on Prod. Does anyone know what I can do to force it to use the profile it's built with? Is this a bug in IntelliJ or what?

答案1

得分: 1

可能的原因是启动应用程序的Run Configuration在启动应用程序之前使用默认配置文件进行构建。请验证配置的Before Launch部分。

作为解决方法,针对每个要启动的配置文件创建一个Run Configuration,并在运行配置的VM选项中指定-Dspring.profiles.active=<PROFILE-NAME>。另外,您可以在启动应用程序之前使用相同的配置文件构建应用程序,相应地调整Before Launch部分。

英文:

A possible reason of such behaviour could be the Run Configuration used to start the application which builds it using default profile before starting the application. Verify Before Launch section of the configuration.

As a workaround, create a Run Configuration per profile you'd like to start, and specify -Dspring.profiles.active=<PROFILE-NAME> in the VM Options of the run configuration. Additionally, you could build the application using the same profile before starting it, adjust the Before Launch section accordingly.

答案2

得分: 1

你可以使用 Spring Boot Maven 插件来代替。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这允许你使用 mvn spring-boot:run -PBrandADev 命令,以正确的 Maven 配置文件来启动 Spring Boot 应用程序。

英文:

You can use the spring-boot maven plugin instead.

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

This allows you to use mvn spring-boot:run -PBrandADev and start the spring boot application with the correct maven profile.

huangapple
  • 本文由 发表于 2020年7月29日 00:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定