Spring Boot Project with Apache Maven

With Spring Boot and Apache Maven it is possible to create microservices in shortest time! In this tutorial I would like to show you how to create a Spring Boot project with Apache Maven. Later there will be more tutorials, which will build on this tutorial.
SpringBoot Maven Logo

Spring Boot – Create Spring Boot projects with Apache Maven

Einleitung

In this tutorial I would like to show you how to create a Spring Boot project with Apache Maven. With Spring Boot and Apache Maven it is possible to create microservices in shortest time!  This tutorial will be the beginning for further tutorials I will write. There will be a series of Spring Boot and Spring Cloud tutorials. This Spring Boot Tutorial will be the basis for the other tutorials.

Create Java Project with Apache Maven

By entering the following command in the Linux or Windows Console you create an empty Maven Project!

When creating a Maven project, it is important to adjust the parameters „-DgroupId“ and „-DartifactId“ to your own project needs. I have entered exemplarily „de.ertantoker.tutorial“ and „spring-boot-with-maven“.

After the project is created a directory with the name of the „artifactId“ appears in the same folder, which you entered before. In my case the name of the directory is „spring-boot-with-maven“. The directory contains a file named „pom.xml“ and a directory named „src“. Here are all source code files!

Create Spring Boot Project

Now you can open the newly created project in a source code editor of your choice. I will use IntelliJ from Idea in my tutorials.

The project now has the following directory structure.

spring boot apache maven

Customize Maven pom.xml file

To include Spring Boot in your projects we need to make some changes to the generated „pom.xml“. The POM file is the central configuration file for Maven projects.

In its current form, the pom.xml looks like this:

Include Spring Boot libraries

Embedding Spring Boot is quite simple. First the „pom.xml“ must be extended so that it becomes an extension of the Spring Boot Parent. The following changes have to be made to the pom.xml:

Activate Spring Boot in Java project

By generating the project via Maven, we also automatically created an App.java class. This class is quite simple in the current version and offers no special function. To turn a Java application into a Spring Boot application you need a special Spring Boot Annotation and a code in the main method of the App.java class.

The @SpringBootApplication annotation configures the application to a Spring Boot application. This is mandatory. The line of code „SpringApplication.run(App.class, args);“ is used to automatically start a Spring Boot application when the Java application is started.

Test Spring Boot Project

If we have done everything right we can check it with a short test on the console. Enter the following command in the console:

Now Maven starts to compile and build the application. At the end a new directory named „target“ appears. Only the Spring Boot application with the name „spring-boot-with-maven-1.0-SNAPSHOT.jar“ is located here. Now change to the directory „target“ and start the Spring Boot application with the following command:

Now the Spring Boot application is started. Please make sure that a JDK version 1.8 (Java 8) is installed on your system. It should also be mentioned that Spring Boot always starts on port 8080 by default. This port should not be occupied by you. If yes you have to close the application which uses this port. Alternatively you can use a configuration file (application.yml) to tell Spring Boot to start the application on a different port, I will show you how to do this in one of the next tutorials.

The Spring Boot application is now started. We can see this from the last line: „2018-04-24 18:01:58.992 INFO 26292 — [ main] en.ertantoker.tutorial.App : Started App in 1.899 seconds (JVM running for 2.278)“.

The result

After the Spring Boot application has started it does not offer any real functionalities or methods, so we will add a RestController to the Spring Boot application in the next tutorial!

I hope you enjoyed my little introductory tutorial into the Spring Boot world. If you have any questions, criticism or suggestions, please feel free to comment on the tutorial.

Verwandte Beiträge

Leave a comment

Diese Website verwendet Cookies. Durch die Nutzung unserer Services erklären Sie sich damit einverstanden, dass wir Cookies setzen. weitere Informationen

Diese Website verwendet Cookies. Durch die Nutzung unserer Services erklären Sie sich damit einverstanden, dass wir Cookies setzen.

Schließen