Search code examples
javaintellij-ideaideexportrefactoring

How to export class file and all dependency file to other location


I'm in a situation where I need to copy class file to other location but this class file depends on another class files and can go up to 100+ files, which is impractical doing it manually.

For example, let's say I have the following classes:

class A {}

Class B depends on A:

import A;
class B {}

Class C depends on B (which depends on A)

import B;
class C {}

Say I need to copy class C file (say C.java), but in this case B.java and A.java should be copied as well.

I checked intellj which was able to detect these production classes dependency (ie classes I create - not external libraries), but got stuck moving them.

Question is, how can I automate this?


Solution

  • How to inspect all dependencies of class/package/module/custom scope

    In IntelliJ IDEA there is a tool to analyze all dependencies of class/package/module/custom scope: https://www.jetbrains.com/help/idea/dependencies-analysis.html#analyze-dependencies.

    "Right click on package/class/whatever in Project explorer > Analyze > Analyze Dependencies..."

    You will see a window in which you can mark "Analysis Options > Show transitive dependencies" and specify some number of maximum depth of transition.

    After that you will see Dependency Viewer: https://www.jetbrains.com/help/idea/dependencies-analysis.html#dependency-viewer. On the right side you can see all classes that considered scope depends to.

    How to export all dependencies of class/package/module/custom scope

    In Dependency Viewer I can not see any easy way to copy all files from the right side (dependency side). But the thing you can do is:

    1. Right click on dependency files on the right side you want to export.
    2. Click "Add to scope" in context menu. It will move files from the right side to left side.
    3. Click "Export to Text File" icon, which you can find on the top of Dependency Viewer: https://www.jetbrains.com/help/idea/dependencies-analysis.html#toolbar1
    4. It will generate XML file, which should looks similar to this:
        <root isBackward="false">
          <file path="$PROJECT_DIR$/path/to/file1>
            <dependency path="path/to/dependency1" />
            <dependency path="path/to/dependency2" />
            <dependency path="$path/to/dependency3" />
          </file>
          <file path="$PROJECT_DIR$/path/to/file2>
            <dependency path="path/to/dependency4" />
            <dependency path="path/to/dependency5" />
          </file>
        </root>
    

    You can write some script to iterate through all file elements in xml to copy files specified in path attribute from one project to another.

    For example it can be a python script as follows:

    import xml.etree.ElementTree as ET
    import os
    import shutil
    
    XML_DEPENDENCIES = "path_to_xml_file.xml"
    SCR_PROJECT_DIR = "path/to/source/project/directory"
    DESTINATION_DIR = "path/to/destination"
    
    tree = ET.parse(XML_DEPENDENCIES)
    root = tree.getroot()
    
    for file_element in root:
        path = file_element.get("path")
        source_file_path = path.replace("$PROJECT_DIR$", SCR_PROJECT_DIR)
        dest_dir_path = str(os.path.dirname(path)).replace("$PROJECT_DIR$", DESTINATION_DIR)
    
        if not os.path.exists(dest_dir_path):
            os.makedirs(dest_dir_path)
    
        shutil.copy(source_file_path, dest_dir_path)