Programatic manipulation of project settings

Hi,

I need to perform the following operations programmatically from a plugin. I don't manage to find it in the openapi, I would appreciate if you can help me on this:

1. Modify the dependencies of a given module - specify that it uses a certain jar
2. Set a different output folder for a given module
3. Remove a named facet from the facets of a module
4. Add an Ant build file to the list of ant files included by the project, and later execute it (showing the output in IntelliJ console)
5. Given a certain VirtualFile that belongs to the project, make it the selected node in the project-view

Thanks,
Shmulik

11 comments
Comment actions Permalink

Hello Shmulik,

I need to perform the following operations programmatically from a
plugin. I don't manage to find it in the openapi, I would appreciate
if you can help me on this:

1. Modify the dependencies of a given module - specify that it uses a
certain jar


There's a number of steps involved in here - see, for example, TestNGUtil.checkTestNGInClasspath()
for a code sample.

2. Set a different output folder for a given module


ModuleRootManager.getInstance(module).getModifiableRootModel().setCompilerOutputPath()

3. Remove a named facet from the facets of a module


FacetManager.getInstance(module).getModifiableModel().removeFacet()

4. Add an Ant build file to the list of ant files included by the
project, and later execute it (showing the output in IntelliJ console)


AntConfiguration.getInstance(project).addBuildFile()

5. Given a certain VirtualFile that belongs to the project, make it
the selected node in the project-view


SelectInManager.getInstance(project).getTarget(SelectInManager.PROJECT).selectIn()

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Dmitry Hi,

Thank you so much for you prompt response!

bye,
Shmulik

0
Comment actions Permalink

Hi,

When I try to change the output path of the project, it is not changed but no exception is thrown. The new output path exists and the output path remains the same also after reloading the project.
Here is the code that I'm using:

ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
try {
VirtualFile moduleFile = module.getModuleFile();
VirtualFile classesFile = moduleFile.getParent().findChild("bin").findChild("classes");
System.out.println("Exists? "classesFile.exists()" "+classesFile.isDirectory());
ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
rootModel.setCompilerOutputPath(classesFile);
rootModel.setCompilerOutputPathForTests(classesFile);
rootModel.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
});

any ideas?

thanks,
Shmuliik

0
Comment actions Permalink

Hello Shmulik,

You also need to call inheritCompilerOutputPath(false).

When I try to change the output path of the project, it is not changed
but no exception is thrown. The new output path exists and the output
path remains the same also after reloading the project.

Here is the code that I'm using:

ApplicationManager.getApplication().runWriteAction(new Runnable()
{
public void run() {
try {
VirtualFile moduleFile = module.getModuleFile();
VirtualFile classesFile =
moduleFile.getParent().findChild("bin").findChild("classes");
System.out.println("Exists? "classesFile.exists()"
"+classesFile.isDirectory());
ModifiableRootModel rootModel =
ModuleRootManager.getInstance(module).getModifiableModel();
rootModel.setCompilerOutputPath(classesFile);
rootModel.setCompilerOutputPathForTests(classesFile);
rootModel.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
});
any ideas?

thanks,
Shmuliik

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hi,

Thanks, it works!
I looked for such a method, but expected something like setInherit..., sorry for asking too fast.

bye,
Shmulik

0
Comment actions Permalink

Hi,

Sorry, it is still not clear to me how exactly to make a file selected in the project tree. You've mentioned:

SelectInManager.getInstance(project).getTarget(SelectInManager.PROJECT).selectIn(...)

I need to hand this method a SelectInContext, I do I get this context given a VirtualFile?

thanks,
Shmulik

0
Comment actions Permalink

I am Working on IntelliJ 9 and migrating my plugin from 5.x to 9.x, facing the same problem of how to set the output path as ModuleRootManager setOutputPath is removed altogther from the API.

0
Comment actions Permalink

Hello Gaurav,

I am Working on IntelliJ 9 and migrating my plugin from 5.x to 9.x,
facing the same problem of how to set the output path as
ModuleRootManager setOutputPath is removed altogther from the API.


The replacement API is CompilerModuleExtension.getInstance(module).setCompilerOutputPath().

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hello Dmitry,

Thnaks for replying but when I try and use CompilerModuleExtension.getInstance(module).setCompilerOutputPath(). I am thrown with an exception
Configuring project failed for unexpected reason
java.lang.AssertionError: Writable model can be retrieved from writable ModifiableRootModel
    at com.intellij.openapi.roots.impl.CompilerModuleExtensionImpl.a(CompilerModuleExtensionImpl.java:225)
    at com.intellij.openapi.roots.impl.CompilerModuleExtensionImpl.setCompilerOutputPath(CompilerModuleExtensionImpl.java:196)
    at com.intellij.openapi.roots.impl.CompilerModuleExtensionImpl.setCompilerOutputPath(CompilerModuleExtensionImpl.java:186)
    at com.yolus.intellij.yde.ProjectConfigurer$YDEModule.configureModuleOrFail(ProjectConfigurer.java:340)
    at com.yolus.intellij.yde.ProjectConfigurer$YDEModule.access$000(ProjectConfigurer.java:280)
    at com.yolus.intellij.yde.ProjectConfigurer.configureProjectOrFail(ProjectConfigurer.java:148)
    at com.yolus.intellij.yde.ProjectConfigurer.run(ProjectConfigurer.java:96)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:748)
    at com.yolus.intellij.yde.ProjectSettingsAction.actionPerformed(ProjectSettingsAction.java:45)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:214)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:82)
    at com.intellij.ui.plaf.beg.BegMenuItemUI.a(BegMenuItemUI.java:496)
    at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:43)
    at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:516)
    at java.awt.Component.processMouseEvent(Component.java:6263)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
    at java.awt.Component.processEvent(Component.java:6028)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at com.intellij.ide.IdeEventQueue.f(IdeEventQueue.java:621)
    at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:507)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:372)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


Can't I use ModifiableRootModel, setExplodedDirectory method instead.

0
Comment actions Permalink

Well I am still not able to solve this puzzle

0

Please sign in to leave a comment.