Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

spring - How to pass system property to Gradle task

I'm using Gradle spring-boot plugin and I need to select a spring active profile for the test run.

How do I pass spring.profiles.active system property to the bootRun plugin's task?

What has already failed:

task bootRunLocal {
    systemProperty "spring.profiles.active", "local"
    System.setProperty("spring.profiles.active", "local")
    tasks.bootRun.execute() // I suspect that this task is executed in a separate JVM
}

and some command line magic also fails:

./gradle -Dspring.profiles.active=local bootRun

Could someone kindly help me solve my troubles?

Update from the answers and comments:

I'm able to set the systemProperty and pass it to the spring container by doing :

run {
    systemProperty "spring.profiles.active", "local"
}

However, when I do this, the local profile is being set for both bootRun task and bootRunLocal task. I need a way to set this property for bootRunLocal task and call booRun task from bootRunLocal.

That might sound very simple, but I come with peace from the structured world of Maven.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know I'm late here... but I recently faced this exact issue. I was trying to launch bootRun with spring.profiles.active and spring.config.location set as system properties on the command line.

So, to get your command line "magic" to work, simply add this to your build.gradle

bootRun {
    systemProperties System.properties
}

Then running from the command line...

gradle -Dspring.profiles.active=local bootRun

Will set local as the active profile, without needing to define a separate task simply to add the env variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...