Document how to enable assertions
I would like to know how to enable assert statements in an OpenLiberty application, for example for use within REST endpoints. Perhaps something like MAVEN_OPTS=-ea mvn liberty:dev should be recognized.
Thank you.
Hi @inad9300,
Are you talking about JUnit assertions? We do not provide JUnit in OL, you would have to package JUnit in your application.
@inad9300 If this is a JVM argument, you could try specifying it in a jvm.options file in src/main/liberty/config. That would get copied to the target Liberty server if you are using the default for configDirectory. Here is the documentation for the common server parameters.
Thanks, @cherylking. The "-ea" or "-enableassertions" switch is indeed a JVM argument, and with your trick I was able to effectively enable them.
Since I want to control the behavior depending on whether the application is running in production or not, I opted for using a Maven property, like so:
<properties>
<liberty.jvm.enableAssertions>-ea</liberty.jvm.enableAssertions>
</properties>
However, I am then unable to override that property by running mvn -Dliberty.jvm.enableAssertions= liberty:dev, which contradicts the explanation given in the documentation you linked:
Note that properties specified with
-Don the command line are also analyzed for the property name formats listed above and take precedence over Maven properties specified in the pom.xml.
The other way around is possible, though not ideal, i.e. having no Maven properties but running the application with mvn -Dliberty.jvm.enableAssertions=-ea liberty:dev works as expected.
To be honest, wanting assertions during development but not in production seems like the most sensible configuration to me, so I would propose this be the default. This Maven plugin is perfectly positioned to accomplish something like that: assertions could be enabled by default when running specifically "liberty:dev", while disabled when running other commands like "liberty:run", "liberty:package", etc. Please, implement something like this :pray:
@inad9300 Another option for you to control the JVM arguments for development and production environments would be to set up a jvm.options file outside the default configDirectory and specify the path to your new jvm.options file with the jvmOptionsFile parameter when running dev mode --- something like mvn liberty:dev -DjvmOptionsFile=path/to/jvm.options
Your suggestion to set the JVM argument to turn on assertions by default is an interesting one and you're the first to suggest something like this. We will give this some thought and discuss it further internally.
Opened https://github.com/OpenLiberty/ci.maven/issues/1565 to investigate whether we can change the impl to have the one liberty.jvm.xyz property override (or "unset" in this case) the other. That makes intuitive sense...we just have to scan through the complete merge algorithm to make sure this doesn't create any new difficulties.
Thank you for raising this point !