DEV Community

Chrissy Vinco
Chrissy Vinco

Posted on

Maven for Spring Boot

I have recently decided to dive into the world of Spring Boot to expand my backend knowledge. A part of this process was also learning about Apache Maven which serves as a build automation and project management tool. This concept could be quite difficult to grasp for beginners (just as I initially found it a bit tough to understand too) which led me to write an article that could make it more approachable for fellow newcomers.

One could imagine Maven as a task master or project manager of Spring Boot. With Spring Boot as the application framework, it relies heavily on Maven to handle the building, testing, and packaging the whole application.

Spring Boot and Maven

Project Managers for Maven and Spring Boot

As developers, we have the power to create great applications with Spring Boot which includes the process of designing the architecture, writing logic, defining endpoints, configuring components, and implementing features that elevate the application's performance, utility and efficiency. Yet, as software engineers, we must understand that crafting code is just a piece to a puzzle considering that we must also need to build and manage the projects being worked on. This is where Maven comes in. Its role is to understand which steps to take to transform the source code to a fully functioning application. In order for both Spring Boot and Maven to function together, they both communicate through a pom.xml file - the Project Object Model which includes the set of tasks from Spring Boot that Maven needs to do. This file acts as the bridge to make sure that these two important tools collaborate seamlessly to create a fully-functioning application.

Task Delegation with Maven

Just as I first started, determining what type of tasks I could assign to Maven could be quite puzzling. So, allow me to give you a list in order to provide some clarity:

  • Dependencies

Let me start with dependencies. A dependency is the secret sauce needed to make the project shine. It is something that we need in order for our project to work properly.

Technically, it is a code or a specific library that we could use to perform specific tasks. Sometimes, to implement a feature, we would need to write multiple lines of code from scratch. However, thanks to the pre-made code that are covered by the dependencies, we are able to save time and effort.

Maven then steps in as the project manager by overseeing the project's dependencies through the pom.xml file which outlines the project's initial needs including essential libraries and frameworks such as Spring Boot.

Here are some of the different examples of the common dependencies being used by Maven and where they come into play:

spring-boot-starter-web is our all-in-one package for web applications. It includes Spring MVC which is one of the most important factors as it handles HTTP requests, and embedded web server configurations to name a few.

spring-boot-starter-security is used when security is a concern for our web application (which it definitely should be) and is in charge of the authentication and authorization, strengthening the security of the project.

spring-boot-starter-test is used for unit tests and integration testing in Spring Boot just to name a few.

spring-boot-starter-data-jpa is used to simplify the Java Persistence API (JPA) to interact with databases.

A code block below shows how we can add a specific dependency to our pom.xml file:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode
  • Structure

Organization is one of my favorite things about maven as it follows a specific convention with its predefined directory structures and naming conventions for building the project. Here are some examples of the defaults for project management:

src/main/java is the home for your Java source code

src/main/resources is the designated spot for non-Java resources which includes the configuration files and static assets.

src/test/java is where test code is placed to ensure a clear separation between production and testing code.

  • Lifecycle

Maven also takes care of defining the lifecycle that includes the multiple phases of the program that span from code compiling, testing, packaging, installation and deployment. Each phase is aligned to a specific task which will be executed based on the specific Maven command.

Here are some specific tasks that we can ask Maven to perform:

compile triggers the build lifecycle as it compiles the source code in src/main/java directory. This also check our code for errors.

test Maven executes the tests that are found in the src/test/java directory and uses frameworks such as JUnit or TestNG to execute unit tests, integration tests, and other tests that we've written.

run Running the application is simplified by executing spring-boot:run which instantly starts the Spring Boot application.

debug Spring Boot relies on Maven's plugins by executing spring-boot:run --debug. The --debug flag will instruct Maven to run the program in debug mode which is essentially useful in code tracing and in-depth analysis of the application's execution.

  • Consistent

Maven's remarkable consistency shines through its single pom.xml file, maintaining uniformity across diverse environments, and effectively reducing the risk of build-related issues. However, errors in the development process are very much inevitable. When these are found, Maven acts as the intermediary by alerting the developer and providing valuable feedback on how these errors could be approached. Moreover, Maven is designed to be flexible and consistent as it can adjust to any specified Java version for any project and will still have the same functionality and dependencies. One can configure the JAVA version being used through the pom.xml file and set the properties <maven.compiler.source> and <maven.compiler.target>.

For example, if we would want to set the JAVA version to JAVA 11, we can use the following configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version> 
            <configuration>
                <source>1.11</source>
                <target>1.11</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Enter fullscreen mode Exit fullscreen mode

Task Master

Once the pom.xml file has been set-up, it all comes down to what Maven is asked to do. Maven assumes the role of the Task Master, following the commands being asked by the developer. To execute these commands, we have to open the terminal or command prompt to the root directory of the Maven project where the pom.xml file is found. We are then able to compile code with mvn compile command, tackle test source code with test-compile, execute tests via test, package your application as a JAR or WAR with package, and deploy the package to a remote Maven repository using deploy.

In conclusion, developers can solely focus on the development of their core application while
Maven handles the nitty-gritty of managing the build process effectively making a developer's working process to be more productive and development-focused.

Top comments (0)