how to use maven in intellij

Mastering Maven in IntelliJ: A Step-by-Step Guide

By Mark Thompson
AD

Whether you're a seasoned Java developer or just starting out, Maven is an incredibly powerful tool for managing your build process. And when it comes to integrating Maven into your development environment, IntelliJ IDEA is a top choice. This comprehensive guide will walk you through how to use Maven in IntelliJ, from setting up your project to running build commands and more.

Before we dive in, ensure you have IntelliJ IDEA installed on your system. The examples in this guide use IntelliJ IDEA Ultimate, but the steps should be similar in the Community Edition as well. Also, make sure you have Apache Maven installed and added to your system's PATH.

Setting Up a Maven Project in IntelliJ IDEA

To start, let's create a new Maven project in IntelliJ. Open IntelliJ IDEA and click on "Create New Project". In the dialog box that appears, select "Maven" from the list of project templates and click "Next".

AD
Java 9 Modules (Part 2): IntelliJ and Maven
Java 9 Modules (Part 2): IntelliJ and Maven
How to Convert a Project to Maven Project In Intellij
How to Convert a Project to Maven Project In Intellij
Top 21 Maven Interview Questions with Answers for Java Programmers
Top 21 Maven Interview Questions with Answers for Java Programmers

On the next screen, choose whether you want to create a new project from scratch or import an existing one. For this guide, let's create a new project. Name your project, select a location on your file system, and choose the Java version you want to use. Click "Next" to proceed.

Project Structure

IntelliJ IDEA will create a project structure based on the Maven conventions.You'll see folders like 'src/main/java', 'src/test/java', 'target', etc. 'src/main/java' is where your main application code will go, 'src/test/java' is for your tests, and 'target' is where Maven will store its build outputs.

Maven Tutorial for Beginners 5 - How to create a jar file with Maven
Maven Tutorial for Beginners 5 - How to create a jar file with Maven
A tutorial about Maven. Maven 3.0.4: Setup web application with Jetty server. Includes examples.
A tutorial about Maven. Maven 3.0.4: Setup web application with Jetty server. Includes examples.

The 'pom.xml' file in the root directory is your project's build file, where you'll define your project's dependencies. Let's add a dependency to demonstrate:

  • Open 'pom.xml' and find the section.
  • Add the following to define a dependency on the 'slf4j-api' artifact:
```xml org.slf4j slf4j-api 1.7.36 ```

dependencyManagement and plugins

In addition to , you might also see and sections. is used to manage the versions of the dependencies. are used to configure the build plugins.

AD

Next, let's see how to use these settings to run a Maven build.

Running Maven Builds from IntelliJ IDEA

Now that we have a simple Maven project set up, let's run a build. We'll start with the default 'clean package' goal.

To run a build, open the 'Run/Debug Configurations' dialog (Ctrl+Shift+Alt+F10 on Windows/Linux, or Cmd+Shift+Alt+F10 on Mac). Click on the '+' icon and choose 'Maven from the list of templates. Name your configuration, select the 'clean package' goal, and click 'OK'.

AD

Run the Build

Now, you can run your build by clicking on the run icon next to your configuration in the 'Run/Debug Configurations' dialog. Or, you can use themaven.run.as.test shortcut (Ctrl+Shift+A, 'maven.run.as.test').

Once the build is complete, IntelliJ IDEA will show you the output in the 'Run' tab at the bottom of your screen.

Inspecting the Build Output

The 'target' folder now contains several files that Maven has created. The 'classes' folder contains the compiled bytecode, 'test-classes' is for test classes, and 'my-app-1.0-SNAPSHOT.jar' is your project's JAR file.

AD

You can use IntelliJ IDEA to inspect this build output. Open the 'target' folder in Project view, ROS or Opening files and folders, then right-click and choose 'Open in External Tools'. This will open the 'target' folder in your file system's default explorer.

IntelliJ IDEA and Maven work together seamlessly, providing a powerful development environment. From here, you can explore other Maven goals, configure plugin settings, and even use IntelliJ IDEA's JavaEE support to deploy your application to a server.

Happy coding!"

AD