IntelliJ IDEA 2016.1 Help

Getting Started with Maven

IntelliJ IDEA lets you work with Maven by importing a project that contains pom.xml or by creating a Maven project from scratch.

Creating Maven Project

  1. Open Project Wizard, in the left-hand pane select Maven.
  2. In the right-hand pane, specify the following options:
    • Project SDK - specify your project SDK(JDK).
      If the necessary JDK is already defined in IntelliJ IDEA, select it from the list. Otherwise, click New and select the installation folder of the desired JDK.
    • If you want to create a Maven project based on one of the Maven Archetypes, select the Create from archetype check box, and select the desired archetype from the list.
      You can click Add Archetype and add a new archetype to the list of the existing ones.
    Click Next.
    creat_maven_project
  3. On the next page of the wizard, specify properties for your Maven project and click Next.
    new_maven_proj
  4. On the next page of the wizard, specify project's information and click Finish.
    new_maven_proj_page2

    IntelliJ IDEA creates a project with pom.xml.

Importing Project from the Maven Model

IntelliJ IDEA enables you to create a project on the base of an existing Maven project, represented by pom.xml.

  1. If no project is currently open in IntelliJ IDEA, click Import Project on the Welcome screen. Otherwise, select File | New | Project from Existing Sources.
  2. In the dialog that opens, select the directory that contains the project to be imported, or a file that contains an appropriate project description. Click OK.
    maven_select_dir_for_import
  3. On the first page of the Import Project wizard, select Maven, and click Next. (This page is not shown if IntelliJ IDEA has guessed what you are importing.)
    maven_import_model
  4. On the next page of the wizard, specify the project root directory. Define your import preferences, if they differ from those specified in the Maven Integration dialog. Click Environment settings to configure Maven environment settings.

    Click Next.

    maven_import_rootdir_page
  5. On the next page of the wizard, select the desired profiles, and click Next.

    Note that the Select profiles page appears if your root directory contains more than one pom.xml and the Search for projects recursively check box is selected.

  6. On the next page of the wizard, select the projects to be imported. Note that IntelliJ IDEA detects Maven projects by scanning the specified root. Click Next.
    maven_import_select_proj_to_import
  7. On the next page of the wizard, select the project's SDK. Click Next.
    maven_import_select_sdk
  8. On the next page of the wizard, specify the new project name and location of the project files. Click Finish.
    maven_import_proj_info

Working with Project Structure

After importing a Maven project, check its project structure to make sure that the Maven structure was imported as well.

  1. On the main menu select File | Project Structure.
  2. On the page that opens, check that the appropriate libraries and dependencies were downloaded.
    maven_project_structure

You can download an external maven library to your project using Project Structure dialog.

  1. Open Project Structure page, from the left-hand pane, select Libraries, click add icon and from the drop-down list, select From Maven.
    maven_new_lib
    The Download Library from Maven Repository window opens.
    maven_download_from_repos
  2. Start typing the name of the library in the search field and select your target from the search results' list.
    maven_download_from_repos2
  3. Select Download to check box, enter the location to which you want to download the library and press OK. By default, the Download to check box is cleared and IntelliJ IDEA downloads the library to the local repository.
    maven_download_to
  4. In the window that opens, select the module to which you want to add the maven library and press OK.
    maven_choose_module_for_lib
  5. As a result, IntelliJ IDEA adds the library to your dependencies' list. When you open the project, IntelliJ IDEA synchronizes such libraries. It might be helpful if the libraries were deleted from your computer or the project was taken from the VCS and libraries haven't been downloaded yet.
    maven_added_lib_ps
    The library is also added to your project tool window.
    maven_added_lib_pv

Working with POM

The code assistance is available when you work with POM in the editor. You can generate a new dependency and manage it. You can also add dependency, repository or plugin templates.

  1. Open your POM file and in the editor and press Alt + Insert.
  2. From the list that opens, select an item that you want to generate, for example, a dependency.
    maven_generate
  3. In the window that opens search for the artifact that you want to add and click Add. If you don't remember the full name of an artifact you can enter part of the groupId and part of the artifactId separated by a colon (:).
    maven_search_artifact
    You can search either based on the name of artifact or the name of a class.
    maven_search_class

Working with Maven Tool Window

After creating a new or importing an existing Maven project, IntelliJ IDEA displays the maven model inside the Maven tool window. You can use the Maven tool window to run maven goals, create run/debug configurations, show dependencies, customize the goal execution and perform other useful tasks.

  1. On the main menu click View | Tool Windows | Maven Projects to open the Maven tool window.
    maven_tool_window
  2. You can perform the following tasks:
    • run goals
      Click maven_goal_icon Execute Maven Goal icon.
      maven_exec_maven_goal
    • create run/debug configuration for the list of goals
      Highlight the goals you want to run, right-click on them and from the context menu select Create 'mvn [name of a goal]'.
      maven_create_run_debug_for_goals
      In the window that opens, specify run/debug configuration settings and click OK.
      maven_run_debug_config_for_goals
    • display dependencies
      Click Show Dependencies icon.
      maven_show_dependencies
    • set triggers for running maven goals
      Right-click on the goal and from the context menu select at what point IntelliJ IDEA should run your goal.
      maven_customize_goal_exec
      As a result, the trigger for the goal execution is displayed next to the selected goal.
      maven_customize_goal_exec_result
    • assign a shortcut for running maven goal
      Right-click on the goal and from the context menu select Assign Shortcut.
      maven_goal_shortcut
      In the window that opens, add the shortcut and press OK.
      maven_keymap_dialog

Working with Maven Settings

IntelliJ IDEA lets you change maven settings that are specific for your current project. Under Maven category you can find a number of options that can help you configure how your project is set up.

maven_setiings_categories
For example, you can choose to work in offline mode, add repositories or change how things are imported in your project.

  1. On the main menu select File | Settings | Build, Execution, Deployment | Build Tools | Maven.
    maven_settings
  2. In the window that opens specify settings for your current maven project and click OK.

See Also

Concepts:

Last modified: 13 July 2016