Create project programmatically

Hi,

I want to create a Java Project progammatically in IDEA? Is it possible, if
yes can someone provide me with some pointers?

I had a look at the Open API documentation, but couldn't find anything.

Regards,
Priya


10 comments
Comment actions Permalink

In order to implement that kind of thing you'll have to get hold of the JavaModuleType instance. You can get it throught ModuleTypeManager.

Once you do that you can get the ModuleBuilder instance using javaModuleType.createModuleBuilder().

The moduleBuild.setupRootModel() method should take care of the module creation.

Hope this work, i never used it..... :o)

0
Comment actions Permalink

I am getting NPE for ModuleTypeManager.getInstance()

Can someone provide me with more information on how can i create a Java
Project programmatically?

~Priya

"Hugo Palma" <no_reply@jetbrains.com> wrote in message
news:21081966.1147097774275.JavaMail.itn@is.intellij.net...

In order to implement that kind of thing you'll have to get hold of the

JavaModuleType instance. You can get it throught ModuleTypeManager.
>

Once you do that you can get the ModuleBuilder instance using

javaModuleType.createModuleBuilder().
>

The moduleBuild.setupRootModel() method should take care of the module

creation.
>

Hope this work, i never used it..... :o)



0
Comment actions Permalink

Hi,

Can someone provide more information on this? It is really urgent.
Any help in this regard will be highly appreciated.

~Priya

0
Comment actions Permalink

You can get the ModuleTypeManager by either:

- Declaring a ModuleTypeManager type parameter in you plugin component contructor.

- Using ModuleTypeManager.getInstance() in your plugin component contructor.


You're probably getting the NPE because you're not executing in a plugin component.

0
Comment actions Permalink

Yes, I was trying to create it from a normal java project.

I have put it in the constructor of the component of a plugin project. I am
not getting a NPE now, but I am facing a new problem of ambiguity.
Since createModuleBuilder method is defined in both classes ModuleType and
JavaModuleType, there is ambiguity. I tried resolving this by directly
creating new JavaModuleBuilder object.

After that, I need to call setupRootModel() to create the module, but this
api takes ModifiableRootModel as its parameter and I am not sure how can I
get an object of this interface. I tried using ModuleRootManager, but this
class needs Module type object as a parameter for its getInstance method.
I want to create a module and I don't have an object of type Module, how can
I proceed further on this?

Thanks in advance.

~Priya


"Hugo Palma" <no_reply@jetbrains.com> wrote in message
news:9615819.1148638232408.JavaMail.itn@is.intellij.net...

You can get the ModuleTypeManager by either:

>

- Declaring a ModuleTypeManager type parameter in you plugin component

contructor.
>

- Using ModuleTypeManager.getInstance() in your plugin component

contructor.
>
>

You're probably getting the NPE because you're not executing in a plugin

component.


0
Comment actions Permalink

Ok. I just realized that you can get the JavaModuleType by doing just:

ModuleType.JAVA


You can get the ModifiableModuleModel for the createModule() method with:

ModuleManager.getInstance(myProject).getModifiableModel()


The createModule() method already calls setupRootModel() so you only need to call createModule(), so forget that call, sorry.


Hope this gets you going.....

0
Comment actions Permalink

Hi,
I have created a project component with project as a parameter to the
constructor.
I have written the code pasted below in the initComponent menthod, this
creates a module but I can't locate it at the location I have set for the
ModuleBuilder.
Also when I print the module project path, it prints the path of the project
passed to the constructor of the Project component.
Please let me know if I am missing any steps.

I followed these steps:

JavaModuleType javaModuleType= (JavaModuleType) ModuleType.JAVA;
final ModifiableModuleModel model =
ModuleManager.getInstance(myProject).getModifiableModel();
// JavaModuleBuilder builder = javaModuleType.createModuleBuilder(); This
gives me ambigious function error.
final JavaModuleBuilder builder = new JavaModuleBuilder();
builder.setName("TestModule");
builder.setModuleFilePath("D:
temp
");
ApplicationManager.getApplication().runWriteAction(new Runnable(){
public void run() {
try {
Module module =builder.createModule(model);
if(module!=null)
{
System.out.println("module file path:
"+module.getModuleFilePath());
System.out.println("module name:
"+module.getName());
System.out.println("module prj path:
"+module.getProject().getProjectFilePath());
}
} catch (InvalidDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ModuleWithNameAlreadyExists
moduleWithNameAlreadyExists) {
moduleWithNameAlreadyExists.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
});


~Priya

"Hugo Palma" <no_reply@jetbrains.com> wrote in message
news:20907524.1148652967469.JavaMail.itn@is.intellij.net...

Ok. I just realized that you can get the JavaModuleType by doing just:

>

ModuleType.JAVA

>
>

You can get the ModifiableModuleModel for the createModule() method with:

>

ModuleManager.getInstance(myProject).getModifiableModel()

>
>

The createModule() method already calls setupRootModel() so you only need

to call createModule(), so forget that call, sorry.
>
>

Hope this gets you going.....



0
Comment actions Permalink

Can someone please help me out?

"Priya" <priya_bunty@yahoo.co.in> wrote in message
news:e5epsg$7cq$1@is.intellij.net...

Hi,
I have created a project component with project as a parameter to the
constructor.
I have written the code pasted below in the initComponent menthod, this
creates a module but I can't locate it at the location I have set for the
ModuleBuilder.
Also when I print the module project path, it prints the path of the
project
passed to the constructor of the Project component.
Please let me know if I am missing any steps.

>

I followed these steps:

>

JavaModuleType javaModuleType= (JavaModuleType) ModuleType.JAVA;
final ModifiableModuleModel model =
ModuleManager.getInstance(myProject).getModifiableModel();
// JavaModuleBuilder builder = javaModuleType.createModuleBuilder();
This
gives me ambigious function error.
final JavaModuleBuilder builder = new JavaModuleBuilder();
builder.setName("TestModule");
builder.setModuleFilePath("D:
temp
");
ApplicationManager.getApplication().runWriteAction(new Runnable(){
public void run() {
try {
Module module =builder.createModule(model);
if(module!=null)
{
System.out.println("module file path:
"+module.getModuleFilePath());
System.out.println("module name:
"+module.getName());
System.out.println("module prj path:
"+module.getProject().getProjectFilePath());
}
} catch (InvalidDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ModuleWithNameAlreadyExists
moduleWithNameAlreadyExists) {
moduleWithNameAlreadyExists.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
});

>
>

~Priya

>

"Hugo Palma" <no_reply@jetbrains.com> wrote in message
news:20907524.1148652967469.JavaMail.itn@is.intellij.net...

>> Ok. I just realized that you can get the JavaModuleType by doing just:
>>
>> ModuleType.JAVA
>>
>>
>> You can get the ModifiableModuleModel for the createModule() method with:
>>
>> ModuleManager.getInstance(myProject).getModifiableModel()
>>
>>
>> The createModule() method already calls setupRootModel() so you only need

to call createModule(), so forget that call, sorry.

>>
>>
>> Hope this gets you going.....
>



0
Comment actions Permalink

Here is the code to create project programmatically. (Java project)

//code to create project
String newProjectFilePath =projectPath"
"
projectName"
"
.ipr;
final Project prj =
ProjectManagerImpl.getInstanceEx().newProject(newProjectFilePath,true,false);
ProjectJdk[] jdks = ProjectJdkTable.getInstance().getAllJdks();
final ProjectJdk projectjdk = jdks[0];
if(projectjdk != null)
{ ApplicationManager.getApplication().runWriteAction(new
Runnable(){
public void run() {

ProjectRootManagerEx.getInstanceEx(prj).setProjectJdk(projectjdk);
ProjectRootManagerEx.getInstanceEx(prj).setLanguageLevel(LanguageLevel.JDK_1_4);
}
});
}
prj.save();

//code to create module and add it to project

final JavaModuleBuilder modulebuilder = new JavaModuleBuilder();
final ModifiableModuleModel model =
ModuleManager.getInstance(prj).getModifiableModel();
modulebuilder.setName(projectName);
String newProjectImplimentationFilePath
=projectPath"
"
projectName"
"
.iml;
modulebuilder.setModuleFilePath(newProjectImplimentationFilePath);
ArrayList list = new ArrayList();
list.add(new Pair(projectPath,""));
modulebuilder.setSourcePaths(list);
String classesPath = projectPath+"
classes";
modulebuilder.setCompilerOutputPath(classesPath);
modulebuilder.setContentEntryPath(projectPath);
ProjectJdk[] jdks = ProjectJdkTable.getInstance().getAllJdks();
final ProjectJdk projectjdk = jdks[0];



if(model != null)
{
ApplicationManager.getApplication().runWriteAction(new
Runnable(){
public void run() {
try {
modulebuilder.createModule(model);
modulebuilder.setModuleJdk(projectjdk);
model.commit();

} catch (InvalidDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ModuleWithNameAlreadyExists
moduleWithNameAlreadyExists) {
moduleWithNameAlreadyExists.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (ConfigurationException e) {
e.printStackTrace();
}
catch (ModuleCircularDependencyException e) {
e.printStackTrace();
}
}
});
}


StartupManager.getInstance(project).registerPostStartupActivity(new
Runnable() {

public void run()
{
SwingUtilities.invokeLater(new Runnable() {

public void run()
{
ToolWindow toolwindow =
ToolWindowManager.getInstance(prj).getToolWindow(ToolWindowId.PROJECT_VIEW);
if(toolwindow != null)
toolwindow.activate(null);
if(modulebuilder == null)
ModulesConfigurator.showDialog(project,
null, null, true);
}
});
}

});

ProjectManagerEx.getInstanceEx().openProject(prj);

This code creates the project, module for that project and opens the project
in IDEA. But attimes two IDEA frames are opened, not sure why.

Hope this helps those who want to do this.

~Priya


"Priya" <priya_bunty@yahoo.co.in> wrote in message
news:e3ni5d$hcm$1@is.intellij.net...

Hi,

>

I want to create a Java Project progammatically in IDEA? Is it possible,
if
yes can someone provide me with some pointers?

>

I had a look at the Open API documentation, but couldn't find anything.

>

Regards,
Priya

>



0
Comment actions Permalink

Here is the code to create project programmatically. (Java project)

//code to create project
String newProjectFilePath =projectPath"
"
projectName"
"
.ipr;
final Project prj =
ProjectManagerImpl.getInstanceEx().newProject(newProjectFilePath,true,false);
ProjectJdk[] jdks = ProjectJdkTable.getInstance().getAllJdks();
final ProjectJdk projectjdk = jdks[0];
if(projectjdk != null)
{ ApplicationManager.getApplication().runWriteAction(new
Runnable(){
public void run() {

ProjectRootManagerEx.getInstanceEx(prj).setProjectJdk(projectjdk);
ProjectRootManagerEx.getInstanceEx(prj).setLanguageLevel(LanguageLevel.JDK_1_4);
}
});
}
prj.save();

//code to create module and add it to project

final JavaModuleBuilder modulebuilder = new JavaModuleBuilder();
final ModifiableModuleModel model =
ModuleManager.getInstance(prj).getModifiableModel();
modulebuilder.setName(projectName);
String newProjectImplimentationFilePath
=projectPath"
"
projectName"
"
.iml;
modulebuilder.setModuleFilePath(newProjectImplimentationFilePath);
ArrayList list = new ArrayList();
list.add(new Pair(projectPath,""));
modulebuilder.setSourcePaths(list);
String classesPath = projectPath+"
classes";
modulebuilder.setCompilerOutputPath(classesPath);
modulebuilder.setContentEntryPath(projectPath);
ProjectJdk[] jdks = ProjectJdkTable.getInstance().getAllJdks();
final ProjectJdk projectjdk = jdks[0];



if(model != null)
{
ApplicationManager.getApplication().runWriteAction(new
Runnable(){
public void run() {
try {
modulebuilder.createModule(model);
modulebuilder.setModuleJdk(projectjdk);
model.commit();

} catch (InvalidDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ModuleWithNameAlreadyExists
moduleWithNameAlreadyExists) {
moduleWithNameAlreadyExists.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (ConfigurationException e) {
e.printStackTrace();
}
catch (ModuleCircularDependencyException e) {
e.printStackTrace();
}
}
});
}


StartupManager.getInstance(project).registerPostStartupActivity(new
Runnable() {

public void run()
{
SwingUtilities.invokeLater(new Runnable() {

public void run()
{
ToolWindow toolwindow =
ToolWindowManager.getInstance(prj).getToolWindow(ToolWindowId.PROJECT_VIEW);
if(toolwindow != null)
toolwindow.activate(null);
if(modulebuilder == null)
ModulesConfigurator.showDialog(project,
null, null, true);
}
});
}

});

ProjectManagerEx.getInstanceEx().openProject(prj);

This code creates the project, module for that project and opens the project
in IDEA. But attimes two IDEA frames are opened, not sure why.

Hope this helps those who want to do this.

~Priya

0

Please sign in to leave a comment.