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?
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.
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:
<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)