ActionCallback (@NotNull PsiFile psiFile, @NotNull String sourceArtifactNotation, @NotNull String artifactCoordinates, @NotNull String externalProjectPath) { final String sourcesLocationFilePath; final File sourcesLocationFile; try { sourcesLocationFile = new File(FileUtil.createTempDirectory("sources", "loc"), "path.tmp"); sourcesLocationFilePath = StringUtil.escapeBackSlashes(sourcesLocationFile.getCanonicalPath()); Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtil.delete(sourcesLocationFile), "GradleAttachSourcesProvider cleanup")); } catch (IOException e) { GradleLog.LOG.warn(e); return ActionCallback.REJECTED; } Project project = psiFile.getProject(); String taskName = "ijDownloadSources" + UUID.randomUUID().toString().substring(0, 12); ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings(); settings.setExecutionName(getName()); settings.setExternalProjectPath(externalProjectPath); settings.setTaskNames(List.of(taskName)); settings.setVmOptions(GradleSettings.getInstance(project).getGradleVmOptions()); settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId()); UserDataHolderBase userData = prepareUserData(sourceArtifactNotation, taskName, sourcesLocationFilePath); final ActionCallback resultWrapper = new ActionCallback(); ExternalSystemUtil.runTask( settings, DefaultRunExecutor.EXECUTOR_ID, project, GradleConstants.SYSTEM_ID, new TaskCallback() { @Override public void onSuccess() { File sourceJar; try { Path downloadedArtifactPath = Path.of(FileUtil.loadFile(sourcesLocationFile)); if (!isValidJar(downloadedArtifactPath)) { GradleLog.LOG.warn("Incorrect file header: " + downloadedArtifactPath + ". Unable to process downloaded file as a JAR file"); FileUtil.delete(sourcesLocationFile); resultWrapper.setRejected(); return; } sourceJar = downloadedArtifactPath.toFile(); FileUtil.delete(sourcesLocationFile); } catch (IOException e) { GradleLog.LOG.warn(e); resultWrapper.setRejected(); return; } attachSources(sourceJar, orderEntries); resultWrapper.setDone(); } @Override public void onFailure() { resultWrapper.setRejected(); String title = GradleBundle.message("gradle.notifications.sources.download.failed.title"); String message = GradleBundle.message("gradle.notifications.sources.download.failed.content", artifactCoordinates); NotificationData notification = new NotificationData(title, message, NotificationCategory.WARNING, NotificationSource.PROJECT_SYNC); notification.setBalloonNotification(true); ExternalSystemNotificationManager.getInstance(project).showNotification(GradleConstants.SYSTEM_ID, notification); } }, ProgressExecutionMode.NO_PROGRESS_ASYNC, false, userData); return resultWrapper; }