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
- Importing Project from the Maven Model
- Working with Project Structure
- Working with POM
- Working with Maven Tool Window
- Working with Maven Settings
Creating Maven Project
- Open Project Wizard, in the left-hand pane select Maven.
- 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.
- Project SDK - specify your project SDK (JDK).
- On the next page of the wizard, specify properties for your Maven project and click Next.
- On the next page of the wizard, specify project's information and click Finish.
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
.
- If no project is currently open in IntelliJ IDEA, click Import Project on the Welcome screen. Otherwise, select File | New | Project from Existing Sources.
- 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.
- 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.)
- 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.
- 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. - 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.
- On the next page of the wizard, select the project's SDK. Click Next.
- On the next page of the wizard, specify the new project name and location of the project files. Click Finish.
Working with Project Structure
After importing a Maven project, check its project structure to make sure that the Maven structure was imported as well.
- On the main menu select .
- On the page that opens, check that the appropriate libraries and dependencies were downloaded.
You can download an external maven library to your project using Project Structure dialog.
- Open Project Structure page, from the left-hand pane, select Libraries, click icon and from the drop-down list, select From Maven. The Download Library from Maven Repository window opens.
- Start typing the name of the library in the search field and select your target from the search results' list.
- 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.
- In the window that opens, select the module to which you want to add the maven library and press OK.
- 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. The library is also added to your project tool window.
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.
- Open your POM file and in the editor and press Alt + Insert.
- From the list that opens, select an item that you want to generate, for example, a dependency.
- 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 (:). You can search either based on the name of artifact or the name of a 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.
- On the main menu click to open the Maven tool window.
- You can perform the following tasks:
- run goals
Click Execute Maven Goal icon. - 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]'. In the window that opens, specify run/debug configuration settings and click OK. - display dependencies
Click Show Dependencies icon. - 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. As a result, the trigger for the goal execution is displayed next to the selected goal. - assign a shortcut for running maven goal
Right-click on the goal and from the context menu select Assign Shortcut. In the window that opens, add the shortcut and press OK.
- run goals
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. For example, you can choose to work in offline mode, add repositories or change how things are imported in your project.
- On the main menu select .
- In the window that opens specify settings for your current maven project and click OK.