Spring Boot with Maven Wrapper

Here we will cover the fundamental commands for building and running a Spring Boot application using Maven Wrapper.

For this post, we suppose that we have a Spring Boot project

Maven Wrapper

Maven Wrapper is a tool that helps to ensure consistent builds across different environments and eliminates the need for developers to install Maven on their local machines.

Why do we have to use Maven Wrapper:

  1. Easy to use: Maven is easy to use and comes with a simple and intuitive syntax for defining build processes. You can quickly build, test, and deploy your Java projects.
  2. Dependency management: Maven provides powerful dependency management capabilities, allowing you to manage dependencies easily.
  3. Standardization: Maven provides a standardized build process, making it easier to work with different Java projects. Maven follows a convention over the configuration approach, which means that you don’t need to specify a lot of details about your project as Maven already knows where to find the source files, where to put the compiled code, and how to package the project.
  4. Plugins: Maven provides a vast collection of plugins that allow performing a wide range of tasks. These plugins are easily configurable and can be used to run tests, generate documentation, build and deploy applications, and much more.
  5. Continuous Integration: Maven integrates well with Continuous Integration (CI) tools such as Jenkins, Bamboo, and TeamCity. This allows developers to automate the build, test, and deployment processes, ensuring the project is always in a releasable state.
  6. Community support: Maven has a large and active community of developers who contribute to the development of the tool, provide support, and create plugins.

What is a Maven Plugin?

Plugins in Maven are used to perform a wide range of tasks, such as compiling code, generating documentation, running tests, deploying artifacts, and more. Maven plugins are typically executed during specific phases of the build process, and they are defined in the pom.xml file for a project.

What is a Spring Boot Maven plugin?

The spring-boot-maven-plugin is used to build and package Spring Boot applications. It is included with the Spring Boot framework and provides several features to simplify the build process.

Some of the key features of the spring-boot-maven-plugin include:

  1. Packaging: The plugin allows you to package your Spring Boot application into an executable JAR or WAR file.
  2. Re-packaging: The plugin can re-package an existing JAR or WAR file so that it can be executed as a Spring Boot application.
  3. Dependency management: The plugin can manage dependencies for your Spring Boot application, ensuring that all necessary libraries are included in the final package.
  4. Spring Boot configuration: The plugin can read and apply configuration properties from the Spring Boot configuration files (application.properties or application.yml) during the build process.
  5. Running your application: The plugin can start and stop your Spring Boot application during the build process, allowing you to run integration tests against a running instance of your application.

To use the spring-boot-maven-plugin, you need to add it to the pom.xml file of your Spring Boot project and configure it to suit your needs. The plugin is typically used in the package phase of the Maven build lifecycle.

How to add the Spring Boot Maven plugin:

To add the Spring Boot plugin, simply add the following to your project’s pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Once you have added the Maven Wrapper plugin, you can generate the Maven wrapper script by running the following command from the root directory of your project:

./mvnw clean install

This will generate all of the maven files to run your application later and it will download the necessary dependencies for your project.

Maven folders and files description

Here is a brief description of the folders and files in a Maven project:

  • src/main/java: This is the directory where you put your Java source code files. By default, Maven expects Java source files to be located in this directory.
  • src/main/resources: This is the directory where you put non-Java resources that your project needs at runtime. Examples of such resources include property files, XML files, and configuration files.
  • src/test/java: This is the directory where you put your test code. By default, Maven expects test source files to be located in this directory.
  • src/test/resources: This is the directory where you put resources that your test code needs at runtime.
  • pom.xml: This is the Project Object Model (POM) file. It is an XML file that contains information about the project, such as its dependencies, build settings, and plugins.
  • target: This is the directory where Maven puts the output of the build process. For example, when you run “mvn package”, the resulting JAR file will be located in the target directory.
  • .m2: This is the default location of the local repository where Maven downloads dependencies. When you specify a dependency in your POM file, Maven will automatically download it and store it in the local repository. This .m2 folder is located into your “USER HOME”.
  • .mvn: This is a directory that contains Maven configuration files and the wrapper jar file. These configuration files can be used to configure Maven settings and specify which JDK to use for building the project.
  • mvnw and mvnw.cmd: These are wrapper scripts that allow you to run Maven without having to install it on your system.

These are the most important folders and files you will encounter in a Maven project.

├───.mvn
│   └───wrapper
├───src
│   ├───main
│   │   ├───java
│   │   └───resources
│   └───test
│       ├───java
│       └───resources
└───target

Here is an example of a common Maven folder structure as seen from IntelliJ

Maven folder structure

The Maven pom.xml file

In Maven, the POM (Project Object Model) is represented by an XML file named “pom.xml”. It is the fundamental configuration file for a Maven-based project and contains information about the project, such as its dependencies, build settings, and plugins.

The pom.xml file is located in the root directory of the project and provides a complete and accurate description of the project to Maven.

The pom.xml file includes the following information:

  • Project information such as name, description, and version.
  • Dependencies required by the project, including their version numbers and scope.
  • Build settings, such as the location of the source code and output files.
  • Plugins to be used in the build process, including their configuration.
  • Profiles, provide a way to specify different configurations for different environments.

A common Spring Boot pom.xml maven file looks like that:

Here is a short description of each part of Maven pom.xml file:

  • groupId: It is a unique identifier of the project’s group. It is generally in the form of a reversed domain name.
  • artifactId: It is a unique identifier of the project’s artifact (jar, war, ear). It is generally in the form of a name that identifies the project.
  • version: It is the version number of the project.
  • dependencies: It specifies the dependencies that this project has on other projects or libraries.
  • build: It specifies all the information related to building the project such as source directory, test source directory, output directory etc.
  • plugins: It specifies all the plugins that are used in building the project.

Spring_Boot_Maven_pom.png

For Spring Boot web application use this dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

Main commands for Spring Boot with Maven

Following are some of the most frequent Maven commands for work with Spring Boot:

  • mvnw -help: Print all of the maven commands and options.
  • mvnw clean: Cleans the project and removes all files generated by the previous build.
  • mvnw compile: Compiles source code of the project.
  • mvnw test: Run unit tests of the project.
  • mvnw package: Packages the compiled code into a distributable format such as JAR or WAR files.
  • mvnw install: Build and Installs the package into the local repository for use as a dependency in other projects.
  • mvnw deploy: Copies the final package to the remote repository for sharing with other developers and projects.
  • mvnw validate: validate the project and check if everything is correct.
  • mvnw dependency:tree: show all of the dependencies in a tree ouput.

Spring Boot Maven plugin command:

  1. mvnw spring-boot:run: This command starts your Spring Boot application in a local development environment.
  2. mvnw spring-boot:start: This command starts your Spring Boot application in the background as a daemon process.
  3. mvnw spring-boot:stop: This command stops a running instance of your Spring Boot application that was started with spring-boot:start.

spring-boot:run with common arguments:

  • mvnw spring-boot:run -Dspring-boot.run.arguments="--server.port=8081": This command starts your Spring Boot application with additional command line arguments, such as changing the default port number. These additional commands are optional.
  • mvnw spring-boot:run -Dspring.profiles.active=dev: This command starts your Spring Boot application with a specific active Spring profile, such as dev. *

(*) Check this page about Spring Boot profiles. Spring Boot Profiles

Combining command:

You can combine commands to execute multiple tasks in one line. For example:

.\mvnw clean install

Maven clean install

Conclusion:

This document provides an initial guide to using Maven Wrapper with Spring Boot. It covers the benefits of using Maven Wrapper, the structure of a Maven project, the Maven pom.xml file, and the most frequent Maven commands for working with Spring Boot. It also explains the Spring Boot Maven plugin and its features, as well as providing examples of common Maven commands for running a Spring Boot application.
Check this post about Spring Boot with Gradle wrapper

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)

See also