Creating a new maven project

IntelliJ IDEA lets you create a Maven project or add a Maven support to any existing project.

  1. Launch the New Project wizard. If no project is currently opened in IntelliJ IDEA, click Create New Project on the Welcome screen: Otherwise, select File | New | Project from the main menu.
  2. Select Maven from the options on the left.
  3. Specify project's SDK (JDK) or use a default one and an archetype if you want to use a predefined project template (configure your own archetype by clicking Add Archetype).
    Click Next.
  4. On the next page of the wizard, specify the following Maven basic elements that are added to the pom.xml file:
    • GroupId - a package of a new project.
    • ArtifactId - a name of your project.
    • Version - a version of a new project. By default, this field is specified automatically.
    Click Next.
  5. If you are creating a project using a Maven archetype, IntelliJ IDEA displays Maven settings that you can use to set the Maven home directory and Maven repositories. Also, you can check the archetype properties. Click Next.
  6. Specify the name and location settings. Click Finish.

Importing a project from a maven model

  1. Launch the New Project wizard. If no project is currently opened in IntelliJ IDEA, click Import Project on the Welcome screen. Otherwise, select File | New | Project from Existing Sources from the main menu.

    Note that you can also select File | Open from the main menu and choose pom.xml file or a directory containing pom.xml file. IntelliJ IDEA will import a Maven project even if the project was not opened or imported before.

  2. In the dialog that opens, select the directory that contains the project you want to import or a directory that contains a pom.xml. Click OK.
  3. On the first page of the Import Project wizard, in the Import Project from External model select Maven and click Next.

    (This page is not displayed if IntelliJ IDEA has guessed what you are importing.)

  4. On the next page of the wizard, specify Maven settings or use the default selection.
    The default settings are usually sufficient for a project. However, you can select the following (frequently used) options:
    • Search for projects recursively - if you select this option, the sub projects (if any) are located and set up correctly.
    • Import Maven projects automatically - if you select this option, the project is imported automatically every time you make changes to your POM file and you don't need to control manually when to import the changes. However, note that it might take some time to re-import a large project. Also, note that the changes made in the IntelliJ IDEA project (for example, adding a dependency to your project through the Project Structure dialog) will be overwritten on re-import by POM since IntelliJ IDEA considers the POM file as a single source of truth.
    Click Next.
  5. On the next page of the wizard, IntelliJ IDEA displays the found projects and you can select the ones you need to import.
    Click Next.
  6. On the next page of the wizard, specify the project's SDK and click Next.
  7. On the next page of the wizard, specify a name and the location of your project.
    Click Finish.

Adding a new maven module to an existing project

You can add a Maven module to the project in which you are already working.

  1. In the Projects view, right-click the project folder and select New | Module. Alternatively, on the main menu, select File| New | Module to open the New Module wizard.
  2. If you used main menu to add a module then the process of adding a module is the same as Creating a new Maven project.
    If you are adding sub modules by right-clicking the root folder then the process of adding a new module is shorter. You need to specify the name of your module in the ArtifactId field. The rest of the information is added automatically and you can use either default settings or change them according to your preferences.
    Also, note that Add as module to and Parent fields, by default, display the basic Maven attributes (groupId, artifactId, and version) of the project to which you are trying to add the module. You can click images/browseButton.png to change the information displayed.

Configuring a multi-module maven project

IntelliJ IDEA lets you create a multi-module Maven project. The multi-module project is defined by a parent POM file with several sub modules.

  1. Create a Maven parent project. IntelliJ IDEA creates a standard Maven layout including an src folder.
  2. In the Project view, remove the src folder (you don't need it for the parent project).
  3. In the project view, right-click your project (or in the main menu, click File) and select New | Module to add a sub project.
  4. In the New Module wizard following the instructions on how to add a module, specify the necessary information and click Finish.
    The src folders are created automatically and you can open POM and add a packaging that you need. IntelliJ IDEA adds the module to the parent project. IntelliJ IDEA also adds name and the description of the sub project to the parent POM.

    images/maven_parent_pom.png

    Note that the packaging in the parent POM is defined as pom since it is an appropriate packaging for the parent project which refers to other sub projects.
  5. You can also add dependencies to the parent POM that will be inherited by the modules.

    images/maven_parent_pom_dependency.png

  6. Open Maven Projects tool window to see that all changes made in the parent POM are reflected in sub projects.

    images/maven_global_dependency.png

Converting a regular project into a maven project

  1. Create an external POM file where you describe your project.
  2. Import your project via Maven model or simply open your POM in IntelliJ IDEA. IntelliJ IDEA adds POM to the project and a Maven layout for the existing elements.

    images/maven_project_layout.png

    IntelliJ IDEA also generates the corresponding structure in the Maven Projects tool window.

    images/maven_tool_window_structure.png

Alternatively, you can create a non-Maven project and add a Maven support via IntelliJ IDEA UI.

  1. Create a regular project, for example, a Java project or open an existing one.
  2. Open File | Settings to get to Maven settings if you need to check or change the Maven version

    images/maven_settings_home_dir.png

    or to see which JDK IntelliJ IDEA will use to import Maven settings.

    images/maven_settings_JDK.png

  3. In the Projects view, right-click your project and select Add Framework Support.
  4. In the dialog that opens, select Maven from the options on the left and click OK.
    IntelliJ IDEA adds a default POM to the project and generates a corresponding structure in Maven Projects tool window.
  5. Open the generated POM and specify a groupId, the artifactId and version are specified automatically.

    images/maven_groupId.png

    Every time you change the POM, IntelliJ IDEA displays a pop-up suggesting to import your changes.

    images/maven_popup.png

    We recommend you to make all your project changes in POM since IntelliJ IDEA considers pom.xml as a single source of truth.