Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run bootRun with spring profile via gradle task

I'm trying to set up gradle to launch the bootRun process with various spring profiles enabled.

My current bootRun configuration looks like:

bootRun {     // pass command line options from gradle to bootRun     // usage: gradlew bootRun "-Dspring.profiles.active=local,protractor"     if (System.properties.containsKey('spring.profiles.active')) {         systemProperty "spring.profiles.active", System.properties['spring.profiles.active']     } } 

I'd like to set system properties with a gradle task, and then execute bootRun.

My attempt looked like this:

task bootRunDev  bootRunDev  {     System.setProperty("spring.profiles.active", "Dev") } 

A few questions:

  1. is systemProperty a part of the spring boot bootRun configuration?
  2. is it possible to set a system property in another task?
  3. What should my next step be? I need to get bootRunDev configuration to happen before bootRun
  4. Is there another approach I should look into

-Eric

like image 860
Eric Francis Avatar asked Apr 28 '16 18:04

Eric Francis


People also ask

How does Gradle bootRun work?

The Spring Boot gradle plugin provides the bootRun task that allows a developer to start the application in a “developer mode” without first building a JAR file and then starting this JAR file. Thus, it's a quick way to test the latest changes you made to the codebase.

How do I pass a spring profile in IntelliJ?

Override any configuration property by passing it as a JVM option. For example, if you override the value of the spring.config.name property with my. config , IntelliJ IDEA will pass -Dspring.config.name=my. config on the command line when running this Spring Boot application.


2 Answers

Spring Boot v2 Gradle plugin docs provide an answer:

6.1. Passing arguments to your application

Like all JavaExec tasks, arguments can be passed into bootRun from the command line using --args='<arguments>' when using Gradle 4.9 or later.

To run server with active profile set to dev:

$ ./gradlew bootRun --args='--spring.profiles.active=dev' 
like image 55
Ivar Avatar answered Sep 19 '22 22:09

Ivar


Environment variables can be used to set spring properties as described in the documentation. So, to set the active profiles (spring.profiles.active) you can use the following code on Unix systems:

SPRING_PROFILES_ACTIVE=test gradle clean bootRun 

And on Windows you can use:

SET SPRING_PROFILES_ACTIVE=test gradle clean bootRun 
like image 42
david Avatar answered Sep 23 '22 22:09

david