javagradleintellij-ideajetbrains-gateway

Unable to set system properties from intellij run configuration for a gradle task


Oh god this is driving me crazy for the past hours and nothing seems to work. I'm using Intellij 2024.3.3 Ultimate edition; more precisely I'm doing remote development. I have an application where the test execution for local environment requires a custom system property to be set. The project is using gradle and the gradle.file cannot be changed! I tried literally everything for intellij to set me a system property from JAVA_OPTS to JAVA_OPTIONS till VM options, System.getPropert("...") returns null every single time.

I tried adding JAVA_OPTS="-Dmy.awesome.property=plswork" to the environment variables in the run configuration for the integrationTest gradle task, to no avail. I tried the same with JAVA_OPTIONS but the outcome was the same. I tried adding "-Dmy.awesome.property=plswork" to VM options just to come back to the same conclusion.

var systemProperties = System.getProperties();
if (!systemProperties.containsKey(OVERRIDE)) {
    // always the case...
} else {
    // I actually want this!
}

Override is:

private static final String OVERRIDE = "my.awesome.property";

running the gradle task outside intellij works perfrectly fine and obviously without the override it also works just fine. How on earth one can configure a system property for a single very specific gradle task (I only need this property for this very specific integrationTest task, none else!) while using intellij?

What am I missing? I already went through every single similar question and tested all the solutions (which seemed applicable) to my case to no avail. Modifying the integration.gradle file might work, but I cannot do that as that comes from a different repo through submodules and an update will affect loads of other projects (not to mention it's not my team who manages that).


Solution

  • If you can write a file somewhere you can configure the test using an init script.

    Write an init script to configure your test task using a hook:

    // /somewhere/over/the/rainbow/init.gradle
    
    afterProject { project ->
        if (rootProject.name != "buildSrc") // Init scripts don't work with buildSrc
            project.tasks.withType(Test).configureEach {
                systemProperty("OVERRIDE", "my.awesome.init.script")
            }
    }
    

    Then pass it at the command line:

    $ ./gradlew test --init-script /somewhere/over/the/rainbow/init.gradle