javaspring-bootgradlespring-profilesgradle-task

Gradle systemProperty in custom task not setting Spring Profile


I'm trying to selectively deactive tests if there is a spring profile called "unit", which is working:

@DisabledIfEnvironmentVariable(named = "SPRING_PROFILES_ACTIVE", matches = "unit")

If I run SPRING_PROFILES_ACTIVE=unit ./gradlew clean test this works fine. The issue I'm having is that I don't want to have to write all that out, and instead I want a custom gradle task, also called "unit". It currently looks like this:

task unit(type: Test) {
    useJUnitPlatform()
    systemProperty "SPRING_PROFILES_ACTIVE", "unit"
}

This runs all tests, even if they are supposed to be disabled. I tried useJUnitPlatform { sysProp... }, I tried adding options { sysProp... }, I tried using doFirst, I tried switching "SPRING_PROFILES_ACTIVE" to "spring.profiles.active" in both the build.gradle and in the annotation, I tried systemProperties "SPRING_PROFILES_ACTIVE": "unit" but that doesn't work.

I found lots of StackOverflow questions where people just put systemProperties into the default test task, but I couldn't find anything with a custom task.

What do I need to call where to get the task to pass a spring profile system property?


Solution

  • @DisabledIfEnvironmentVariable annotation requires environment variable to be set. Try to set environment variable instead of system property in Your task:

    task unit(type: Test) {
        useJUnitPlatform()
        environment "SPRING_PROFILES_ACTIVE", "unit"
    }