How to run the project
Perform the following steps to run a Maven project in the Eclipse IDE:
- Extract the project ZIP package that we created in the previous topic and open Eclipse.
- We are going to import our project into the Eclipse IDE. To start the import process, select the File | Import menu and the import wizard will be opened. The following screenshot shows the first page of the wizard:
- In the first phase, you should select Existing Maven Projects from the list under the Maven folder, and then go to the next phase by pressing the Next button. The following screenshot shows the second step of the import wizard:
- In this phase, select the extracted project folder by pressing the Browse... button. Then, Eclipse finds the pom.xml file from the root of your project folder and shows it inside the Projects section of the window.
- Press the Finish button to finalize the import. If everything went correctly, you should see the cardatabase project in the Eclipse Project Explorer. It takes a while before the project is ready because all the dependencies will be loaded by Maven after importing. You can see the progress of the dependency download at the bottom-right corner of Eclipse. The following screenshot shows the Eclipse Project Explorer after a successful import:
The Project Explorer also shows the package structure of our project, and now, at the beginning, there is only one package called com.packt.cardatabase. Under that package, is our main application class, called CardatabaseApplication.java.
- Now, we don't have any functionality in our application, but we can run it and see whether everything has started successfully. To run the project, open the main class by double-clicking on it and then pressing the Run button in the Eclipse toolbar, or select the Run menu and press Run as | Java Application:
You can see the Console view open in Eclipse, and that contains important information about the execution of the project. This is the view where all log texts and error messages appear, so it is really important to check the content of the view when something goes wrong.
Now, if the project was executed correctly, you should see the Started CardatabaseApplication in... text at the end of the console. The following screenshot shows the content of the Eclipse console after our Spring Boot project has been started:
In the root of our project, there is the pom.xml file, which is the Maven configuration file for our project. If you look at the dependencies inside the file, you can see that there are now dependencies that we selected on the Spring Initializr page. There is also a test dependency included automatically without any selection. In the following chapters, we are going to add more functionality to our application, and then we will add more dependencies manually to the pom.xml file:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Let's look at the Spring Boot main class more carefully. At the beginning of the class, there is the @SpringBootApplication annotation. It is actually a combination of multiple annotations, such as the following:
The following code shows the Spring Boot application's main class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CardatabaseApplication {
public static void main(String[] args) {
SpringApplication.run(CardatabaseApplication.class, args);
}
}
The execution of the application starts from the main method, as in standard Java applications.