Importing, running, and debugging Maven projects in IntelliJ IDEA

please click the link for the video version of the topic: https://youtu.be/B-FouixXDPE

The first step in any project is to set up the project and get a base app running.

In this section, you’ll learn:

  • How to import a project starter into your IDE.
  • How to set up your browser to automatically show updates as you build your application.

Importing a Maven project into IntelliJ

Maven is the most popular project management tool for Java. It takes care of managing your project structure and dependencies and builds runnable artifacts.

  1. To start, download a Spring Boot-based Vaadin starter project:
  2. Download
  3. Unzip the downloaded archive to a file location of your choice. TIP: Avoid unzipping to the download folder, as you could unintentionally delete your project when clearing out old downloads.
  4. In IntelliJ, select Open in the Welcome screen or File menu.

5. Find the extracted folder, and select the pom.xml file.

6. Select Open as Project. This imports a project based on the POM file.

7. IntelliJ imports the project and downloads all necessary dependencies. This can take several minutes, depending on your internet connection speed.

When the import is complete, your project structure will be similar to this:

  • Java source files are in the src/main/java folder.
  • Test files are in the src/test folder (we’ll refer to these later).

Running a Spring Boot project

Spring Boot makes it easier to run a Java web application because it takes care of starting and configuring the server.

To run your application, run the Application class that contains the main method that starts Spring Boot. IntelliJ automatically detects that you have a class with a main method and displays it in the run configurations dropdown.

To start your application:

  • Open Application.java and click the play button next to the code line containing the main method.
  • After you have run the app once from the main method, it will show up in the run configurations dropdown in the main toolbar. On subsequent runs, you can run the app from there.

The first time you start a Vaadin application, it downloads front-end dependencies and builds a JavaScript bundle. This can take several minutes, depending on your computer and internet speed.

You’ll know that your application has started when you see the following output in the console:

COPY

You can now open localhost:8080 in your browser. You’ll see a Say hello button and Your name field on the screen. Enter your name and click the button to see the notification that displays.

Debugging with IntelliJ

The debugger is a useful tool to understand what’s happening in your code when things are not working as expected. Knowing how to use the debugger saves you from having to use a lot of System.out.println’s.

To use the debugger in IntelliJ:

  1. If your application is still running from the previous step, click the red stop-button to terminate it.
  1. Start your application in debug mode, by clicking the bug icon next to the play button. You can now insert a debug point. This tells the debugger to pause the app whenever it gets to the line marked in the code.
  1. You can now insert a debug point. This tells the debugger to pause the app whenever it gets to the line marked in the code.
  2. In MainView.java, on the line containing Notification.show, click next to the line number to add a breakpoint (debug point). When you click, the code line is highlighted and a red dot displays.
  1. If you now open http://localhost:8080 in your browser and click the Say Hello button, nothing happens. This is because the application stops on the line indicated in the IDE.
  2. In IntelliJ, have a look at the highlighted code line and the debug panel in the lower part of the screen.
  1. In the debug panel, you can see values for all variables. There are also controls that allow you to run the app one step at a time, to better understand what’s happening. The most important controls are:
  • Step over: Continue to the next line in the same file.
  • Step into: Drill into a method call (for instance, if youwanted to see what’s going on inside service.greet()).
  • Step out: Go back to the line of code that called the methodyou’re currently in.
  1. Click Resume Program when you are done.
  1. Your code will now run normally and you’ll see the notification in your browser.

Enabling live browser reload

One final thing to do before starting to program is to enable live reloading of changes. This provides a far better development experience. All code changes you make are automatically displayed in the browser, without the need to refresh the page manually.

  1. Start by downloading the LiveReload plugin for your browser:
  1. Install the plugin, reload your browser window, and click on the LiveReload icon in the top bar of your browser. (Make sure your app is running when you do this.)
  1. The middle of the icon should turn solid to indicate that LiveReload is working and has connected to your app. If it doesn’t, try refreshing the page or reloading the browser.
  2. When LiveReload is running, verify that it works by making a change in the code:
  3. Create a new H1 heading and add it as the first argument in the add() method on the last line in MainView.
  1. Click the build icon in IntelliJ (next to the run targets dropdown)
  1. The first time you make a change with the debugger active, you’ll see a “Reload changed classes now?” dialog. Select Do not ask again and click No. Spring Boot DevTools will take care of the reload for us.
  1. If all goes well, you’ll see a notification that the build was successful, and your browser will reload automatically to show the change. Magic.

please click the link for the video version of the topic: https://youtu.be/B-FouixXDPE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store