Excluding Imports from IntelliJ

July 09, 2017

We can exclude unwanted classes and packages from ever being suggested by IntelliJ IDEA. See official docs.

Context

It’s a lot easier to develop consistently fast when errors are prevented early.

We know how to fail the build when either an unwanted class or unwanted package has been imported. The build is the best place to implement this kind of safety-net behavior for a couple reasons:

  1. No setup required for other contributors to the project
  2. CI will always run the same checks, preventing these errors from making it to test or production environments.

Improving the Developer Experience

Waiting for a build to find out that an unwanted class has been used is frustrating. Fortunately we can set up project-specific or global auto import excludes in Preferences | Editor | General | Auto Import:

IntelliJ Auto Import Excludes

This can also be added on-the-fly when importing a new class:

Import Class Dialog

Note There is not currently a plugin that can sync the Checkstyle IllegalImport rule and IntelliJ import exclusions, so watch for these getting out of sync.


Profile picture

Written by @sghill, who works on build, automated change, and continuous integration systems.

© 2010-2022, Built with Gatsby