Create new project with multiple modules
Answered
I want to programmatically create a new project (Java for now) containing multiple modules (also Java for now, three modules with different names).
I am already creating a new project with the following code using the ProjectManagerEx:
val projectBaseDir = File(projectPath)
FileUtil.ensureExists(projectBaseDir)
val newProject = projectManager(projectName, projectPath, false, false)
newProject?.save()
I then create new modules for this project:
return runWriteAction {
val module = moduleManager.newModule(moduleFilePath, ModuleTypeId.JAVA_MODULE)
project.save()
module
}
My problem is, that although this code runs without errors, the actual created directories and files are just empty and the module xml in the project doesn't even reference the created modules when opening the project. How do I actually write those objects to disk, e.g. create the .iml files for the modules, etc.?
Please sign in to leave a comment.
Please show the full source code and explain how it is invoked by user/from where, Thanks
Sure, the code is invoked by a button in an integrated web view in the plugin. This will forward three repository URLs to the plugin itself, which will then create a new project and clone the three repositories to subdirectories inside the project. I then would like to create modules out of these subdirectories.
The project is created like this:
fun newEmptyProject(courseId: Long, exerciseId: Long, exerciseName: String, view: ExerciseView): Project? {
val projectManager = ProjectManagerEx.getInstanceEx()
// Create the (empty) base directory of the new project and returns the path as a String
val exercisePath = MyPluginFileUtils.setupExerciseDirPath(courseId, exerciseId, exerciseName, view)
val newProject = projectManager.newProject(exerciseName, exercisePath, false, false)
newProject?.save()
return newProject
}
Afterwards, I clone the repositories and then try to create modules out of the three new subdirectories, calling three times the following function. For now, I only create an empty .iml in the module directory since I don't know how to generate this and also update the modules.xml in the root .idea directory. Sure, I could just fill it with my own xml data, but there has to already be a way to generate this based on the project and module objects?
fun newModule(project: Project, name: String): Module {
val modulePath = project.basePath + File.separatorChar + name
FileUtil.ensureExists(File(modulePath))
val moduleFilePath = modulePath + File.separatorChar + "$name.iml"
val moduleFile = File(moduleFilePath)
FileUtil.createIfNotExists(moduleFile)
val moduleManager = ModuleManager.getInstance(project)
return runInEdtAndGet {
runWriteAction {
val module = moduleManager.newModule(moduleFilePath, ModuleTypeId.JAVA_MODULE)
project.save()
module
}
}
}
You shouldn't create .iml files "manually", but use API to configure new module programmatically http://www.jetbrains.org/intellij/sdk/docs/reference_guide/project_model/module.html
That reference doesn't show how to create a module. How are you supposed to create one now?
Tewan, see ModifiableRootModel to modify the module to create
Seems like I have a different issue. I will create a new thread for it