How to add maven to project idea

How to add maven to project idea

Maven. Importing

Keep projects files in

Store generated project files externally

For the already linked projects, whatever you have previously selected remains intact. For newly imported projects, this option is selected by default.

It is helpful in the following cases:

correctly opening a project that contains both regular IntelliJ IDEA modules and Maven modules.

Detect compiler automatically

Create IntelliJ IDEA modules for aggregator projects (with ‘pom’ packaging)

If this checkbox is selected, IntelliJ IDEA Maven Modules will be created for each module included in the pom.xml file of an aggregative project, provided that its packaging is set to ‘pom’.

Create module groups for multi-module Maven projects

If this checkbox is selected, IntelliJ IDEA will create a module group from an aggregative Maven project, with the nested modules included in this group.

Keep source and test folders on reimport

If this checkbox is selected, all the source and test folders will be preserved on every import.

If this checkbox is cleared, all previously configured source and test folders will be removed on every import.

By default, this checkbox is set as follows:

For new projects : the checkbox is cleared.

For already imported projects : the checkbox is selected.

Exclude build directory PROJECT_ROOT/target

Select this checkbox to exclude a build directory from the project. This might be useful, if you want to speed up the project’s importing process. If this checkbox is cleared, IntelliJ IDEA will index files in the build directory every time you import a project, which might take additional time.

Use Maven output directories

If this checkbox is not selected, the build will be created in the regular IntelliJ IDEA’s output directory USER_HOME\IdeaProjects\

Create separate modules for production and test roots (experimental)

Use this option if you need to create a source-set like structure.

For example, you might have a production legacy code that uses 1.6 Java, but your tests use much later versions of Java. In this case, when you select this option, IntelliJ IDEA creates several source sets on your project’s reload.

Generated sources folders

Specify the directory of your source root when you reimport a project.

You can select one of the following options:

Detect automatically This is a default option. When you select this option, IntelliJ IDEA automatically detects the location of the generated sources. IntelliJ IDEA also detects which directory to mark as a source root. However, IntelliJ IDEA searches for the generated sources only in target/generated-sources and target/generated-sources/* directories.

target/generated-sources This option enables you to mark the directory as source root manually.

subdirectories of «target/generated-sources» This option enables you to mark a subdirectory as a source root manually.

Don’t detect This option lets you skip the detection process.

Phase to be used for folders update

Select a Maven phase to be used for folders update. This might be useful, if you adjust your plugins so that additional sources are loaded at some phase.

Select the corresponding checkboxes to automatically download sources ( Sources ) and documentation comments ( Documentation ) on opening Maven projects.

You can select the Annotations option that lets you automatically dowload custom annotations. IntelliJ IDEA searches for those annotations in the JetBrains repository and downloads them as Maven artifacts during the project’s import.

Use this field to specify dependency types that you want to include when you reimport your project.

VM options for importer

When you specify the options, follow the following rules:

JDK for importer

Use this list to specify which JDK to use when the maven project is imported.

You can choose one of the following options:

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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 How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideato 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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

Maven dependencies

IntelliJ IDEA lets you manage Maven dependencies in your project. You can add, import Maven dependencies, and view them in the diagram.

Add a Maven dependency

Open your POM in the editor.

Press Alt+Insert to open the Generate context menu.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

For more information on working with the Dependencies tool window, refer to Package Search.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

IntelliJ IDEA also adds the dependency to the Dependencies node in the Maven tool window and to the External Libraries in the Project tool window.

If the added dependency has its own transitive dependencies, IntelliJ IDEA displays them in both tool windows.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Enable annotation processors

Open your POM file.

Specify the annotationProcessors and annotationProcessorPaths options.

For example, check the following code:

For more information, refer to Maven.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Centralize dependency information

In a multi-module Maven project, the dependency in the parent POM will be inherited by all sub projects. You can use dependencyManagement to consolidate and centralize the management of the dependencies’ versions.

Open your POM in the editor.

Press Alt+Insert to open the Generate context menu.

From the context menu, select the Managed Dependency option that will show you the list of the dependencies that are defined in the dependencyManagement section of your parent POM in a multi-module project. IntelliJ IDEA also shows the list of dependencies from the BOM files.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

However, if you want to overwrite the defined version, you need to include version when you add the managed dependency to the POM.

Add a scope for the Maven dependency

You can add a scope for your dependency using POM. In this case IntelliJ IDEA will execute the dependency at the specified phase.

In your POM, in the dependency description add scope and using the code completion add the name of the scope.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Import your changes. The name of the scope is displayed in the Maven tool window. In the Project Structure dialog, on the Modules page you can see that the scope of the dependency is also displayed.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Note that changing dependency’s scope in the Project Structure dialog will not affect the pom.xml file.

You can also add a custom jar file as a dependency using the Maven scope system when you define your dependency. However, note that this dependency will only be available on your machine and you can use it only for the local deployment.

Work with Maven transitive dependencies

You can view transitive dependencies that were pulled in with the added or imported Maven dependency. You can check their versions, change them, or exclude those dependencies altogether.

The Maven tool window displays the direct dependency and all its transitive dependencies that were pulled in.

View the transitive dependency

In your project’s POM, press Ctrl and hover the mouse over the dependency.

Click the dependency to open the dependency’s POM.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

In the dependency POM, view the active dependency, its transitive dependencies and their versions.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

You can check the origin from which the dependency was pulled in.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Exclude the transitive dependency

You can exclude a transitive dependency if you want.

In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

You can use Exclude command from the context menu in the Maven dependency diagram to quickly exclude the specified dependency from POM and the respective tool windows.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

The dependency is also excluded from the Project and Maven tool windows.

Import Maven dependencies

You can import dependencies to your Maven project. When IntelliJ IDEA imports the added dependency, it parses the dependency and updates your project.

In the Maven tool window, press How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea. In this case you manually trigger the re-importing process of all projects and their dependencies.

View Maven dependencies as a diagram

Make sure that the Diagrams and Maven Extension bundled plugins are enabled.

IntelliJ IDEA lets you view and work with Maven dependencies in a diagram format.

Generate a diagram

In the Maven tool window, on the toolbar, click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaor select the appropriate option from the context menu.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

In the diagram window, IntelliJ IDEA displays the sub project and all its dependencies including the transitive ones.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

View the diagram structure

Generate a diagram.

Click Structure in the left toolbar. IntelliJ IDEA opens the Structure tool window that displays a map of your diagram with a small preview of the selected block and its neighbors.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Use the context menu to zoom in and out, move canvas, change layout, and export part of the diagram into an image.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

You can perform different actions while in the diagram window.

Change the visibility level

You can change the visibility level and, for example, view dependencies that have a specific scope (compile, test, and so on).

From the list, select the dependency scope you want to see. IntelliJ IDEA displays only the specified dependency scope.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Navigate to POM

You can easily navigate to POM from the diagram window.

You can check conflicts and duplicates by clicking How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaicon in the diagram window.

Check conflicts and duplicates

In the diagram window, click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaicon.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

The red arrow points to dependencies that contain duplicates or errors. IntelliJ IDEA also displays dependencies’ versions to help you resolve the conflicts. Double-click the dependency to open its POM.

Show path from the selection to the root

You can select dependencies and see how they are included into the project.

In the diagram window, select a dependency for which you want to see a connection to a project. If you want to select several dependencies at once, hold down Shift and make the selection.

Click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Show neighbors of the selected node

You can select dependencies and see what other dependencies are connected to the selected nodes. It might be helpful if you have a large diagram and want to focus on just a part of it.

In the diagram window, select a dependency you need. If you want to select several dependencies at once, hold down Shift and make the selection.

Click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Exclude a dependency

You can use a diagram to exclude a dependency from the project’s POM.

Select a dependency in the diagram window.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

From the list, select the module (if any) where the exclusion definition will be added. The selected dependencies will be removed from diagram, and the exclusion section will be added to the corresponding dependency in the module’s POM.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

You can undo this operation by pressing Ctrl+Z before you import the changes.

Dependency analyzer

Maven dependency analyzer lets you quickly see and analyze resolved dependencies, the unresolved ones, dependencies with conflicts, and transitive dependencies in your project and subprojects.

Analyze dependencies

On the toolbar in the Maven tool window, click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea.

Alternatively, in the Maven tool window, right-click the needed dependency and select Analyze Dependencies from the context menu. You can also right-click a module in the Project view and use the same action.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

In the Dependency Analyzer window, you can narrow down your analysis scope, check only the conflicted dependencies, or check usages.

If the duplicate dependency is found, it will be greyed out.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

For more information on available options and icons in the Dependency Analyzer window, refer to the reference section.

Dependency analyzer tool window reference

You can see scopes in which every dependency is used in the project. To narrow down the list of the dependencies based on their scope, select the appropriate option from the list of scopes.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Show Conflicts Only

Click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideato see only unresolved or conflicted dependencies.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaand select Show GroupId to display GroupId in the list of dependencies.

Toggle How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideato display the list of dependencies in a form of nodes or in the flat list.

The tree view will help you quickly check the transitive dependencies.

Use How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea/ How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaexpand or collapse the list of dependencies.

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 How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaif you want to add other languages available via plugins.

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

Select Add sample code if you want your project to have a file with a basic code sample.

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

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.

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

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.

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.

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Add a new Maven module to an existing project

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

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.

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

You can click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideain 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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Access the Maven settings

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

Click How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project ideaon 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

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

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

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

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

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.

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.

Maven and IntelliJ IDEA

Table of Contents

On February 10, 2021, we hosted the live stream Using Maven in IntelliJ IDEA by Java Champion and Java Group Leader Chandra Guntur, a very dear friend and amazing speaker. In this session, Chandra Guntur demonstrated some of his best tips for using Apache Maven with IntelliJ IDEA. You can find the slides from this presentation at this repository, and here’s the recording of the session. These tips could be useful no matter how much experience you have with Maven in IntelliJ IDEA.

I’d like to highlight some of the most exciting features of using Maven in IntelliJ IDEA. If you want a comprehensive account of the tools IntelliJ IDEA has for working with Maven, be sure to check out the full recording.

Visual representation of all dependencies in your pom.xml file

When you see a list of dependencies in your project, it can be easy to miss the transitive ones. One of my favorite features in IntelliJ IDEA is the ability to view all the dependencies in code visually. You can also save this visual representation as an image and share it with your team.

There are multiple ways to access this feature. In the Structure window, you can right-click on the dependencies node and select Diagrams, and then Show Dependencies… In the editor window, you can access the context menu by right-clicking and accessing Diagrams and then selecting Show Dependencies.

The same option is available in the Maven window as an icon, and it can also be accessed using shortcuts: Ctrl+Alt+Shift+U on Windows / Linux and ⌥⇧⌘U on macOS.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Assigning a shortcut to your Maven tool window

I often see developers who keep their tool window bars constantly on display because it seems to be the fastest way to access various tool windows. You can claim the real estate used by the tool window bars and quickly access tool windows like the Maven Tool window by using its shortcut (or assign one if none exists).

Assigning a shortcut to a tool window like Maven takes just a couple of steps, and they are the same steps you need to take to assign a shortcut to any tool window. Use Find Action by using Ctrl+Shift+A (or ⇧⌘A), search for ‘Maven’ with description ‘View | Tool Windows’. When the Find Action dialog appears, Press Alt+Enter to assign your shortcut. I used Alt+N.

There are other ways to assign a shortcut. For example, you can search for ‘Keymap’ in your IntelliJ IDEA settings, search for the ‘Maven tool window’, and then simply assign a shortcut to it.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Building Maven projects using archetypes

You can create new Maven projects using archetypes (an ‘archetype’ is a ‘template’). However, some archetypes might not be up-to-date with the latest Java or framework versions.

For example, when you use the Maven quickstart archetype, it might use an older version of the Java compiler and runtime than the one you selected for your project,as pom.xml values override the values you define in IntelliJ IDEA when creating a project. To fix such issues, you can open your pom.xml file and modify it. In this specific case (using the Maven quickstart archetype), you can open your project’s pom.xml file, search for the following text, and replace 1.7 with the Java version you want to use for your project:

If you are using the preview language features, you can also add the compilerArgs tag to your pom.xml file.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Quick tip: Most windows in IntelliJ IDEA use Type Ahead Search and let you search for text if you just start typing – you don’t have to look for the search text field. For example, in the preceding gif, I was able to search for the Maven quickstart archetype, simply by typing ‘quickstart’ in the search textbox, and without even pressing Enter. This can be quite useful when you can’t remember the exact or full name of an archetype or when there are a lot of options to choose from.

Additionally, if you are interested in creating your own archetypes, check out this guide.

Using different Maven versions

IntelliJ IDEA bundles Apache Maven so you don’t have to install it first to use it. This usually works well. However, if you are working with a team on a project, all the members of the team will probably want to work with the same version of Maven. In IntelliJ IDEA, switching to a different version of Maven is no problem.

When creating a new project, you can specify the Maven version to use. For existing projects, modify Maven home path in Settings.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Using pom.xml to open a Maven project in IntelliJ IDEA

Imagine you check out an open source project to work on, one that happens to be a Maven project. When you choose to open its pom.xml file in IntelliJ IDEA, the IDE detects that it can be opened either as a file or as a project. When you choose the latter, IntelliJ IDEA sets a project up and opens it for you. You can even accomplish this by simply dragging and dropping the project’s pom.xml file into IntelliJ IDEA.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Browsing through the structure of a Pom.xml using the Structure tool window

You might already be familiar with looking into the structure of your classes using the Structure tool window in IntelliJ IDEA. Did you know you can use the same tool window to browse through the structure of your pom.xml file? This can be useful for quickly navigating to its properties, dependencies, plugins, and other sections.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Other sections of the Maven tool window

In addition to being able to use the tool window’s icons to reload all Maven projects and show Dependencies as a diagram, you can also use the Maven tool window to view Maven lifecycle phases, plugins, run configurations, dependencies, and more. You can even use it to execute goals.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Jump to Source of pom.xml

While navigating the dependencies of your pom.xml file in the Maven tool window, you can use IntelliJ IDEA’s Jump to Source feature (F4 in Windows and Linux / ⌘Down on Mac) to view the details of the dependency in the editor. Navigate to the artifactId or groupId and use Jump to Source again to view the pom.xml file of the dependency.

Using this you can drill down and view any number of pom.xml files for the dependencies you want. When the editor window displays another pom.xml file, the contents of the Structure window will change accordingly.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Download sources and documentation

You can use the Maven tool window to download sources and documentation for specific dependencies in your project, or you can download all of them using a single command. This can be useful if you want to browse through the implementation details.

Life cycles – are there just a handful of them?

By default, the Maven tool window displays the most frequently used Maven lifecycles or phases. To view more phases than those listed in the Maven tool window, you can uncheck Show Basic Phases Only.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Using the Run window to track the execution of Maven phases

You can use the Maven tool window to select and execute multiple phases for your project, say verify and install. When you click on Run, the Run tool window appears and displays a collection of metrics, like the current execution phase and the time it takes to finish. It also marks the completed phases with a cross, allowing you to easily track which ones have been run successfully.

You can also click on the individual phases to view the messages specific to the execution of that phase. It is easy to read through the messages related to a phase, rather than wading through all the messages in one big chunk.

Creating and saving custom run configurations

Usually, all of the members of a team need to be working with the same Maven build or run configurations. In IntelliJ IDEA, you can create new run configurations, define the Maven goals you want to run for them, rename them, and store them as project files. Of course, you can also override the default directory where they are defined.

Saving these configuration files to your shared repository helps you share run configurations across teams.

Navigating to a module pom.xml from your project pom.xml

Imagine you have a multi-module project. While browsing through the list of modules in your root pom.xml, you can access a module’s pom.xml by using Ctrl+Click on the module’s name or by using Ctrl+B (or ⌘ B), which is a navigation feature in IntelliJ IDEA for accessing declarations or usages.

No implicit bi-directional relation between aggregator and child pom.xml

If a pom.xml file lists the coordinates of a parent pom.xml file (groupId, artifactId, and version), this doesn’t imply that the parent is aware of this child. Similarly, a pom.xml file might list modules as its children, but those child pom.xml files may or may not list any parents. In this case, the root.xml file is referred to as an aggregator pom.xml and not as a parent.

This is a powerful setup that allows a child or parent to execute commands or inherit values and properties without the other being aware of the relationship.

Using the IntelliJ IDEA terminal to run Maven commands

If you prefer executing Maven commands using the command line, you can use IntelliJ IDEA’s terminal to do so (meaning you don’t have to leave the IDE and start another application). Additionally, IntelliJ IDEA detects when you are executing Maven commands and reminds you that you can use the IDE to execute the command. For example, if you are using IntelliJ IDEA’s built-in terminal window to execute the Maven command ‘maven clean verify’, you just need to hit Ctrl+Enter (or Cmd+Enter) and IntelliJ IDEA will execute the command.

Using Run Anything to execute Maven commands

With the Run Anything feature, you can execute multiple goals with a Maven command. With code completion, you don’t even need to remember all the available options. You can actually perform a variety of tasks using the Run Anything window, including opening another project.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Dependencies Tool Window (new in IntelliJ IDEA 2021.2)

IntelliJ IDEA 2021.2 includes a new tool window – Dependencies, which can be used to update all your dependencies in a super convenient manner. It lists all your dependencies, their current version and the new versions that are available. You can upgrade all your dependencies or choose to upgrade selected ones. This saves you from browsing through the dependencies in your code base and checking for their newer version using tools external to IntelliJ IDEA.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Autocompletion in pom.xml file

One of the most used features in IntelliJ IDEA – auto-completion – is also available in your pom.xml file. You get the options to complete tags and their values too.

How to add maven to project idea. Смотреть фото How to add maven to project idea. Смотреть картинку How to add maven to project idea. Картинка про How to add maven to project idea. Фото How to add maven to project idea

Summary

IntelliJ IDEA provides a wide variety of features that allow you to work on your Maven projects more easily and efficiently.

Источники информации:

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

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