private static DataNode<? extends ExternalEntityData> createDependencyDataNode(ExternalProjectDependency projectDependency,
                                                                                 @NotNull ProjectResolverContext resolverCtx,
                                                                                 @NotNull DataNode<? extends ExternalEntityData> ownerDataNode,
                                                                                 boolean isTransitiveDepsEmpty,
                                                                                 @NotNull Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap,
                                                                                 @NotNull ArtifactMappingService artifactMap,
                                                                                 AtomicInteger classpathOrderShift) {
    DataNode<? extends ExternalEntityData> resultDataNode = null;
    final ModuleData ownerModule = getOwnerModule(ownerDataNode);
    DependencyScope dependencyScope = getDependencyScope(projectDependency.getScope());

    Collection<ProjectDependencyInfo> projectDependencyInfos = new ArrayList<>();
    List<File> artifactsToKeepAsLibraries = new ArrayList<>();
    if (resolverCtx.getSettings() != null) {
      GradleExecutionWorkspace executionWorkspace = resolverCtx.getSettings().getExecutionWorkspace();
      ModuleData moduleData = executionWorkspace.findModuleDataByArtifacts(projectDependency.getProjectDependencyArtifacts());
      if (moduleData != null) {
        projectDependencyInfos.add(new ProjectDependencyInfo(moduleData, null, projectDependency.getProjectDependencyArtifacts()));
      }
    }
    if (projectDependencyInfos.isEmpty()) {

      Pair<DataNode<GradleSourceSetData>, ExternalSourceSet> projectPair;
      MultiMap<Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>, File> projectPairs =
        new MultiMap<>(new Reference2ObjectLinkedOpenHashMap<>());

      for (File file : projectDependency.getProjectDependencyArtifacts()) {
        ModuleMappingInfo mapping = artifactMap.getModuleMapping(ExternalSystemApiUtil.toCanonicalPath(file.getPath()));
        if (mapping != null) {
          for (String moduleId : mapping.getModuleIds()) {
            if (moduleId == null) continue;
            projectPair = sourceSetMap.get(moduleId);

            if (projectPair == null) continue;
            projectPairs.putValue(projectPair, file);
          }
          if (mapping.getHasNonModulesContent()) {
            artifactsToKeepAsLibraries.add(file);
          }
        }
      }

      for (Map.Entry<Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>, Collection<File>> entry : projectPairs.entrySet()) {
        projectDependencyInfos.add(new ProjectDependencyInfo(
          entry.getKey().first.getData(), entry.getKey().second, entry.getValue()));
      }

      String moduleIdFromDependency = getModuleId(projectDependency);
      Pair<DataNode<GradleSourceSetData>, ExternalSourceSet> projectPairFromMap = sourceSetMap.get(moduleIdFromDependency);
      if (projectPairFromMap != null) {
        if (doesNotContainDependencyOn(projectDependencyInfos, projectPairFromMap.first.getData())) {
          final Collection<File> artifacts = projectDependency.getProjectDependencyArtifacts();
          artifacts.removeAll(collectProcessedArtifacts(projectDependencyInfos));
          projectDependencyInfos.add(new ProjectDependencyInfo(projectPairFromMap.first.getData(),
                                                               projectPairFromMap.second,
                                                               artifacts));
        }
      }
    }

    if (projectDependencyInfos.isEmpty()) {
      LibraryDependencyData libraryDependencyData =
        createLibraryDependencyData(projectDependency, classpathOrderShift.get(), ownerModule, dependencyScope);

      if (!projectDependency.getProjectDependencyArtifacts().isEmpty()) {
        for (File artifact : projectDependency.getProjectDependencyArtifacts()) {
          libraryDependencyData.getTarget().addPath(LibraryPathType.BINARY, artifact.getPath());
        }
        resultDataNode = ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData);
      }
      else {
        resultDataNode = ownerDataNode;
      }
    }
    else {
      int i = 0;
      for (ProjectDependencyInfo projectDependencyInfo : projectDependencyInfos) {
        if (i++ > 0 && isTransitiveDepsEmpty) {
          classpathOrderShift.incrementAndGet();
        }
        ModuleDependencyData moduleDependencyData = new ModuleDependencyData(ownerModule, projectDependencyInfo.myModuleData);
        moduleDependencyData.setScope(dependencyScope);
        if (projectDependencyInfo.mySourceSet != null && isTestSourceSet(projectDependencyInfo.mySourceSet)) {
          moduleDependencyData.setProductionOnTestDependency(true);
        }
        moduleDependencyData.setOrder(projectDependency.getClasspathOrder() + classpathOrderShift.get());
        moduleDependencyData.setExported(projectDependency.getExported());
        moduleDependencyData.setModuleDependencyArtifacts(ContainerUtil.map(projectDependencyInfo.dependencyArtifacts, File::getPath));
        resultDataNode = ownerDataNode.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData);
      }

      for (File artifact : artifactsToKeepAsLibraries) {
        LibraryDependencyData data =
          createLibraryDependencyData(projectDependency, classpathOrderShift.incrementAndGet(), ownerModule, dependencyScope);
        data.getTarget().addPath(LibraryPathType.BINARY, artifact.getPath());
        ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, data);
      }

      // put transitive dependencies to the ownerDataNode,
      // since we can not determine from what project dependency artifact it was originated
      if (projectDependencyInfos.size() > 1) {
        resultDataNode = ownerDataNode;
      }
    }

    return resultDataNode;
  }
private static DataNode<? extends ExternalEntityData> createDependencyDataNode(ExternalProjectDependency projectDependency,
                                                                                 @NotNull ProjectResolverContext resolverCtx,
                                                                                 @NotNull DataNode<? extends ExternalEntityData> ownerDataNode,
                                                                                 boolean isTransitiveDepsEmpty,
                                                                                 @NotNull Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap,
                                                                                 @NotNull ArtifactMappingService artifactMap,
                                                                                 AtomicInteger classpathOrderShift)