How to get the dependency tree of gradle project

已回答

I found a post   https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006872339-Get-Gradle-project-information-in-plugin

 

But I can't get through MODULE_DEPENDENCY and LIBRARY_DEPENDENCY, they are both empty

But I can get it through child.getKey().getDataType().equals(ProjectDependencies.class.getName()) (I tried ProjectKeys.DEPENDENCIES_GRAPH but that also returns empty)

Below is my code

I want to do two things
1. Get the dependency tree
2. Jump to the location of the corresponding implementation according to the dependency name

 for (Module module : ModuleManager.getInstance(project).getModules()) {
     DataNode<ModuleData> moduleNode = GradleUtil.findGradleModuleData(module);
     if (moduleNode != null) {
         for (DataNode<?> child : moduleNode.getChildren()) {
             if (child.getKey().getDataType().equals(ProjectDependencies.class.getName())){
                 for (ComponentDependencies componentsDependency : ((ProjectDependencies) child.getData()).getComponentsDependencies()) {
                     for (DependencyNode dependency : componentsDependency.getRuntimeDependenciesGraph().getDependencies()) {
                         System.out.println(dependency.getDisplayName());
                     }
                 }
             }
         }
         ProjectDependencies data = moduleNode.getData(ProjectKeys.DEPENDENCIES_GRAPH);
         if (data!=null){
             for (ComponentDependencies componentsDependency : data.getComponentsDependencies()) {
                 for (DependencyNode dependency : componentsDependency.getRuntimeDependenciesGraph().getDependencies()) {
                     System.out.println(dependency.getDependencies());
                 }
             }
         }
           for (DataNode<?> child : moduleNode.getChildren()) {
               Key<?> key = child.getKey();
               if (key.equals(ProjectKeys.MODULE_DEPENDENCY) || key.equals(ProjectKeys.LIBRARY_DEPENDENCY)) {
                   System.out.println(child);
               }
           }
     }
 }
0

Hi,

Please try using:

com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil#getChildren

instead of:

com.intellij.openapi.externalSystem.model.DataNode#getData(com.intellij.openapi.externalSystem.model.Key<T>)

The first finds all direct children with the specified data node's key. The second returns data in the current node or in their parent.

 

0

请先登录再写评论。