How to enable assertions in the Gradle run task
Asked Answered
S

2

9

By default, Java disables assertions. (The test I'm using here is assert false; as the first line in main().)

I'd like to have them enabled while running my program with gradle run. What's the trick for doing this?

Sangsanger answered 23/1, 2018 at 7:26 Comment(0)
C
13

There is a specific flag enableAssertions that you can set to enable assertions. Such flags are often more readable than working with the jvm args directly. It comes down to personal preferences I think.

Instead of working with applicationDefaultJvmArgs you can add the following to your build.gradle file:

run {
    enableAssertions = true
}

This configures the run task to enable assertions.

The run task is of type JavaExec, have a look at the documentation if you are interested in other parameters that you can set (e.g., workingDir or heapMaxSize).

Ceratoid answered 28/11, 2018 at 18:22 Comment(1)
Thanks for referring to the documentation where this can be found instead of just letting this be some magic/mystery/secret knowledge.Ricardo
C
3
tasks.withType(JavaExec) {
     enableAssertions = true
}
Corydalis answered 2/5, 2022 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.