Unfortunately this error can have multiple causes, but all caused due to the compatibility issues between the JDK used for compilation and the dependent libraries.
To learn the exact cause, run maven with -e or -X switch. This will produce the stack trace pointing the exact incompatibility issue. Post this, you can change the JDK version and/or upgrade/downgrade the library.
In my case it was lombok (trace below). I had to upgrade the version from 1.16.xx to 1.18.28 for it to compile.
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs accessible: module jdk.compiler does not "opens com.sun.tools.javac.processing" to unnamed module @4ed56cfd
at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:354)
at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:297)
at java.lang.reflect.Field.checkCanSetAccessible (Field.java:178)
at java.lang.reflect.Field.setAccessible (Field.java:172)
-> at lombok.javac.apt.LombokProcessor.getFieldAccessor (LombokProcessor.java:116)
-> at lombok.javac.apt.LombokProcessor. (LombokProcessor.java:108)
at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
...