Importing, running, and debugging Maven projects in IntelliJ IDEA
Tags: Java, Spring Boot, Tutorial, Vaadin, Web Development

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.

To start, download a Spring Boot-based Vaadin starter project: Download 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. 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.

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.

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:

Tomcat started on port(s): 8080 (http) with context path ''

Started Application in 80.189 seconds (JVM running for 83.42)

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:

If your application is still running from the previous step, click the red stop-button to terminate it.

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.

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

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. In IntelliJ, have a look at the highlighted code line and the debug panel in the lower part of the screen.

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.

Play around with the debugger to familiarize yourself with it. If you want to learn more, JetBrains has an excellent resource on using the debugger.

Click Resume Program when you are done.

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.

Start by downloading the LiveReload plugin for your browser:

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

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. When LiveReload is running, verify that it works by making a change in the code: Create a new H1 heading and add it as the first argument in the add() method on the last line in MainView.

MainView.java

add(new H1("Hello world"), textField, button);

Click the build icon in IntelliJ (next to the run targets dropdown)

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.

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