Question
How can add multiple modules at once in IntelliJ IDEA? Users may need to add several modules at once from a directory containing various Java projects. Each of these folders is named by the username-surname_firstname convention. However, IntelliJ doesn't treat every folder as a separate project, producing compile failures due to duplicate classes. The issue arises when selecting multiple folders to add. Instead of adding them as individual modules, IntelliJ treats them as source folders. With many projects, this process isn't efficient when done manually for each folder. How can resolve this issue?
Answer
- Create a Java program to set your folder into a large Gradle project. However, issues may arise with Unicode path encoding, preventing some folders from being loaded in the final
settings.gradle.ktsfile.
Here is an example of a script to accomplish this:
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static final String BUILD_GRADLE_KTS = "build.gradle.kts";
public static void main(String[] args) {
String inputDir = "/Users/xxx/your_project_root";
StringBuilder settings_gradle_kts = new StringBuilder("rootProject.name = \"myproject\"\n");
List<String> projectNames = new ArrayList<>();
String build_gralde =
"""
plugins {
id("java")
}
java {
sourceSets {
main {
java.srcDirs("src/main/java")
// resources.srcDirs("src/main/resources")
}
}
}
""";
File rootDir = new File(inputDir);
File[] projects = rootDir.listFiles();
// Root dir build.gradle
writeGradleBuildFile(build_gralde, rootDir, BUILD_GRADLE_KTS);
for (int i = 1; i < projects.length; i++) {
File prjFile = projects[i];
if(!prjFile.isDirectory()) continue;
String prjFolder = projects[i].getName();
System.out.println(prjFolder);
if(prjFolder.startsWith(".")) { // skip .idea, .gradle etc
continue;
}
projectNames.add(prjFolder);
settings_gradle_kts.append("include(\"%s\")\n".formatted(prjFolder));
File[] prjJavaFiles = new File(inputDir + "/" + prjFolder).listFiles(
(dir, name) -> name.endsWith(".java"));
File srcDir = new File(inputDir + "/" + prjFolder + "/src/main/java");
if (!srcDir.exists()) {
srcDir.mkdirs();
}
for (int j = 0; prjJavaFiles != null && j < prjJavaFiles.length; j++) {
String javaFile = prjJavaFiles[j].getName();
System.out.println(javaFile);
Path sourcePath = prjJavaFiles[j].toPath();
Path targetPath = Paths.get(srcDir.getPath(), javaFile);
try {
Files.copy(sourcePath, targetPath);
} catch (IOException e) {
System.err.println("Failed to copy java src: " + e.getMessage());
}
}
writeGradleBuildFile(build_gralde, projects[i], BUILD_GRADLE_KTS);
}
System.out.println(settings_gradle_kts);
writeGradleBuildFile(settings_gradle_kts.toString(), rootDir, "settings.gradle.kts");
}
private static void writeGradleBuildFile(String buildGraldeString, File dir, String fileName) {
try {
FileWriter gradleFile = new FileWriter(new File(dir, fileName ));
gradleFile.write(buildGraldeString);
gradleFile.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
This script assumes that your IntelliJ IDEA projects are plain Java projects (not Maven or Gradle-based) and creates a Gradle project structure where each of your folders is a module.
Please remember to replace "inputDir": "/Users/xxx/your_project_root" with the actual location of your directory.
The majority of folders should now be viewable as modules once they are loaded into IDEA with the final projects. Unicode path encoding may result in a failure to load all folders, so ensure all path names don't contain special characters.
- After successfully using the script, you should be able to view the folders as modules in your IDE.
Please note that this solution can potentially create different issues due to varied project structures created by inconsistent user actions. Adopting this solution indicates a willingness to deal with these inconsistencies further down the line.
For better control and management, you may want to consider introducing a project setup ruleset to the contributing users, ensuring uniformity and reducing potential issues that may arise.
Comments
0 comments
Please sign in to leave a comment.