Java Guide: Setting Up and Running an Existing Maven Project in IntelliJ

In my github repository, i have prepared a very simple java project in maven. If you not use github and just wanna know how to setting up an unzipped Project, you will also find the answer here.
This article shows how to setup IntelliJ to build and run this project on MacOS, also the JUnit Test will be executed when everything prepared.
If you have Windows or Linux, the setting up steps should be more or less the same.
Table of Content
1. Open Github Project in IntelliJ
2. (Alternative) Open unzipped Project in IntelliJ
3. Setting up Java in Local Environment
4. Setting up IntelliJ
5. Build and Run the Project
1. Open Project in IntelliJ
1.1 Download and Install IntelliJ
On the official website of IntelliJ, there are free version (Community) and commercial version (Ultimate, 30-day trial) for download.
For this article, you can use either of them. I choosed the Ultimate version.
During install process, i just choose the default options.
1.2 Open Project
1.2.1 Use Github Project
If you wanna use the project from my Github repository, you just need to import the project into IntelliJ by using the URL provided by Github.
Firstly ,click on “Get from VCS” after IntelliJ is opened.


Then, open this Github repository from browser and click on Code -> HTTPS -> copy:

Last but not least, paste this URL to the IntelliJ URL input field which was shown at the beginning step, the Directory input field will be updated automatically when the URL input field was changed.
Then, click on the Clone button on the bottom right.

The Project should now be imported into the IntelliJ:

2. (Alternative) Open unzipped Project in IntelliJ
If you wanna know know to setting up an unzip project, you can download the project by using “Download Zip” button from the github repository, after that, please unzip the project.

Then, please open IntelliJ and click on “Open” button.

Then, please navigate to the project folder and click on Open:

The project structure should be shown now in IntelliJ as same as that from the end of step 1:

3. Setting up Java in Local Environment
3.1 Check Version
Usually, IntelliJ has built-in JDK and Maven.
As Java project may use different version of JDK, the Java Version should be installed in the local environment. Details can be found in maven official website.
It is also possible to define the version of maven to be used in pom.xml, in this case, the right version maven should also be used. Details can be found here and also here. As maven is not specified in this project, we use the built-in maven from IntelliJ.
In the pom.xml of this project, following codes show that Java 17 is used.
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
3.2 Install JDK in local Environment
I used following step to install Java 17 on my computer. Details can be found in my previous blog.
java -version
# Ignore the output if it said "no matches found:"
sudo rm -fr /Users/$(whoami)/Library/Java/JavaVirtualMachines/*
# Ignore the output if it said "no matches found:"
sudo rm -fr /Library/Java/JavaVirtualMachines/*
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew uninstall openjdk@11
brew install openjdk@17
sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
java -version4 Setting up IntelliJ
This is the main part of this article.
4.1 (Optional) Use new UI of IntelliJ
Choose the checkbox “Enable new UI” from IntelliJ -> Preferences -> Apperance & Behavior -> New UI.
4.2 Define Java in IntelliJ for current Project
Open your project, click on Menu -> File -> Project Structure -> Platform settings -> SDK, then add JDK 17 which you installed before.


Double check that in Menu -> File -> Project Structure -> Project Settings that JAVA 17 is used. Also the Language level in Project Settings -> Modules should suitable for the Java Version.


4.2 Check Maven in IntelliJ for current Project
Check that “Maven home path” is set in IntelliJ -> Preferences ->Build, Execution, Deployment -> Build Tools -> Maven:

Also check that right JRE (JAVA 17) is defined in IntelliJ -> Preferences ->Build, Execution, Deployment -> Build Tools -> Maven->Runner.
4.3 Set source root in IntelliJ
Mouse right click on “java” folder of “src/main”, click on “Make Directory as” -> “Source Root”.

The Structure of project looks like this now, i.e., org.example from src is shown as a package. Also org.example from test folder is shown as a package.

5. Build and Run the Project
Click on dropdown list “Current File” -> “Edit Configurations” from toolbar.

Then click on “+” Icon and click on entry “Maven”:

Please input “clean install” as options in the input textfield for RUN. You can also double check the Maven and Java Options. Maven home should be shown and Java 17 should be used:

Click on “OK” button, you will see the project name is shown left to Run and Debugger Icon on the top right:

Click on the Run Icon, the project will be built. In the target folder, a jar file with dependencies is built (debug-step-back-demo-1.0-SNAPSHOT-jar-with-dependencies.jar), this is because in pom.xml we have defined, that a jar with all dependencies should be built.

If you search the text “test” in the output message, you will find the the unit tests are executed and passed. This is because the dependencies and plugins in pom.xml has been defined be able to run JUnit5, also test belongs to the phase of “mvn clean install”.

Click on the Terminal Icon in IntelliJ, cd into target folder and run the jar file, the project will be started and you will see the output.
cd target
java -jar debug-step-back-demo-1.0-SNAPSHOT-jar-with-dependencies.jar
Alternative, you can also run the code and test directly from source:


Thank you for reading. I hope you enjoyed today’s content.
You are welcome to my network:
Follow me on Medium





