Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set spring boot active profile in Gradle 4.7

Overall:
I'm trying to run gradle build task for a specific spring profile but I've got an error in passing following test:

au.com.mnpd.security.JwtTokenUtilTest > generateToken_succeeds FAILED
java.lang.IllegalStateException
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
            Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
                Caused by: org.springframework.beans.factory.BeanCreationException
                    Caused by: java.lang.IllegalArgumentException

The test is using some properties from spring development profile (located in application-development.yaml). But I couldn't find any way to pass active profile to gradle build command. I tried followings but again the same issue:

- gradlew -Dspring.profiles.active=development build

- gradlew -Pdevelopment build

Question:
Is there anyway to pass active profile to gradle (v 4.7) build task like what is applicable for bootRun task as follows:

bootRun {
        bootRun.systemProperty 'spring.profiles.active', 'development'
}

Note: I tried the same for build but build.systemProperty method does not exist for build task.

As I'm new in gradle, I'd be grateful is you could share your genuine solutions with me.

like image 446
saeedj Avatar asked Jun 03 '18 11:06

saeedj


People also ask

How do I set a spring boot profile?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.

How do I change the gradle version in spring boot?

You are going to need to update gradle, as newer spring boot versions are incompatible with older versions of gradle. You can either download the new gradle manually or use gradle wrapper to set the version for your project.

How do I add a spring boot to an existing Gradle project?

Adding Spring Boot Support Into Our Gradle ProjectAdd the Spring Boot Gradle plugin (version 1.2. 5. RELEASE) to the classpath of the build script. Apply the Spring Boot Gradle plugin.


3 Answers

What you are looking for is setting system properties on the Test task, which is what will run your unit tests:

test {
  systemProperty 'spring.profiles.active', 'development'
}

Edited after comment - leaving the original answer below as it may still be useful.

Gradle does not know the way bootRun exposes its system properties.

You thus have to add a configuration in your build script, to expose what you need to the Gradle command line.

Something like:

bootRun {
    bootRun.systemProperty 'spring.profiles.active', "${springProfile}"
}

and then have a default in gradle.properties:

springProfile = development

and possibly override the value on the command line:

./gradlew -PspringProfile=test build
like image 64
Louis Jacomet Avatar answered Oct 17 '22 07:10

Louis Jacomet


If you using gradle boot run you need to add this to your build.gradle file

bootRun {
    String activeProfile =  System.properties['spring.profiles.active']
    systemProperty "spring.profiles.active", activeProfile
}

and then at the time of building you can use gradle bootRun -Dspring.profiles.active=test

or to build you can use gradle build -Dspring.profiles.active=test

like image 5
Rahul Anand Avatar answered Oct 17 '22 07:10

Rahul Anand


Gradle files should not depend on your spring profile, unless you explicitly wish that thing.

To run your app with specific profile, use :

SPRING_PROFILES_ACTIVE=myprofile ./gradlew bootRun
like image 4
Eugene Avatar answered Oct 17 '22 07:10

Eugene