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

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

Run Spring Boot JAR with predefined profile

问题

我正在尝试使用java -jar sample.jar命令运行我的JAR文件,并且我正在使用名为application.yml的配置文件,其中包含以下设置:

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

但是应用程序无法识别配置文件,返回以下信息:
No active profile set, falling back to default profiles: default,并将端口设置为默认的8080。

我尝试过以下方式:
java -jar sample.jar -Drun.jvmArguments=-Dspring.profiles.active=dev 或者
java -jar sample.jar -Dspring.profiles.active=dev

在我的pom.xml文件中,我唯一进行的更改是添加了<packaging>jar</packaging>属性。

根据我所了解,Maven配置文件和Spring配置文件是完全不同的东西,不应相互影响。

我不知道从哪里查找问题,因为当我在Intellij IDEA中点击Run按钮启动应用程序时,它可以完美运行,并识别了设置为活动状态的每个配置文件。

实际的问题是:如何在构建构件时设置默认配置文件,以便在运行JAR的终端命令中不需要额外添加标志。

我知道有很多类似问题的提问,但其中大部分的已接受答案在我的情况下不起作用,其余的问题则没有得到解答。如果您知道可能出现的问题,请告诉我。非常感谢您提前的任何帮助!

英文:

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

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

I tried things like
java -jar sample.jar -Drun.jvmArguments=-Dspring.profiles.active=dev or
java -jar sample.jar -Dspring.profiles.active=dev

In my pom.xml file the only change I made was adding &lt;packaging&gt;jar&lt;/packaging&gt; property.

From what I've learned maven profiles are completely different things and shouldn't influence spring profiles.

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.

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.

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!

答案1

得分: 10

通过命令行参数可以激活配置文件。在尝试中,您需要在jar文件名之后定义活动配置文件。您需要在运行jar文件之前提供程序参数。

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

设置活动配置文件的另一种方法是创建一个包含以下属性的application.properties文件:

spring.profiles.active=prod

英文:

You can activate profile through command line argument. In your trying, you are defining the active profile after the name of the jar. You need to provide the program argument before the name of the jar you are running.

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:

spring.profiles.active=prod

huangapple
  • 本文由 发表于 2020年9月27日 01:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64080705.html
匿名

发表评论

匿名网友

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

确定