How to (programatically) set the Scope of a Module's dependencies?
I'm struggling to do two things with the IntelliJ IDEA API:
1) Set the Scope of a Module's dependencies. My best attempt:
Project project = event.getProject();
ModuleManager moduleManager = ModuleManager.getInstance(project);
Module[] allProjectModules = moduleManager.getModules();
for (Module module : allProjectModules) {
ModuleRootManager root = ModuleRootManager.getInstance(module);
root.orderEntries().forEach(new Processor<OrderEntry>() {
@Override
public boolean process(OrderEntry orderEntry) {
// Do something here?
return false;
}
});
}
2) Mark a module's directory as a "Sources Root". My best attempt:
Project project = event.getProject();
ModuleManager moduleManager = ModuleManager.getInstance(project);
Module[] allProjectModules = moduleManager.getModules();
for (Module module : allProjectModules) {
ModuleRootManager root = ModuleRootManager.getInstance(module);
for (VirtualFile file : root.getSourceRoots()) {
if (file.getName().equals("desired Name")) {
// Do something here?
}
}
}
Any help would be greatly appreciated.
Please sign in to leave a comment.
In order to modify module roots settings you need to use ModuleRootManager#getModifiableModel directly or via ModuleRootModificationUtil#updateModel.
1)
2) getSourceRoots() returns directories which are already marked as source roots. To add a new root, use ContentEntry#addSourceFolder. ContentEntry describes a content root, so if you need to add a source root which isn't added as a content root yet, use something like
First code snippet is exactly what I was looking for, thanks!
For the second, how do I change an existing source folder? E.g., if I have a source folder that is marked as a "Test Sources Root", how can I simply mark it as a "Sources Root"?
Note: I tried just re-adding the source dir, like so:
ModuleRootModificationUtil.updateModel(projectModule, model -> {
for (VirtualFile sourceRoot : model.getSourceRoots()){
if (sourceRoot.getName().equals("tst")){
model.addContentEntry(sourceRoot).addSourceFolder(sourceRoot, JavaSourceRootType.SOURCE);
}
}
});
This seem to kind of work, but when I do this, the tst dir has the project name in brackets behind it, and it's not colored like it would be when manually setting in the editor. E.g.,
MyProject
> src
> tst [MyProject] test sources root
It is also showing up as "test sources root", rather than "sources root" (which is desired in my weird use case).
There is no way to change type of an existing source root. You need to remove it using ContentEntry#removeSourceFolder method, and then add a new one. Also if you already have a content root, you should find its ContentEntry using ModifiableRootModel#getContentEntries method, not add a new one. I.e. you can write something like
This worked. Thank you very, very much.