javagradleintellij-ideajetbrains-gateway

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


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.getProperty("...") 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 perfectly 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 some similar questions and tested all applicable solutions, 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