Opening Maven Projects is Easy as Pie

March 24th, 2008 by Maria Khalusova

Maven integration in a Java IDE might not be a surprise for you, but let me show you just one thing, that makes IntelliJ IDEA Maven support prominent and noteworthy.

If you have any Maven project, you do not need to perform routine steps while importing it to IntelliJ IDEA and you do not need to be anxious about dependencies and artifacts in it, nor would you need to worry about technologies used in project. Just open the project as you do with usual IntelliJ IDEA projects, sit back and relax. IntelliJ IDEA will automatically recognize everything.

Here is an example. To open our Maven project, we just go to File | Open Project (Alt + F + O) and specify the path to pom.xml (the Maven project descriptor file).

Select Path dialog

IntelliJ IDEA creates a project based on the Maven Project descriptor file.
When done, all maven goals can be found in the Maven projects tool window. We can simply run/debug arbitrary Maven goals; execute them before/after make or run/debug, or by invoking an assigned keyboard shortcut. Select any Maven goal in the tree and right-click it to view options.

Run Maven Goal

Now, let’s view our project structure. Go to Settings (Ctrl + Alt + S) | Project Structure and click Modules. As you can see, IntelliJ IDEA recognized modules and web facets. Moreover, all dependencies and deployment settings were detected and stored.

Facet

That’s all, nothing but a single click and you can enjoy complete support for Maven builder, including smart coding assistance for editing Maven pom.xml, intelligent code completion, inspections and quick-fixes, refactorings and other IntelliJ IDEA productivity-boosting features, which we are going to cover in the next posts.

Technorati tags: , ,

15 Responses to “Opening Maven Projects is Easy as Pie”

  1. Souk Says:

    I work a lot with maven and Idea, works great, but I have one small question: is there a way to have Idea remember that the “target” folders are ignored, even when I do a “mvn clean” via the command line ? Usually, doing this just kills the “ignore” settings and I have to go through the Project settings > modules > set target folders to ignore all over again

  2. Anton Makeev Says:

    Unfortunatelly we cannot exclude target directory since it may contain generated sources. All the directories under the target are exluded, though.
    The 8.0 eap and forthcoming 7.0.4 eap feature improved handling of target directory. You may try them both.

  3. Jeremy Leipzig Says:

    I don’t understand why the maven plugin doesn’t inform IntelliJ of where it is putting the dependencies. When I go to the dependency tab all the jars are red and cannot be edited (the edit button does nothing and the navigate right-click option is greyed out). I have to manually find them and add them to my global library to do any real work.

  4. Anton Makeev Says:

    All the dependencies are resolved and downloaded during importing and are put in the maven’s repository (by default in ~/.m2). If you have ‘red’ dependencies, it must be something wrong with either pom or repository.

    You may write me on ‘Anton dot Makeev at jetbrains dot com’ and describe you problem in details.

  5. Jeremy Leipzig Says:

    they are downloaded and placed in your repository but those dependencies are not placed into IntelliJ’s global libraries
    so you can build a Maven project and run through its goals without issue but to properly develop an application further you need to manually build your global library based on where Maven put those dependencies in your repository or copy the source’s application library config file
    ~/.IntelliJIdea70/config/options/applicationLibraries.xml
    this is not an optimal solution

  6. Anton Makeev Says:

    Maven dependencies are not supposed to be placed into any global library: they are configured as module libraries. Could you send me your pom file and some screenshots on \’Anton dot Makeev at jetbrains dot com’ so I could understand the problem?

  7. Darin Manica Says:

    I really like the pom support in Idea, but I’m having trouble with HotSwap in the debugger. When I compile just a single file (ctrl + shift + F9), it appears to be running a maven target, which triggers a reload of my webapp. I really want to just compile the file and have it HotSwap without triggering anything from Maven. Is this possible?

    Thanks!
    Darin

  8. Anton Makeev Says:

    I don’t quite understand you.
    Do you want to automatically run hotswap after executin maven goal?
    If it’s so you may vote for and watch http://www.jetbrains.net/jira/browse/IDEADEV-26044.
    We’ll implement it in one of the following eaps.

  9. Darin Manica Says:

    I don’t want to run hotswap after executing a maven goal, I want to be able to compile a single file and hotswap it without running any maven goals. For example, say that I run “maven package” to build a .war file and start my app server. Then I make a change to a java file. I’d like to compile that single file in Idea and have it hotswap. Currently, when I run “Compile Foo.java (ctrl+shift+F9)” it runs a maven goal that regenerates the war file instead of just compiling the single file and hotswapping it. The problem is that redeploying the war file is too slow and doesn’t always work (and defeats the purpose of hotswap.)

    I used to do this exact behavior, when using running “mvn idea:idea” and having maven generate my project/module files. For some reason, when I switched over to using the pom as the project file, I lost this functionality. Otherwise, I prefer using the pom support, so I’m hoping it is possible to hotswap a single file without running maven targets.

  10. Anton Makeev Says:

    Compilation with SHIFT+CTRL+F9 does not run maven goal: it runs the native idea compilation process; this process involves regeneration of the war file, though.
    I could recommend you to use an exploded directory instead of the war if you want to have a quick hatswap.
    If you want any details, please mail me to Anton dot Makeev at jetbrains dot com.

  11. Dan Delorey Says:

    I am having trouble debugging maven goals. I can run individual goals by double-clicking them or by selecting “Run Goal” from the context menu. However, if I select “Run ‘project_name [goal_name]‘” or “Debug ‘project_name [goal_name]‘” from the context menu, I get an error “Error running Tests: No valid Maven installation found. Either set the home directory in the configuration dialog or set the M2_HOME environment variable on your system.” I have set the home directory on the configuration dialog. Can you give me any other pointers?

  12. Anton Makeev Says:

    Unfortunately at the moment you have to specify an installation path for the Maven Run Configuration itself: the Maven Settings do not affect Run Configurations.
    This inconsistency will be fixed in next versions.

  13. Jigar Gosar Says:

    I have an idea project in which i have couple of modules each of which has a maven pom file. Now if I want to compile the project from within idea I have to manually add each and every library into idea project. How can i tell idea to add all maven dependencies for each module in its configuration so that i can compile projects from within idea.

    Basically, I want that every time I add a dependency in pom file it should also be included in the module dependency, is there any easier way to achieve this?

  14. Jeremy Leipzig Says:

    Jigar Gosar: You want the Maven Dependency Sync plugin.
    That is what I was looking for myself as you can see several posts above. I don’t know why this is not a built-in feature.

  15. Axel Wagner Says:

    @Dan Delorey & @Anton Makeev

    bug in .idea/workspace.xml:

    after import a maven project just add:

    and modify by setting a mavenHome variable in
    :

Leave a Reply