Library (@NotNull ModifiableRootModel model, String libName, @NotNull List<? extends VirtualFile> classesRoots, @NotNull List<? extends VirtualFile> sourceRoots, @NotNull List<? extends VirtualFile> javaDocs, @NotNull List<? extends VirtualFile> externalAnnotationsRoots) { LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(model.getProject()); return WriteAction.computeAndWait(() -> { Library library = libraryTable.createLibrary(libName); Library.ModifiableModel libraryModel = library.getModifiableModel(); try { for (VirtualFile root : classesRoots) { libraryModel.addRoot(root, OrderRootType.CLASSES); } for (VirtualFile root : sourceRoots) { libraryModel.addRoot(root, OrderRootType.SOURCES); } for (VirtualFile root : javaDocs) { libraryModel.addRoot(root, JavadocOrderRootType.getInstance()); } for (VirtualFile root : externalAnnotationsRoots) { libraryModel.addRoot(root, AnnotationOrderRootType.getInstance()); } libraryModel.commit(); } catch (Throwable t) { //noinspection SSBasedInspection libraryModel.dispose(); throw t; } model.addLibraryEntry(library); OrderEntry[] orderEntries = model.getOrderEntries(); OrderEntry last = orderEntries[orderEntries.length - 1]; System.arraycopy(orderEntries, 0, orderEntries, 1, orderEntries.length - 1); orderEntries[0] = last; model.rearrangeOrderEntries(orderEntries); return library; }); }