avatardatatec.studio

Summary

The website provides a detailed guide on setting up and running an existing Maven project in IntelliJ IDEA, including steps for importing the project from GitHub, configuring Java and Maven, and executing the project and its tests.

Abstract

The guide outlines the process for developers to import a Java Maven project from a GitHub repository into IntelliJ IDEA on MacOS, although the steps are applicable to Windows and Linux as well. It covers the installation of IntelliJ, both the Community and Ultimate editions, and the importing of the project via a GitHub URL or from a downloaded zip file. It also explains how to set up Java 17 in the local environment, configure the JDK and Maven within IntelliJ, and execute the project with a focus on running JUnit tests and building the project using Maven commands. The guide ensures that by following these steps, developers can successfully build and run the project, including executing unit tests and generating a JAR file with dependencies.

Opinions

  • The author believes that IntelliJ's built-in JDK and Maven are usually sufficient for Java projects but emphasizes the importance of using the correct Java version as specified in the project's pom.xml.
  • The author suggests that using the new UI of IntelliJ can enhance the user experience and provides instructions on how to enable it.
  • There is a preference for defining the JDK and Maven settings at the project level rather than globally, ensuring compatibility with the project's requirements.
  • The author values the importance of proper configuration of source roots within IntelliJ to reflect the project's package structure accurately.
  • The guide assumes that readers will appreciate the inclusion of terminal commands for installing OpenJDK 17 on MacOS, linking to a more detailed blog post for this purpose.
  • The author demonstrates a commitment to best practices by including the execution of unit tests as part of the build process, which is automated through Maven's lifecycle phases.
  • The guide is written with an assumption that readers will benefit from a step-by-step approach to building and running the project, culminating in the execution of the application from both the JAR file and directly from the source code.

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.

Open IntelliJ and Import Project
Ask Github URL

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

Copy URL of Github Repository

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.

Git Import

The Project should now be imported into the IntelliJ:

Project structure

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.

Download Zip file

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

Open Project

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

Import unzipped project

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

Project Structure

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>
JAVA Version in maven

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 -version

4 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.

Structure
SDK

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.

Java Settings
Language Level for Java 17

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:

Maven Settings

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”.

Mark java folder as Sources 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.

Package Known

5. Build and Run the Project

Click on dropdown list “Current File” -> “Edit Configurations” from toolbar.

Edit Configurations…

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

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:

mvn clean install

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

Edit Configurations

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.

Build Result

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”.

Test Results

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
Output of Application

Alternative, you can also run the code and test directly from source:

Run Application
Run Unit Test

Thank you for reading. I hope you enjoyed today’s content.

You are welcome to my network:

Follow me on Medium

Java
Maven
Intellij
Setup
Junit 5
Recommended from ReadMedium