Importing a Maven project in IntelliJ IDEA
This tutorial explains how to import an existing Maven project into IntelliJ IDEA and how to create a running configuration for a Maven goal. This is useful, for example, when using the Jetty Maven plugin that allows you to quickly deploy and run a Java web application using this popular server.
|Maven is a project management tool that goes beyond dependency management. See Learning Maven Concepts to learn more.|
This tutorial uses a Maven archetype that generates a web application with a preconfigured Jetty Maven Plugin in it:
mvn -B archetype:generate -DarchetypeGroupId=com.vaadin -DarchetypeArtifactId=vaadin-archetype-application -DarchetypeVersion=LATEST -DgroupId=org.test -DartifactId=webapp -Dversion=1.0-SNAPSHOT
|If you prefer, you can generate a ZIP file with the project at https://vaadin.com/start/latest or https://start.vaadin.com. Make sure you select the Plain Java Servlet option before downloading the project.|
The project should include a directory with a Maven project in it:
|You need Node.js installed on your computer to compile the example application.|
There are several ways to import a Maven project in IntelliJ IDEA. The most common approach is to open the
pom.xml file directly. You can do it in the welcome screen by clicking Open:
|Import Project gives you more options to tweak the import process. However, this is out of the scope of this tutorial.|
pom.xml file of the Maven project you want to import and click Open as Project.
|The welcome window is only shown when you close all projects in IntelliJ IDEA. If you don’t want to close your currently opened projects, you can select File > Open in the menu instead.|
IntelliJ IDEA has an excellent integration with Maven. You can run common commands such as
mvn install or
mvn jetty:run without having to leave the IDE.
Open the Maven view by clicking the corresponding option on the left side of the IntelliJ IDEA window:
This view shows all the available Maven projects and their build phases and build goals. Let’s say you want to run
mvn install. To do that, expand the project tree in the Maven view to show the corresponding lifecycle phase and then double-click install:
You’ll see how IntelliJ idea executes the
install build phase (and all the previous phases in the Maven’s default lifecycle) that downloads dependencies and copies the generated artifacts into your local Maven repository, among other things.
You can use a similar approach to run any Maven goal. For example, you can double-click the
jetty:run goal in the Plugins sub-tree to deploy and run the web application implemented in the project you imported. Similarly, if you are using Spring Boot, you can double-click
spring-boot:run to run the application.
Since using the
jetty:run goal could be a frequent task during the development of a Java web application, you may prefer to create a running configuration for it.
A running configuration is a shortcut to run a specific task from within the IDE. Let’s create a running configuration for the
jetty:run Maven goal to make it simpler to run the web application.
jetty:run option in the Maven view and select Create 'webapp [jetty:run]':
For simplicity, change the name of the configuration to Run on Jetty and click OK:
|If you are using Spring Boot, use the |
You should see the new option on the top right corner of IntelliJ IDEA:
Now you can deploy and run the web application by clicking the run (or the debug) icon in the toolbar:
|If you use the Vaadin project used in the tutorial, you can invoke the web application at http://localhost:8080.|
That’s it. Now you can use IntelliJ IDEA to develop your Maven application. To learn more about: