How to Write a Spring Boot Library Project?
By: Emre Kumas
Tags: Spring Boot Library, Spring Boot, Spring, Spring Library, Software Development

Let’s Start

First and foremost, let’s create an empty project and check the dependencies. I’m using Maven as my dependency management tool, so we need to take a look at the pom.xml

1. POM

Initial spring boot projects come with a few dependencies and spring-boot-starter-parent. We don’t need them. The library itself won’t run on its own so we can remove them. There is no need for anything unnecessary. Also, we can delete the spring-boot-maven-plugin from build plugins.

The project version is set as “${revision}”. This way, whenever we create a new version and deploy it, we can pass the version number from our CI/CD tool easily without directly editing the pom file. You can take a look at this article in which my teammate explained how we used this system in our libraries. Added packaging method as ‘jar’. Because we will deploy the library as a jar file to the repository manager. Included two feign dependencies. You can search to learn more about them. But, be aware that if this library was not intended to work with feign, we wouldn’t even need these two dependencies. An example use case may be a common model library that includes only model classes. Included lombok dependency. It will be very useful when we write the library project. Next, distribution management comes. We need a place to deploy our library so that any application can download it at the build phase. You may use Nexus, Jfrog, etc. It’s up to you. Finally, we need a build plugin. We will use the maven-compiler-plugin that will compile the project and create the jar file. Also, a few properties have been included at the top to set the java version and encodings.

2. PROPERTY FILE

The initial project comes with an application.properties file. Personally, I like the yaml based property files more. It allows us to create multiple profiles in a single file. So, I will use a yaml formatted property file. Also, I will use a random service for demonstration. Since it doesn’t have a test URL, I will use the same URL for all environments.

IMPORTANT: Here comes a noteworthy part. If we name our property file as “application.yaml” in the library and the app which will use this library has its own property file named “application.yaml”, they will collide. The property in the parent project will override the one in the library, so it won’t be able to read it in the application. A complete disappointment :(

SOLUTION: The solution to this problem is to name your property file in the library differently. Let’s say “marvelousapiclient-application.yaml”. Maybe a little bit long name, but who cares. Just keep it. Now, the problem is your library won’t see this file automatically as its name is different. But, we will solve this soon.

3. CONFIGURATIONS

Before looking at how to configure properties, you need to know how you set your active profile in containerized environments. There are a few of ways of doing this. You can set a system variable when creating the container or you can pass it as a parameter when running your java application. For local purposes, you can use your IDE’s features to set an active profile as I have shown below (You need to do this in the application, which will use the library):

Setting active profile in IntelliJ IDEA

Now, we need to create a configuration file to read the property file. You may be asking why we need this. Because we have renamed it and since it is not automatically read by the spring itself, we need to tell the system how it should read the marvelousapiclient-application.yaml

You can analyze the code, but simply it takes the active profile firstly from the system environment. If it is null, it checks the spring.profiles.active property we send as a parameter. As you see, it will throw an exception if no active profile is set. We put a simple log to check it in our log monitoring system.