How to Give System Property to My Test via Gradle and -D

Passing system properties from command line to my app via gradle

systemProperties needs to go inside javaexec:

task cucumber(type: JavaExec) {
dependsOn assemble, compileTestJava
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['-f', 'pretty', '--glue', 'steps', 'src/test/resources']
systemProperties = System.getProperties()
}

PS: Unless you absolutely need to combine multiple concerns into a single task, always prefer task types (e.g. JavaExec) over methods (e.g. javaexec).

How to set system property using gradle?

The system property set in gradle.properties will be available only in JVM where Gradle is running.

From gradle-appengine-plugin documentation:

appengineRun: Starts a local development server running your project
code.

jvmFlags: The JVM flags to pass on to the local development server.

If you need your system properties to be available in app engine, which is separate JVM, you should use jvmFlags property.

Explicitly:

appengine {
jvmFlags = ['-DfirstName=Marko', '-DlastName=Vuksanovic']
}

With gradle.properties:

appengine {
jvmFlags = ['-DfirstName=$firstName', '-DlastName=$lastName']
}

How can I add a Java system property during JUnit Test Execution

Sorry to answer my own question. Just stumbled upon the solution here:

test {
systemProperties = System.properties
}

Is it possible to configure system properties dynamically for a Gradle test task?

I have found a way to make this work. The trick is to set the system property on the test task sometime later, when the property is certain to be available. The simplest way to make that happen seems to be via a dummy dependency:

project.task("integrationtestconfig") << {
outputs.upToDateWhen { false }
project.tasks.integrationtest.systemProperty("integration.test.server.wait", project.gwt.getServerWait())
}

project.task("integrationtest", type: org.gradle.api.tasks.testing.Test,
dependsOn: project.tasks.buildApplication, project.tasks.integrationtestconfig)
...

It's not the elegant solution I was hoping for, but it does work and it's not too difficult to follow.

Gradle systemProperty in custom task not setting Spring Profile

@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"
}


Related Topics



Leave a reply



Submit