private static boolean hasGradleModules(@NotNull CompileContext context) {
   for (Module module : context.getCompileScope().getAffectedModules()) {
     if (ExternalSystemApiUtil.isExternalSystemAwareModule(GradleConstants.SYSTEM_ID, module))
       return true;
   }
   return false;
 }
 @Override
 protected boolean isApplicableFor(@NotNull RunConfigurationBase configuration) {
   if (configuration instanceof ModuleBasedConfiguration
       && configuration instanceof CommonProgramRunConfigurationParameters) {
     final RunConfigurationModule runConfigurationModule =
         ((ModuleBasedConfiguration) configuration).getConfigurationModule();
     return ExternalSystemApiUtil.isExternalSystemAwareModule(
         GradleConstants.SYSTEM_ID, runConfigurationModule.getModule());
   }
   return false;
 }
  @NotNull
  private Map<String, GradleModuleResourceConfiguration> generateAffectedGradleModulesConfiguration(
      @NotNull CompileContext context) {
    final Map<String, GradleModuleResourceConfiguration> affectedGradleModuleConfigurations =
        ContainerUtil.newTroveMap();

    //noinspection MismatchedQueryAndUpdateOfCollection
    final Map<String, ExternalProject> lazyExternalProjectMap =
        new FactoryMap<String, ExternalProject>() {
          @Nullable
          @Override
          protected ExternalProject create(String gradleProjectPath) {
            return myExternalProjectDataService.getRootExternalProject(
                GradleConstants.SYSTEM_ID, new File(gradleProjectPath));
          }
        };

    for (Module module : context.getCompileScope().getAffectedModules()) {
      if (!ExternalSystemApiUtil.isExternalSystemAwareModule(GradleConstants.SYSTEM_ID, module))
        continue;

      if (shouldBeBuiltByExternalSystem(module)) continue;

      final String gradleProjectPath =
          module.getOptionValue(ExternalSystemConstants.ROOT_PROJECT_PATH_KEY);
      assert gradleProjectPath != null;
      final ExternalProject externalRootProject = lazyExternalProjectMap.get(gradleProjectPath);
      if (externalRootProject == null) {
        context.addMessage(
            CompilerMessageCategory.ERROR,
            String.format(
                "Unable to make the module: %s, related gradle configuration was not found. "
                    + "Please, re-import the Gradle project and try again.",
                module.getName()),
            VfsUtilCore.pathToUrl(gradleProjectPath),
            -1,
            -1);
        continue;
      }

      ExternalProject externalProject =
          myExternalProjectDataService.findExternalProject(externalRootProject, module);
      if (externalProject == null) {
        LOG.warn("Unable to find config for module: " + module.getName());
        continue;
      }

      GradleModuleResourceConfiguration resourceConfig = new GradleModuleResourceConfiguration();
      resourceConfig.id =
          new ModuleVersion(
              externalProject.getGroup(), externalProject.getName(), externalProject.getVersion());
      resourceConfig.directory =
          FileUtil.toSystemIndependentName(externalProject.getProjectDir().getPath());

      final ExternalSourceSet mainSourcesSet = externalProject.getSourceSets().get("main");
      addResources(resourceConfig.resources, mainSourcesSet, ExternalSystemSourceType.RESOURCE);

      final ExternalSourceSet testSourcesSet = externalProject.getSourceSets().get("test");
      addResources(
          resourceConfig.testResources, testSourcesSet, ExternalSystemSourceType.TEST_RESOURCE);

      final CompilerModuleExtension compilerModuleExtension =
          CompilerModuleExtension.getInstance(module);
      if (compilerModuleExtension != null
          && compilerModuleExtension.isCompilerOutputPathInherited()) {
        String outputPath = VfsUtilCore.urlToPath(compilerModuleExtension.getCompilerOutputUrl());
        for (ResourceRootConfiguration resource : resourceConfig.resources) {
          resource.targetPath = outputPath;
        }

        String testOutputPath =
            VfsUtilCore.urlToPath(compilerModuleExtension.getCompilerOutputUrlForTests());
        for (ResourceRootConfiguration resource : resourceConfig.testResources) {
          resource.targetPath = testOutputPath;
        }
      }

      affectedGradleModuleConfigurations.put(module.getName(), resourceConfig);
    }

    return affectedGradleModuleConfigurations;
  }
  @Override
  public void computeInReadAction(@NotNull ProgressIndicator indicator) {
    if (myProject.isDisposed()) return;
    if (ApplicationManager.getApplication().isUnitTestMode()) return;

    final LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
    final List<PsiFile> psiFileList = ContainerUtil.newArrayList();

    final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    for (Module module : moduleManager.getModules()) {
      if (!ExternalSystemApiUtil.isExternalSystemAwareModule(GradleConstants.SYSTEM_ID, module))
        continue;

      final String modulePath = ExternalSystemApiUtil.getExternalProjectPath(module);
      if (modulePath == null) continue;

      String buildScript =
          FileUtil.findFileInProvidedPath(modulePath, GradleConstants.DEFAULT_SCRIPT_NAME);
      if (StringUtil.isEmpty(buildScript)) continue;

      VirtualFile virtualFile = localFileSystem.refreshAndFindFileByPath(buildScript);
      if (virtualFile == null) continue;

      final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(virtualFile);
      if (psiFile == null) continue;
      psiFileList.add(psiFile);
    }

    final PsiFile[] psiFiles = ArrayUtil.toObjectArray(psiFileList, PsiFile.class);

    final Set<MavenRemoteRepository> mavenRemoteRepositories =
        new ReadAction<Set<MavenRemoteRepository>>() {
          @Override
          protected void run(@NotNull Result<Set<MavenRemoteRepository>> result) throws Throwable {
            Set<MavenRemoteRepository> myRemoteRepositories = ContainerUtil.newHashSet();
            for (PsiFile psiFile : psiFiles) {
              List<GrClosableBlock> repositoriesBlocks = ContainerUtil.newArrayList();
              repositoriesBlocks.addAll(findClosableBlocks(psiFile, "repositories"));

              for (GrClosableBlock closableBlock :
                  findClosableBlocks(
                      psiFile,
                      "buildscript",
                      "subprojects",
                      "allprojects",
                      "project",
                      "configure")) {
                repositoriesBlocks.addAll(findClosableBlocks(closableBlock, "repositories"));
              }

              for (GrClosableBlock repositoriesBlock : repositoriesBlocks) {
                myRemoteRepositories.addAll(findMavenRemoteRepositories(repositoriesBlock));
              }
            }

            result.setResult(myRemoteRepositories);
          }
        }.execute().getResultObject();

    if (mavenRemoteRepositories == null || mavenRemoteRepositories.isEmpty()) return;

    // register imported maven repository URLs but do not force to download the index
    // the index can be downloaded and/or updated later using Maven Configuration UI (Settings ->
    // Build, Execution, Deployment -> Build tools -> Maven -> Repositories)
    MavenRepositoriesHolder.getInstance(myProject).update(mavenRemoteRepositories);
    MavenProjectIndicesManager.getInstance(myProject)
        .scheduleUpdateIndicesList(
            new Consumer<List<MavenIndex>>() {
              @Override
              public void consume(List<MavenIndex> indexes) {
                if (myProject.isDisposed()) return;

                final List<String> repositoriesWithEmptyIndex =
                    ContainerUtil.mapNotNull(
                        indexes,
                        new Function<MavenIndex, String>() {
                          @Override
                          public String fun(MavenIndex index) {
                            return index.getUpdateTimestamp() == -1
                                    && MavenRepositoriesHolder.getInstance(myProject)
                                        .contains(index.getRepositoryPathOrUrl())
                                ? index.getRepositoryPathOrUrl()
                                : null;
                          }
                        });

                if (!repositoriesWithEmptyIndex.isEmpty()) {
                  final NotificationData notificationData =
                      new NotificationData(
                          GradleBundle.message(
                              "gradle.integrations.maven.notification.not_updated_repository.title"),
                          "\n<br>"
                              + GradleBundle.message(
                                  "gradle.integrations.maven.notification.not_updated_repository.text",
                                  StringUtil.join(repositoriesWithEmptyIndex, "<br>")),
                          NotificationCategory.WARNING,
                          NotificationSource.PROJECT_SYNC);
                  notificationData.setBalloonNotification(true);
                  notificationData.setBalloonGroup(UNINDEXED_MAVEN_REPOSITORIES_NOTIFICATION_GROUP);
                  notificationData.setListener(
                      "#open",
                      new NotificationListener.Adapter() {
                        @Override
                        protected void hyperlinkActivated(
                            @NotNull Notification notification, @NotNull HyperlinkEvent e) {
                          ShowSettingsUtil.getInstance()
                              .showSettingsDialog(myProject, MavenRepositoriesConfigurable.class);
                        }
                      });

                  notificationData.setListener(
                      "#disable",
                      new NotificationListener.Adapter() {
                        @Override
                        protected void hyperlinkActivated(
                            @NotNull Notification notification, @NotNull HyperlinkEvent e) {
                          final int result =
                              Messages.showYesNoDialog(
                                  myProject,
                                  "Notification will be disabled for all projects.\n\n"
                                      + "Settings | Appearance & Behavior | Notifications | "
                                      + UNINDEXED_MAVEN_REPOSITORIES_NOTIFICATION_GROUP
                                      + "\ncan be used to configure the notification.",
                                  "Unindexed Maven Repositories Gradle Detection",
                                  "Disable Notification",
                                  CommonBundle.getCancelButtonText(),
                                  Messages.getWarningIcon());
                          if (result == Messages.YES) {
                            NotificationsConfigurationImpl.getInstanceImpl()
                                .changeSettings(
                                    UNINDEXED_MAVEN_REPOSITORIES_NOTIFICATION_GROUP,
                                    NotificationDisplayType.NONE,
                                    false,
                                    false);

                            notification.hideBalloon();
                          }
                        }
                      });

                  ExternalSystemNotificationManager.getInstance(myProject)
                      .showNotification(GradleConstants.SYSTEM_ID, notificationData);
                }
              }
            });
  }