Ken Alton
Ken Alton

Reputation: 734

Spring Property From Command Line

I've a Spring Profile enabled app with two profiles [cat|dog] and running with desired profile is fine in Idea using Maven:

clean compile package exec:java -Dspring.profiles.active=dog

I wish to run the packaged jar from command line but can't seem to find the right syntax to set the profile. I've tried the follwing but none are correct:

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main --spring.profiles.active=dog

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main -Drun.arguments="--spring.profiles.active=dog"

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main -Drun.arguments="spring.profiles.active=dog"

java -cp myShadedJar-1.0-SO-static.jar org.my.package.Main -Dspring.profiles.active=dog

What's the correct way to pass a Spring property when running from CLI?

Thanks in advance, KA.

Upvotes: 1

Views: 1986

Answers (1)

thopaw
thopaw

Reputation: 4054

According to Spring Docs you can set a JVM Property or an Env Var.

java -jar -Dspring.profiles.active=dog myShadedJar-1.0-SO-static.jar

or ( *NIX Systems)

SPRING_PROFILES_ACTIVE=dog java -jar myShadedJar-1.0-SO-static.jar

Upvotes: 3

Related Questions