Does the @NotNull annotations work in Maven or not?
I have seen many conflicting statements about this and need a clear answer. What I have noticed:
I have a code base that uses @NotNull etc. and JUnit5 tests that validate what exception is thrown when passing in null to methods with this annotation for the parameters. When I run the test in InteliJ it throws IllegalArgumentException (as I understand the annotation is suposed to generate code that do) but when I run the same tests using maven I do not get this excpetion thrown (instead my code typically throws a NullPointerException probably later into the method as something fail when a null value is passed in).
According to some sources the required code generation for checking/throwing IllegalArgumentException is suposed to work also in Maven as lopng as the org.jetbrains.annotations dependency is included in the POM file but in my case this do not help i.e. I have this dependency in place already but no luck…
I need a confirmation one way or the other and if it is supposed to work any sugestions about how I can solve or further diagnose the problem is much appreciated.
Please sign in to leave a comment.
Can also mention that I use IntedliJ idea Build #IU-251.26927.53, Apache Maven 3.8.7 and JDK 24 for my tests.
There should be some difference in the IDEA runner and Maven runner. Please provide a minimal sample project so I can reproduce the issue without including your sensitive code. Thank you!
Please upload it to https://uploads.jetbrains.com/ and paste the upload id here, the uploaded file is only visible to JetBrains employees.
Maven build does not apply bytecode enhancements to enforce @NotNull unlike IDEA compiler. So unfortunately any code compiled not by IDEA would not enforce @NotNull at runtime
there is a community maven plugin (github) that does it but it is not being maintained. someone forked it and adopted to JDK 17 and I forked and updated for JDK 21 (or 21 and 23 do not remember) without really diving into details thankfully it worked
I posted a few times and as I recall created a ticket to have JetBrains provide an official compiler plugin to do bytecode enhancement (or take over this project which I believe was originally done by a jetbrainer) but it did not get any traction
Thanks for the answer - I will try your project. Would indeed really be great if IDEA would maintain the required plugin freeing you from this task….
It is not my plugin see it at https://github.com/osundblad/intellij-annotations-instrumenter-maven-plugin
what I was saying is that it is no longer maintained so if you want to use it with JDK > 17 yuo need to fork it and make some changes and push it to your own maven repo
see https://github.com/osundblad/intellij-annotations-instrumenter-maven-plugin/pull/62
it's a good plugin sadly JetBrains does not seem to be interested in having official one or perhaps taking over this one
Hello, we have a feature request about this: IDEA-226562 Official @NotNull instrumenter maven and gradle plugin, please vote/star it.
You may also try the workaround here to see if it helps:
https://youtrack.jetbrains.com/articles/SUPPORT-A-2286/How-to-Instrument-JetBrains-Annotations-with-Maven
Disapointying that this was brought up as an improvement 5 years ago and still not implemented - feels like a very natural feature to ensure annotations works no matter how you are buiulding this is after all Java with all types of portability as a key strength.
Frankly quite dangerous with null checks that silently dont work if code is not compiled with InteliJ - could lead to serious problems.
I also find the documentation not verey clear on this point - this was why I had to ask this question…