public static void addLibraryToRoots(@NotNull Module module, @NotNull Library library, @NotNull DependencyScope scope, boolean exported) {
    final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();

    final LibraryOrderEntry entry;
    if (library.getTable() == null) {
      final Library jarLibrary = rootModel.getModuleLibraryTable().createLibrary();
      final Library.ModifiableModel libraryModel = jarLibrary.getModifiableModel();
      for (OrderRootType orderRootType : OrderRootType.getAllTypes()) {
        VirtualFile[] files = library.getFiles(orderRootType);
        for (VirtualFile jarFile : files) {
          libraryModel.addRoot(jarFile, orderRootType);
        }
      }
      libraryModel.commit();
      entry = rootModel.findLibraryOrderEntry(jarLibrary);
    }
    else {
      entry = rootModel.addLibraryEntry(library);
    }

    if (entry != null) {
      entry.setScope(scope);
      entry.setExported(exported);
    }

    rootModel.commit();
  }
public static void addLibraryToRoots(@NotNull Module module, @NotNull Library library, @NotNull DependencyScope scope, boolean exported)