Configuring Spring Boot Application with Maven

You can build and manage your Spring Boot applications with Maven or Gradle. In this article we will explore the basics of maven configuration for Spring Boot applications.

When we generate a Spring Boot application with Spring Initializr or Spring Boot CLI, a template Spring Boot project is generated for us having the selected dependencies. Let's examine the sections of the generated pom.xml.

1.Parent Pom

In the heart of Spring Boot is inheriting the defaults from predefined configurations and that is the usage of starter pom files. A starter pom file includes convenient dependencies and versions in sync for your application; and when you import that starter you would automatically have the dependencies defined in that starter pom file.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/>
</parent>

In the code block above, the core starter spring-boot-starter-parent is declared as parent. By doing this, the starter will add the default configurations to the project for us; such as adding configurations for the plugins maven-jar-plugin, maven-surefire-plugin. And also we will guarantee to use the correct versions of third party dependencies specified via the bom file spring-boot-dependencies which is the parent of spring-boot-starter-parent. From now on, we donot need to provide version info for dependencies since Spring Boot will manage it for us. However, it is up to us to override the provided versions.

And lastly, by setting the relativePath as empty, we are telling maven to lookup the parent directly from repository, meaning that we command to bypass searching the parent in our local workspace and get it directly from repository.

You can investigate the spring-boot-starter-parent pom file.

2. Overriding Maven Properties

We can add new properties or override the properties defaulted in spring-boot-dependencies. Commonly these properties may be version info to be overriden.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <commons-codec.version>1.10</commons-codec.version>  
</properties>

3. Adding Application Starters

After wiring the dependency space and configuring the default plugins with the parent starter, we can now add some other starters up to the needs of our application. For example, if we need a persistence layer using JPA, then we add the JPA starter that is named by pivotal as spring-boot-starter-data-jpa. And this starter wires dependencies of Hibernate as its default to the project and automatically configures Spring Data, datasource bean definitions and also the transaction management support.

To summarize it, a starter provides two important shortcuts for us:
   - It adds all the dependencies needed for a given type of the application.
   - It automatically makes the configuration for defaults with respect to the general usage.

Official starters provided by pivotal have the same naming convention starting with spring-boot-starter-*, where * denotes a specific type of application. Two of the common starters can be added as follows:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>    

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

3.1 spring-boot-starter

This is the core starter and contains the followings:
   - spring-core: Spring core dependencies
   - spring-boot-autoconfigure: Auto-configuration support
   - spring-boot-starter-logging: Adds the logging support which default to logback

3.2 spring-boot-starter-test

This is the starter for testing support and includes the following defaults:
   - junit for unit testing library
   - mockito for mocking library
   - hamcrest as matcher framework
   - spring-test
   - xmlunit

Other common starters can be found here.

4. Adding Spring Boot Maven Plugin

To package a Spring Boot application as jar/war and to run it, we can use the Spring Boot Maven Plugin by adding it to the pom file.

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

mvn spring-boot:run


Yorumlar

Popular

Java 14 New Features

Pretenders, Contenders and Liars

Java 12 New Features