Creating and importing Maven projects

From IntelliJ-Wiki

Jump to: navigation, search

We won't discuss here why Maven is good - let's just take it for granted. What is worth discussing is how IntelliJ IDEA makes your work with Maven more simple and intuitive.

This short tutorial will walk you step by step through the following tasks:

  • Creating an IntelliJ IDEA project with a Maven module.
  • Importing an existing Maven project into IntelliJ IDEA.
  • Exploring the project structure.
  • Adding and analyzing dependencies.
  • Running a Maven goal.


Contents

Prerequisites

  • Maven integration is shipped with IntelliJ IDEA, and you do not need to perform any additional actions to install it. You can start using it straight away for importing Maven projects.
  • You are working with IntelliJ IDEA version 9 or higher. For dependencies graphs, you will need Ultimate edition.
  • Your Internet connection is up and running.

Creating a project with a Maven module

The starting point of any development process in IntelliJ IDEA is creating a project: on the Welcome screen click Create New Project icon or choose File | New Project on the main menu:


File:NewProjectQuickStart.png

or

File:FileNewProject.png

On the first page of the New Project wizard, in the selector pane, choose Maven Module. On the right side of the page, enter the necessary information, such as a project name and its location:

File:MavenProjectWizard1.png


On the next page of the wizard, IntelliJ IDEA suggests the default values for the Maven cooordinates (GroupId, ArtifactId, and version). Let us accept the defaults:

File:MavenProjectWizard2.png

If you want to create a module from an archetype, select Create from archetype, and choose the archetype you need. For our example, we’ll create a blank Maven module.

Now the project is ready. As you see, IntelliJ IDEA has produced a directory structure, and pom.xml file for your module:

File:MavenSamplePom.png

Importing an existing Maven project into an IntelliJ IDEA project

One of the most attractive features of IntelliJ IDEA’s Maven support is the possibility to open or import existing projects. Let’s import an existing Maven project. As a result, a new module will be added to our IntelliJ IDEA project.

There are three ways to import a Maven project into IntelliJ IDEA:

  • Open the pom.xml file of the Maven project you want to import (File | Open – select pom.xml). However, this way you can create a new IntelliJ IDEA project rather than add a new module to an existing one.
  • Use File | New Module on the main menu, and follow the steps of the wizard.
  • Use the Maven Projects tool window. Let’s follow this way in our example.

To import a Maven project into an existing IntelliJ IDEA project:

1. In the Maven Projects tool window, click Add Maven Projects button:

File:MavenImports.png

2. In the Select Path dialog, select the pom.xml file of the Maven project you want to import:

File:SelectPathMaven.png

Exploring the resulting project

The IntelliJ IDEA project containing two Maven modules is ready. Let’s now explore its structure. Compare the directory tree of your project displayed in the Project tool window, with the collection of Maven projects File:MavenModuleIcon.png with their goals File:MavenGoals.png and dependencies File:icon_library_ext_lib.png, displayed in the Maven Projects tool window:

File:MavenModules.png

The Maven Projects tool window helps perform numerous Maven-related tasks: configure settings, run goals, and more.

Working with dependencies

For the purposes of our tutorial, let’s add some basic content. In the Project tool window, expand the src node, select test/java directory, press ALT+INSERT, choose New | Java Class, and enter the name qqTest.java. Use code completion to extend TestCase, and ALT+INSERT to generate the test, set up, and tear down methods. You see that the resulting class is bright with “red code”:

File:qqTestMaven2.png


This "red code" denotes the missing dependencies. This is where IntelliJ IDEA’s Maven support comes to help, with the two possible ways of providing the necessary dependencies:

We'll try both ways, and then, when the dependencies are ready, we'll analyze them visually.


Using quick fix

Place the caret at the TestCase reference. If you wait just a moment, you will see a red light bulb, which means that IntelliJ IDEA knows how to solve this problem, and can suggest a quick fix. Click the light bulb, or press ALT+ENTER to show the list of possible quick fixes:

File:qqTesAddMaven1.png

Choose Add Maven Dependency. The Maven Artifacts Search dialog box opens with TestCase automatically filled in as the search string. Select the desired dependency from the list:

File:MavenArtifactSearch.png

The missing import statements have been added, and the Maven dependency is now ready to be imported:

File:importjava.png


Click Import Changes or Enable Auto-Import. The Maven dependencies are added to the list of dependencies in the Maven Projects tool window, and to the External Libraries node of the Project tool window. The pom.xml file is updated accordingly with the <dependencies> section:

File:MavenDependenciescode.png

Enjoy the green "traffic light" in the upper-right corner of the editor tab - it means that everything is OK with your code, and all the references are resolved.

Now let us restore the initial state of our sample project, and delete the <dependencies> section. All the “red code” is back here. This step is required to try the alternative way, namely, generating dependencies in the pom.xml file.

Generating dependencies in the pom.xml file

In the Project tool window, select the pom.xml file under MavenModule, and open it for editing (F4). In the editor, place the caret next to the <version> element, and choose Generate on the context menu. Alternatively, press ALT+INSERT.

In the Generate pop-up menu, select the type of dependencies you want to generate. As a result, a new <dependency> group nested under <dependencies> is created. So doing, the point of user input is placed inside the <groupId> element. IntelliJ IDEA displays a suggestion list of suitable values:

File:MavenGenerateDependency.png

Select the desired value, and press ENTER. The point of input is moved to the <artifactId> element, then to <version>. Every time IntelliJ IDEA suggests suitable values to fill in the blanks. Finally, when a new <dependency> element is added, IntelliJ IDEA informs you that a new Maven module is ready to be imported. Click Import changes: the new dependency is added to the list of dependencies. You can now observe it in the Maven Projects tool window, and under the External Libraries node of the Project tool window:

File:DependencyStructure.png

As you see, we have achieved the same result: junit archive is added to the project.

Analyzing dependencies

The amount of dependencies in a real-life project might be huge. If you want to evaluate the scale and complexity of your Maven project, use visual representation of the dependencies.

To do that, select File:dependencyicon.png on the Maven Projects tool bar or right-click MavenModule File:mavenmoduleicon.png in the Maven Projects tool window, and choose Show Dependencies on the context menu:

File:mavenprojectToolBar.png

or

File:ShowDependencies.png

Important: Maven dependency graphs are only available in the Ultimate edition of IntelliJ IDEA.

The visual representation of the module dependencies is opened in a pop-up frame:


File:Dependencypopup.png

Running goals

IntelliJ IDEA provides two ways of running Maven goals. You can:

  • Create run/debug configuration and launch it.
  • Use the Run Maven Build command in the Maven Projects tool window. This way doesn’t require any run/debug configuration.

For this tutorial, let’s use the second of the methods. To execute a goal, select it in the Maven Projects tool window, and press CTRL+SHIFT+F10, or click the Run Maven Build button on the toolbar:

File:MavenRun1.png

Observe the results of the goal execution in the console of the Run tool window:

File:MavenRun2.png

More information

Personal tools