Run Gradle Task With Spring Profiles (Integration Tests)

How to run bootRun with spring profile via gradle task

Simplest way would be to define default and allow it to be overridden. I am not sure what is the use of systemProperty in this case. Simple arguments will do the job.

def profiles = 'prod'

bootRun {
args = ["--spring.profiles.active=" + profiles]
}

To run dev:

./gradlew bootRun -Pdev

To add dependencies on your task you can do something like this:

task setDevProperties(dependsOn: bootRun) << {
doFirst {
System.setProperty('spring.profiles.active', profiles)
}
}

There are lots of ways achieving this in Gradle.

Edit:

Configure separate configuration files per environment.

if (project.hasProperty('prod')) {
apply from: 'gradle/profile_prod.gradle'
} else {
apply from: 'gradle/profile_dev.gradle'
}

Each configuration can override tasks for example:

def profiles = 'prod'
bootRun {
systemProperty "spring.profiles.active", activeProfile
}

Run by providing prod flag in this case just like that:

./gradlew <task> -Pprod

Best way to decouple integration test cases from build (gradle spring-boot)

First of all, welcome to the community.

Next, you can modify the test task inside the build.gradle file or maybe add a new task called integrationTest and implement your custom logic there.

As an instance, you can check this gist on Github: Separating tests from integration tests with Gradle

You can also use @Profile annotation to your integration test classes and run your tests with different profiles. You can read more about profiles using the following link: Spring Profiles

how to set active profile for gradle build of spring boot application?

If your requirement is that you control this externally (i.e., via the command line when launching Gradle), you can then modify your Gradle test task configuration as follows.

test {
systemProperty("spring.profiles.active", project.properties.get("springProfiles"))
// ...
}

And then you can set a value for springProfiles like this: gradlew clean build -PspringProfiles=ci (where ci is the name of the profile you want active on the CI server).

The above will make spring.profiles.active available as a JVM system property for your tests; however, you'd still need to set the active profiles for the Spring TestContext Framework.

To do that, you need to annotate your test class with @ActiveProfiles, but instead of passing in static profiles you'd need to implement a custom ActiveProfilesResolver and register it via @ActiveProfiles(resolver = MyCustomResolver.class). Your customer resolver could then read then simply return the value of the spring.profiles.active system property.

Another option is to implement a custom ApplicationContextInitializer that programmatically sets the active profiles (similar to the custom ActiveProfilesResolver). You can configure one of those via @SpringApplicationConfiguration(initializers = MyCustomInitializer.class).

And yet another option would be to programmatically set the active profiles directly in your SpringApplication -- for example, based on a system property or environment variable.

So, you have several options.



Related Topics



Leave a reply



Submit