Continuous Integration and Delivery for Maven projects with Jenkins and Docker.

In this article we look at continuous integration for Maven projects using Jenkins and Docker. The project from my previous post Multiple Databases in Spring Boot will be built and deployed and run by Jenkins. The codebase for Multiple Databases in Spring Boot is https://github.com/gabrieljeremiahcampbell/multipledatabases

First I will discuss the methodology I use to build the Maven project and create the Docker image. There are normally two approaches:

1. Using a maven plugin for Docker that can run the Docker commands as tasks and goals.

2.Let Docker build the Maven project.

The problem I see in the first methodology is that it can create a dependency of ensuring that your Maven plugins are up to date especially if the Docker API changes. I choose to go with the second option because this way we have decoupled Maven from Docker. It is a type of Inversion Of Control(IoC) – Maven should not know about Docker as it’s focus is the project. In other words Docker is one of many environments that can run our app. If we had other platforms to run that means our POM would have to have plugins for everyone. So I will build the project using Docker.

In our IDE we open our project, we create the following Dockerfile:

image

This Dockerfile is a multistage build. First I use a maven container to build my image, maven3.5.2-jdk-8-apline. 

The important aspect to note is that Docker is running the Maven commands for building and packaging the application.

After this I use an openjdk:8-jdk-apline image to build to create the container for my project. Lastly I expose the port 8080, that my application will listen on.

In Jenkins we create the build plan with the following steps. I list all the screenshots so you can see the commands involved:

image

image

image

image

The last command prune will delete any temporal containers used in the multistage build and created by Docker. It is like a clean up operation. If our container fails to start and is in error it would also be removed. This reduces the manual effort of cleaning up the container and image registries in Docker. However it should be used with caution.

The Dockefile is the GitHub repos link shared above.

Multiple Databases with shared entity classes in Spring Boot and Java.

Hello everyone, It has been a few months since my last post. I have been busy travelling and relocating . In this post I want to illustrate how a Spring Boot application can have multiple data sources with shared entity classes. The need for this arose in my current project where an in-memory database was needed for the high performance and a persistent database for storage.

In this blog post, I will use H2 for the in-memory database and Postgres for the persistent storage. I will setup up the application and show how the entities can be passed from one data source to another.

The application will be a Spring Boot application using Java 8 with REST enabled.  I will highlight the important parts and classes of the application. The link to the Github code is given below.

The application will save a Person entity to an in-memory database and to a persistent database via a REST POST call. We create our entity, Person.java:

image

For the sake of brevity I leave out the Getters and Setters.

Next we setup the two connections for the two databases. I will leave out the reading of the properties but highlight the important parts of the classes,  InMemoryDatabaseConfiguration.java and PeresistentDatabaseConfiguration.java:

image

image

As you can see from the two classes the same data model package is used com.gabriel.multipledatabaseconnection.model. This means that the two databases will share the same Entities and within the application they can be passed to and fro between the data sources.

Next we configure our respositories. These are very simple and just extend Spring’s JpaRepository:

image

image

The last step is to setup a controller which I call the MutlipleDatabaseController. We will create a new person in both databases but simple passing the new person entity from the in-memory database to the persistent database:

image

Note that the transaction managers are defined for the respective data sources and annotate the respective methods.

Last we test. For this I will use SoapUI the opensource version, To make a REST call to create John Connor:

image

If we check in our databases using pgAdmin4 and H2 web console:

image

image

There you have it, one REST call two data sources one entity persisted in both. The Github link is https://github.com/gabrieljeremiahcampbell/multipledatabases. Note have also added the .idea files if using Intellij( This is for the open source version of Intellij).

Cheers,

Gabriel