Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass system property to Gradle task

People also ask

How do you pass JVM arguments in Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.


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.


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

bootRun.mustRunAfter local

Then run gradle command as:

gradle bootRun local

There is no generic way to pass system properties to a task. In a nutshell, it's only supported for tasks that fork a separate JVM.

The bootRunLocal task (as defined above) will not execute in a separate JVM, and calling execute() on a task isn't supported (and would have to happen in the execution phase in any case). Tests, on the other hand, are always executed in a separate JVM (if executed by a Test task). To set system properties for test execution, you need to configure the corresponding Test task(s). For example:

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

For more information, see Test in the Gradle Build Language Reference.


According to the spring-boot-gradle-plugin documentation you should be able to pass arguments like this

./gradlew bootRun --args='--spring.profiles.active=dev'

Seems like this is a new gradle feature since 4.9. I used it in my project and it worked out of the box.


SPRING_PROFILES_ACTIVE=local gradle clean bootRun

This is according to this and this and it works.


For gradle 2.14 below example works. I have added as below.
When System.properties['spring.profiles.active'] is null then default profile is set.

  bootRun {
       systemProperty 'spring.profiles.active', System.properties['spring.profiles.active']
    }

command line example

gradle bootRun -Dspring.profiles.active=dev