简体   繁体   English

使用预定义的配置文件运行 Spring Boot JAR

[英]Run Spring Boot JAR with predefined profile

I'm trying to run my JAR file with the java -jar sample.jar command and I'm using application.yml configuration file with the following setup我正在尝试使用java -jar sample.jar命令运行我的 JAR 文件,并且我正在使用具有以下设置的application.yml配置文件

spring:
  profiles:
    active:
      - dev
---
server:
  port: 8081
spring:
  profiles: dev
---
server:
  port: 8082
spring:
  profiles: test

But the application doesn't recognize the profile giving back the information of但是该应用程序无法识别返回信息的配置文件
No active profile set, falling back to default profiles: default and setting up the port to default 8080 No active profile set, falling back to default profiles: default并将端口设置为默认 8080

I tried things like我试过这样的事情
java -jar sample.jar -Drun.jvmArguments=-Dspring.profiles.active=dev or java -jar sample.jar -Drun.jvmArguments=-Dspring.profiles.active=dev
java -jar sample.jar -Dspring.profiles.active=dev

In my pom.xml file the only change I made was adding <packaging>jar</packaging> property.在我的pom.xml文件中,我所做的唯一更改是添加了<packaging>jar</packaging>属性。

From what I've learned maven profiles are completely different things and shouldn't influence spring profiles.从我了解到的 maven 配置文件是完全不同的东西,不应该影响 spring 配置文件。

I don't know where to look for the problem as when I start the app by Run button in Intellij IDEA it works perfectly and recognizes every profile set up as active.我不知道在哪里查找问题,因为当我通过 Intellij IDEA 中的“运行”按钮启动应用程序时,它运行良好,并且可以识别设置为活动的每个配置文件。

The actual question here is how to set up default profile to run while building an artifact so there's no need to place extra flags in the terminal command running the jar.这里的实际问题是如何在构建工件时设置要运行的默认配置文件,因此无需在运行 jar 的终端命令中放置额外的标志。

I know there's a lot of questions like mine but most of their accepted answers doesn't work in my case and the rest is unanswered.我知道有很多像我这样的问题,但他们接受的大多数答案在我的情况下都不起作用,其余的都没有答案。 If you know what might be an issue here, please let me know.如果您知道这里可能有什么问题,请告诉我。 Thank you in advance for any help!预先感谢您的任何帮助!

You can activate profile through command line argument.您可以通过命令行参数激活配置文件。 In your trying, you are defining the active profile after the name of the jar.在您的尝试中,您是在 jar 名称之后定义活动配置文件。 You need to provide the program argument before the name of the jar you are running.您需要在正在运行的 jar 名称之前提供程序参数。

java -jar -Dspring.profiles.active=dev  sample.jar

An alternative way to set the active profile is to create a application.properties file with the following property:设置活动配置文件的另一种方法是创建具有以下属性的application.properties文件:

spring.profiles.active=prod

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

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