void (@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) { ExternalProject externalProject = getExternalProject(gradleModule, resolverCtx); Set<String> sourceSetContentRoots = Set.of(); if (externalProject != null) { sourceSetContentRoots = addExternalProjectContentRoots(gradleModule, ideModule, externalProject); } MutablePrefixTreeMap<String, ContentRootData> contentRootIndex = CanonicalPathPrefixTreeFactory.INSTANCE.createMap(); for (DataNode<ContentRootData> contentRootDataNode : ExternalSystemApiUtil.findAll(ideModule, ProjectKeys.CONTENT_ROOT)) { ContentRootData contentRootData = contentRootDataNode.getData(); contentRootIndex.set(contentRootData.getRootPath(), contentRootData); } DomainObjectSet<? extends IdeaContentRoot> contentRoots = gradleModule.getContentRoots(); if (contentRoots == null) return; for (IdeaContentRoot gradleContentRoot : contentRoots) { if (gradleContentRoot == null) continue; File rootDirectory = gradleContentRoot.getRootDirectory(); if (rootDirectory == null) continue; boolean oldGradle = false; String contentRootPath = FileUtil.toCanonicalPath(rootDirectory.getPath()); // don't add content root if one of source sets already have this content root boolean sameAsSourceSetContentRoot = sourceSetContentRoots.contains(contentRootPath); if (!sameAsSourceSetContentRoot) { ContentRootData ideContentRoot = new ContentRootData(GradleConstants.SYSTEM_ID, contentRootPath); contentRootIndex.set(contentRootPath, ideContentRoot); Set<File> excluded = gradleContentRoot.getExcludeDirectories(); if (excluded != null) { for (File file : excluded) { ideContentRoot.storePath(ExternalSystemSourceType.EXCLUDED, file.getPath()); } } } boolean sourceSetsAreNotConfigured = sourceSetContentRoots.isEmpty(); if (!resolverCtx.isResolveModulePerSourceSet() || sourceSetsAreNotConfigured) { List<? extends IdeaSourceDirectory> sourceDirectories = gradleContentRoot.getSourceDirectories().getAll(); List<? extends IdeaSourceDirectory> testDirectories = gradleContentRoot.getTestDirectories().getAll(); List<? extends IdeaSourceDirectory> resourceDirectories = Collections.emptyList(); List<? extends IdeaSourceDirectory> testResourceDirectories = Collections.emptyList(); try { final Set<File> notResourceDirs = collectExplicitNonResourceDirectories(externalProject); resourceDirectories = gradleContentRoot.getResourceDirectories().getAll(); removeDuplicateResources(sourceDirectories, resourceDirectories, notResourceDirs); testResourceDirectories = gradleContentRoot.getTestResourceDirectories().getAll(); removeDuplicateResources(testDirectories, testResourceDirectories, notResourceDirs); } catch (UnsupportedMethodException e) { oldGradle = true; // org.gradle.tooling.model.idea.IdeaContentRoot.getResourceDirectories/getTestResourceDirectories methods supported since Gradle 4.7 LOG.debug(e.getMessage()); if (externalProject == null) { populateContentRoot(contentRootIndex, ExternalSystemSourceType.SOURCE, gradleContentRoot.getSourceDirectories()); populateContentRoot(contentRootIndex, ExternalSystemSourceType.TEST, gradleContentRoot.getTestDirectories()); } } if (!oldGradle) { populateContentRoot(contentRootIndex, ExternalSystemSourceType.SOURCE, sourceDirectories); populateContentRoot(contentRootIndex, ExternalSystemSourceType.TEST, testDirectories); populateContentRoot(contentRootIndex, ExternalSystemSourceType.RESOURCE, resourceDirectories); populateContentRoot(contentRootIndex, ExternalSystemSourceType.TEST_RESOURCE, testResourceDirectories); } } } Set<String> existsContentRoots = new LinkedHashSet<>(); for (DataNode<ContentRootData> contentRootDataNode : ExternalSystemApiUtil.findAll(ideModule, ProjectKeys.CONTENT_ROOT)) { ContentRootData contentRootData = contentRootDataNode.getData(); existsContentRoots.add(contentRootData.getRootPath()); } for (ContentRootData ideContentRoot : contentRootIndex.values()) { if (!existsContentRoots.contains(ideContentRoot.getRootPath())) { ideModule.createChild(ProjectKeys.CONTENT_ROOT, ideContentRoot); } } }