void (@NotNull Project project, @NotNull PlaybackContext context, @NotNull AsyncPromise<Void> promise, @NotNull GradleSettings gradleSettings, @NotNull ActionCallback callback) { Collection<GradleProjectSettings> projectsSettings = gradleSettings.getLinkedProjectsSettings(); List<String> projectsPaths = ContainerUtil.map(projectsSettings, ExternalProjectSettings::getExternalProjectPath); AtomicInteger gradleProjectsToRefreshCount = new AtomicInteger(projectsSettings.size()); StringBuilder projectsWithResolveErrors = new StringBuilder(); for (GradleProjectSettings settings : projectsSettings) { ImportSpecBuilder importSpecBuilder = new ImportSpecBuilder(project, GradleConstants.SYSTEM_ID); importSpecBuilder.callback(new ExternalProjectRefreshCallback() { private final ImportSpecBuilder.DefaultProjectRefreshCallback defaultCallback = new ImportSpecBuilder.DefaultProjectRefreshCallback(importSpecBuilder.build()); @Override public void onSuccess(@Nullable DataNode<ProjectData> externalProject) { assert externalProject != null; context.message("Gradle resolve finished for: " + externalProject.getData().getLinkedExternalProjectPath(), getLine()); SimpleMessageBusConnection connection = project.getMessageBus().simpleConnect(); connection.subscribe(ProjectDataImportListener.TOPIC, new ProjectDataImportListener() { @Override public void onFinalTasksFinished(String projectPath) { _onImportFinished(projectPath); } @Override public void onImportFailed(String projectPath, @NotNull Throwable failure) { _onImportFinished(projectPath); } private void _onImportFinished(String projectPath) { if (!projectsPaths.contains(projectPath)) return; connection.disconnect(); if (gradleProjectsToRefreshCount.decrementAndGet() == 0) { ApplicationManager.getApplication().invokeLater(() -> promise.setResult(null)); } } }); defaultCallback.onSuccess(externalProject); callback.setDone(); } @Override public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) { context.error("Gradle resolve failed for: " + settings.getExternalProjectPath() + ":" + errorMessage + ":" + errorDetails, getLine()); synchronized (projectsWithResolveErrors) { if (projectsWithResolveErrors.length() != 0) { projectsWithResolveErrors.append(", "); } projectsWithResolveErrors.append(String.format("'%s'", new File(settings.getExternalProjectPath()).getName())); } defaultCallback.onFailure(errorMessage, errorDetails); if (gradleProjectsToRefreshCount.decrementAndGet() == 0) { ApplicationManager.getApplication().invokeLater(() -> promise.setError(projectsWithResolveErrors.toString())); callback.reject("Gradle sync failed"); } } }); ExternalSystemUtil.refreshProject(settings.getExternalProjectPath(), importSpecBuilder); } }