stackoverflow.com/a/4557275/2809617
You may want to look into configuring different artifacts
https://www.jetbrains.com/idea/help/artifacts.html
for the various versions of the lib and then restructuring your code to isolate the version specific code in subclasses/components that are only including in the relevant artifact. This may be impractical, especially in an existing project, but less trouble than writing a plugin. Maven has similar functionality via classifiers.