How to: Get Project path

First of all: Good morning / Good day / Good evening 
Pick whatever fits yourself best.

 

I am currently working on a plugin to import/Export content from a Server to and from Intellij.

For this I need to get the path where my project is stored (e.g. "C:/User/Xyz/IdeaProjects/MyProject").

Hopefully someone can help me on this matter, the posts wich I found on the web only told everybody you don't need those paths... well I do need them for my Import-/Export-Interface.

11 comments

Hello,

what do you mean by 'my project is stored'? Project may contain different kind of files. There are project configurations files (.idea directory), there are directories where java sources are stored, there are other content roots which may be located in different places. If you have a simple single-module project everything may be stored in a single directory, but if you want your code to work with arbitrary projects it's better to clarify what do you need. What kind of stuff do you want to export from a server? Do you want to ask user where to put it or it can be determined automatically?

0

Hello,

if I create a new project on IntelliJ, it also creates a folder in wich my project is stored. I want this and nothing else!

The Path to the folder, wich contains .idea, src and the whole bothersome rest, nothing else.

0

> if I create a new project on IntelliJ, it also creates a folder in wich my project is stored. I want this and nothing else!

This isn't always true. On the last page of 'New Project' wizard we have 'More Settings' group where you can specify 'Content root' for the module, you can specify a different directory here. In that case .idea and 'src' folders will be located in different places, and which directory do you want? If you need location of the content root, you can call

ModuleRootManager.getInstance(ModuleManager.getInstance(project).getModules()[0]).getContentRoots()[0]

It'll work for simple projects which has single module with single content root.

0

First of all, looking at it now my earlier comment souns quite roude, I want to apologize for this.

I didn't know of that. 

I need the path to the directory of the "src"-folder.
This path, in addition to a pre-generated "\\src\\main\\resources import" is going to be send to my external command module

0

If you want to get location of source roots, try the following:

ModuleRootManager.getInstance(ModuleManager.getInstance(p).getModules()[0]).getSourceRoots()

It'll return locations of all source roots from the first module in the project. If you're not interested in test sources or resources, you can use other versions of getSourceRoots method. For a multi-module project you probably need to iterate over all modules in the project and collect roots from all of them.

0

I'm sorry, but what is "p" supposed to be?

0

It's an instance of Project. If your code is invoked inside AnAction, you can get instance of the current project from AnActionEvent#getProject.

0

Unfortunately I'm not inside an AnAction, but a Swing-GUI.

0

Then how your Swing-GUI is invoked from IDEA?

0

It is embedded into IDEA with the use of plugin.xml

0

There are hundreds of ways you can embed something into IDEA with the use of plugin.xml. Is that Swing-GUI part of your own dialog? If so how is it invoked, from the main menu, or from some popup menu? Or it is a part of some IDEA's dialog like 'Settings'? Or it's a part of a tool window?

0

Please sign in to leave a comment.