Как подключить maven к проекту в intellij idea
Перейти к содержимому

Как подключить maven к проекту в intellij idea

  • автор:

Maven

IntelliJ IDEA supports a fully-functional integration with Maven that helps you automate your building process. You can easily create a new Maven project, open and sync an existing one, add a Maven support to any existing IntelliJ IDEA project, configure and manage a multi-module project.

You can also create a Maven project and store it in the WSL environment or open it from the WSL file system. For more information, refer to the WSL section.

If you want to use a custom Maven version that is not bundled with IntelliJ IDEA, you need to install it locally.

Create a new Maven project

If no project is currently open in IntelliJ IDEA, click New Project on the Welcome screen. Otherwise, select File | New | Project from the main menu.

Name the new project and change its location if necessary.

Select the Create Git repository to place the new project under version control.

You will be able to do it later at any time.

Select a language that you want to use in your project. Click if you want to add other languages available via plugins.

Select Maven in the list of Build system .

Specify project’s SDK (JDK) or use the default one.

The selected Add sample code option will create a file with a basic code sample.

In Advanced Settings , specify the following Maven coordinates that are added to the pom.xml file:

GroupId — a package of a new project.

ArtifactId — a name of your project.

For more information on Maven coordinates, see Maven naming conventions.

IntelliJ IDEA creates a Maven project with the pom.xml file that includes compiler and target versions of Java, dedicated Maven tool window, and all the necessary dependencies to start your work.

The version of Java specified in the pom.xml file overrides the version specified in the JDK for Importer option, located in the Maven settings.

Create a new project with Maven archetype

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

Select Maven Archetype from the options on the left.

Using options on the right, configure your project:

Name : specify the name of your project.

Location : specify the location of your project.

Create Git repository :

Select the Create Git repository to place the new project under version control.

You will be able to do it later at any time.

JDK : specify project’s SDK (JDK) or use the default one.

Catalog : specify which Maven repository you want to use in your project.

The internal Maven catalog is a default one. You can also select the local repository that is located in the .m2 directory, or you can select Maven central repository.

You can also click Manage Catalogs and add a custom repository if you need.

Archetype : select the needed Maven archetype. Click Add to specify the custom archetype.

Version : the version is specified automatically.

Additional properties : add additional properties

If you are creating a project using a Maven archetype, IntelliJ IDEA displays the Maven settings that you can use to set the Maven home directory and Maven repositories. Also, you can check the archetype properties.

To modify or add archetype catalogs later in your project, refer to the Archetype Catalogs settings.

Create a Java EE project with Maven as a build tool

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

Select Jakarta EE from the list of generators.

Leave the default option Maven as your build tool system.

Select additional libraries and frameworks.

Specify your project’s name, location, and artifact coordinates. Click Create .

IntelliJ IDEA creates a Maven project with the dedicated Maven tool window and adds necessary dependencies.

For the more detailed information, refer to Tutorial: Your first Java EE application.

Open an existing Maven project

From the main menu, select File | Open .

Alternatively, click Open on the welcome screen.

If you have some custom plugins that require you to import your project from the IntelliJ IDEA model, press Ctrl+Shift+A and search for the Project from Existing Sources action.

In the dialog that opens, select the pom.xml file of the project you want to open.

In the dialog that opens, click Open as Project .

IntelliJ IDEA opens and syncs the Maven project in the IDE. If you need to adjust importing options when you open the project, refer to the Maven settings. For example, if you have Maven options configured in the maven/config file and want to use them in your project, select the Use settings from .mvn/maven.config option in the Maven setting.

If you have a Maven project with the configured Maven wrapper then IntelliJ IDEA will take the Maven version from the maven.properties file and add it as the Maven home path.

Configure the Maven settings before opening a new project

You can configure IntelliJ IDEA to display Maven settings before you open a new Maven project. (A new Maven project in this case is the existing project that you open in IntelliJ IDEA for the first time.) Since these are application-level settings, they are applied to any new project you open.

On the Welcome screen, select Customize | All Settings .

Alternatively, from the main menu, select File |New Project Setup | Preferences for New Projects .

In the Settings dialog, select Build, Execution, Deployment | Build Tools | Maven .

In the Maven settings dialog, select the Show settings dialog for new Maven projects .

Click OK to save the changes.

Next time you open a new project, IntelliJ IDEA displays the Maven Settings dialog where you can specify a location of the local Maven repository and a user settings file.

Add a new Maven module to an existing project

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

In the Project tool window, right-click the project folder and select New | Module . Alternatively, from the main menu, select File| New | Module to open the New Module wizard.

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 Name field. The rest of the information is added automatically and you can use either the default settings or change them according to your preferences.

Configure a multi-module Maven project

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

If you have the Maven version 3.7 or later installed, IntelliJ IDEA will support importing a project from the MNG-6656 model.

In this case when you open a child POM file in the editor, you don’t have to specify a version inside the parent tags if it is located in the relativePath by default. If you add a dependency to a module in the same project, you don’t have to specify a version of the dependent module.

Create a Maven parent project. IntelliJ IDEA creates a standard Maven layout including an src folder.

In the Project tool window, remove the src folder since you would only need it in the very rare cases. For your general project, you don’t need the src folder for the parent POM.

In the Project tool window, right-click your project (or from the main menu, click File ) and select New | Module to add a sub project.

In the New Module wizard following the instructions on how to add a module, specify the necessary information and click Finish .

The src folder is 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.

the parent POM

In a multi-module project, the parent POM needs to have a pom packaging.

Last, but not least, IntelliJ IDEA adds the description of the parent POM to the sub project’s POM.

the subproject POM

You can click in the left gutter to quickly open the parent POM from your sub project.

You can also add dependencies to the parent POM that will be inherited by the sub projects.

Parent POM: added dependency

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

Access the Maven settings

Use the Maven settings to configure options such as Maven version, local repository, offline mode, and so on.

In the Settings dialog ( Ctrl+Alt+S ), go to Build, Execution, Deployment| Maven .

Click on the toolbar, in the Maven tool window to access the Maven settings.

On the Maven settings page, configure the available options and click OK to save the changes.

Use the Maven wrapper or a custom version of Maven

Since the Maven2 support is not bundled with IntelliJ IDEA, download and enable the Maven2 Support plugin.

For a custom version, Download the needed Maven version on your computer.

From the main menu select File | Settings | Build, Execution, Deployment |Build Tools | Maven .

On the Maven settings page, in the Maven home directory field, specify the location of the Maven custom version installation. For the Maven wrapper, select the use Maven wrapper option from the list. In this case the version of Maven defined in the .mvn/wrapper/maven-wrapper.properties file will be used.

Change the JDK version in a Maven project

There are several places where you can change the JDK version that will affect not only your current project, but the whole application as well.

Change the JDK version in the Project Structure

Changing the JDK version in the Project Structure dialog will only affect the current project.

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S .

In the dialog that opens, in Project SDK , specify the JDK version and click OK to save the changes.

Change the JDK version for the Maven runner

When IntelliJ IDEA runs Maven goals, it will use the JDK version specified for the Maven runner. By default, IntelliJ IDEA uses the project’s JDK.

Changing the JDK for the Maven runner will only affect the current project.

In the Settings dialog ( Ctrl+Alt+S ), go to Build, Execution, Deployment | Maven | Runner .

On the page that opens, in the JRE field, select the JDK version.

Change the JDK version for the Maven importer

Changing the JDK version for the Maven importer will affect the whole application since it is a part of the Maven global settings. If you want to use the same JDK version as you use in your project for syncing or resolving dependencies, change the JDK version for the importer.

In the Settings dialog ( Ctrl+Alt+S ), go to Build, Execution, Deployment | Maven | Importing .

On the page that opens, in the JDK for importer field, select the same JDK version as you used in the Project Structure and click OK to save the changes.

Add Maven support to an existing project

You can open an existing non-Maven project and add a Maven support via IntelliJ IDEA UI.

Add Maven support

Open an existing project, for example, a Java project.

In the Project tool window, right-click your project and select Add Framework Support .

In the dialog that opens, select Maven from the options on the left and click OK .

Add Maven support

IntelliJ IDEA adds a default POM to the project and generates the standard Maven layout in Project tool window.

Maven project view

IntelliJ IDEA also creates a corresponding structure with Lifecycle and Plugins in the Maven tool window.

Open the generated POM and specify a groupId . The artifactId and version are specified automatically.

For more information regarding Maven naming conventions, refer to the Maven documentation.

Generated POM

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

At this point you can further develop your project using Maven. We recommend making all your project changes in POM since IntelliJ IDEA considers pom.xml as a single source of truth.

Create an executable JAR

You can conclude the following optional steps to create an executable JAR.

Click to build project. IntelliJ IDEA generates target folder. Note that IntelliJ IDEA only compiles sources and doesn’t create either JAR file or Manifest file.

Create a Manifest file in the resources directory.

Right-click the directory, select New | Directory to create the META-INF subdirectory. Then right-click the subdirectory, select New | File to create the MANIFEST.MF file.

Open the MANIFEST.MF file in the editor and add information about your main class.

Check the following code:

Alternatively, we can ask Maven to add this line of code into the MANIFEST.MF file with the following code in pom.xml :

In your POM specify the Manifest file information, so you can use Maven to generate an executable jar file.

POM with manifest

Alternatively, you can execute package instead of the install command to achieve the same result.

In the Maven tool window, in the Lifecycle list, double-click the install command to generate the jar file. IntelliJ IDEA generates the appropriate information in the target folder and an executable JAR in the Project tool window.

Generated JAR

You can right-click the generated JAR and select Run to execute the file.

If the existing project contains more than one module, converting such a project into the Maven project becomes quite challenging. In this case we recommend that you create an external POM where you describe your project and open your POM as you would open a regular Maven project.

Importing a Maven project

While it's possible you may want to create your own Maven project, it's much more likely you'll import an existing one. This is very straightforward in IntelliJ IDEA. Select Open from the Welcome Screen, or the File menu, and navigate to the location of the code.

You can select the root of the project and IntelliJ IDEA will figure out how to import the project, but if you specifically know this is a Maven project and you want to use this model for your project structure, it's simplest to select the top-level pom.xml file, and the IDE will know you mean to import a Maven project.

open

Depending upon the version of IntelliJ IDEA you're using, you be shown specific options for importing a Maven project — generally, we'll want to keep the default settings. In the current version of IntelliJ IDEA (2021.2 at the time of writing) you may be shown a series of dialogs:

open as project

If you selected the pom.xml file to open the project (rather than the top level folder), IntelliJ IDEA will ask if you want to open just the file, or import the whole project. In this tutorial, we want to Open as Project.

trust project

If you're opening a project that's not yours (e.g. cloned from GitHub), you may not want to trust the project as it's running code you don't know on your machine. However, if you know the project and you believe the code and the dependencies it downloads are safe, select Trust Project.

IntelliJ IDEA will use the Maven definition to figure out the structure of the application, and of course download all the required dependencies and add them to the project.

Добавить maven в IntelliJ IDEA в обычный java проект

добавить в IntelliJ IDEA в обычный java проект ? Не вручную, cкачивая jar.

Поздний ответ, наверное, но для истории:

Можно в корень обычного проекта в IDEA добавить pom.xml (хоть из другого проекта), подправить ему group, artifact, зависимости. Потом в контекстном меню для этого файла выбрать «Add as Maven Project». Проект станет теперь maven, дальше уже настраиваем его как обычный maven-проект.

Update простейший XML файл имеет вид

    Нажать на папку с проектом правой кнопкой

Выбрать «Добавить поддержку фреймворка» введите сюда описание изображения

Выбрать из списка Maven

Lukyanov Mikhail's user avatar

Можно выгрузить библиотеку из Maven.
Для этого:
1. Войти в структуру проекта;

2. В разделе «Библиотеки» выбрать «Из Maven»;

3. Ввести путь к библиотеке (можно искать как по groupId, так и по artifactId).
введите сюда описание изображения

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *