Run Spring Boot JAR with predefined profile

Viewed 4499

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 <packaging>jar</packaging> 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 Answers

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

Related