javaintellij-ideaannotationsexternal-annotations

Change external annotations location in IDEA 2019.3+


IntelliJ IDEA lets you add external annotations.

enter image description here

However, the files go straight into the project's root directory.

enter image description here

How do I change the folder?
I can't find a setting.


Solution

  • This behavior is extremely confusing, and took me a long IDEA debug session to understand.
    Let's say we start with a clean situation, and we add an external Annotation to a method of a library.

    enter image description here

    A popup appears, letting us choose the external Annotations root directory.

    enter image description here

    And we choose untitled3, the project's root directory. But ops, we made a mistake!
    Now how can we change this directory? Where is it stored?
    Well, actually not under any $projectRoot$/.idea/* XML file...

    Let's debug IDEA. The "call" hierarchy is:

    > AnnotateIntentionAction
      > AddAnnotationPsiFix#invoke
        > ExternalAnnotationsManagerImpl#annotateExternally
    

    The last method annotateExternally calls AnnotationOrderRootType.getFiles(entry), where entry is a LibraryOrderEntry.

    enter image description here

    Here orderEntry.getRootFiles(getInstance()) gets called and the resulting VirtualFile(s) are used as library's external Annotations roots.

    Where are they stored?

    > Project structure
      > Modules
        > *your main module with dependencies*
          > *your library*
            > DOUBLE LEFT CLICK
    

    enter image description here

    Now press CANC over the Annotations entry, and you're done... Finally.
    You can restart over.