Promise<Void> (@NotNull final Collection<? extends Module> modules, @NotNull final ExternalLibraryDescriptor descriptor, @NotNull final DependencyScope scope) { List<String> defaultRoots = descriptor.getLibraryClassesRoots(); List<String> classesRoots; Module firstModule = ContainerUtil.getFirstItem(modules); if (!defaultRoots.isEmpty()) { LOG.assertTrue(firstModule != null); classesRoots = new LocateLibraryDialog(firstModule, defaultRoots, descriptor.getPresentableName()).showAndGetResult(); } else { String version = descriptor.getPreferredVersion(); String mavenCoordinates = descriptor.getLibraryGroupId() + ":" + descriptor.getLibraryArtifactId() + ":" + (version != null ? version : RepositoryLibraryDescription.ReleaseVersionId); RepositoryAttachDialog dialog = new RepositoryAttachDialog(myProject, mavenCoordinates, RepositoryAttachDialog.Mode.DOWNLOAD); if (!dialog.showAndGet()) { return Promises.rejectedPromise(); } RepositoryLibraryProperties libraryProperties = new RepositoryLibraryProperties(dialog.getSelectedLibraryDescriptor()); Collection<OrderRoot> roots = JarRepositoryManager.loadDependenciesModal(myProject, libraryProperties, dialog.getAttachSources(), dialog.getAttachJavaDoc(), dialog.getDirectoryPath(), null); if (roots.isEmpty()) { Messages.showErrorDialog(myProject, JavaUiBundle.message("dialog.mesage.0.was.not.loaded", descriptor.getPresentableName()), JavaUiBundle.message("dialog.title.failed.to.download.library")); return Promises.rejectedPromise(); } classesRoots = roots.stream() .filter(root -> root.getType() == OrderRootType.CLASSES) .map(root -> PathUtil.getLocalPath(root.getFile())) .collect(Collectors.toList()); } if (!classesRoots.isEmpty()) { String libraryName = classesRoots.size() > 1 ? descriptor.getPresentableName() : null; final List<String> urls = OrderEntryFix.refreshAndConvertToUrls(classesRoots); if (modules.size() == 1) { ModuleRootModificationUtil.addModuleLibrary(firstModule, libraryName, urls, Collections.emptyList(), scope); } else { WriteAction.run(() -> { Library library = LibraryUtil.createLibrary(LibraryTablesRegistrar.getInstance().getLibraryTable(myProject), descriptor.getPresentableName()); Library.ModifiableModel model = library.getModifiableModel(); for (String url : urls) { model.addRoot(url, OrderRootType.CLASSES); } model.commit(); for (Module module : modules) { ModuleRootModificationUtil.addDependency(module, library, scope, false); } }); } } return Promises.resolvedPromise(null); }