Today I am going to specifically talk about how to implement JIB for multi-module project.
What is multi-module project?
A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project’s root directory and must have packaging of type pom.
Now, the submodules are regular Maven projects, and they can be built separately or through the aggregator POM.
By building the project through the aggregator POM, each project that has packaging type different than pom will result in a built archive file.
You can refer to this article to understand it better.
What is JIB?
You can look at the official documentation for more details.
Our Multi Module project Implementation
We have a parent pom project called service which has 4 maven modules projects as below. api module depends on core and core depends on common depends on schema.
*No changes are required in this module as this is our executable jar.
In the parent pom you define the jib maven plugin declaration like below
Now coming to the sub modules maven projects. below is what you need to add. (specifically in core, common and schema modules)
Learn about reproducibility builds here.
That is all what you need to get started. Now, in local to execute this you need to provide docker registry credentials somewhere. We are using settings.xml under ~/.m2 directory to add these credentials. see below.
Note: Please avoid storing passwords in your code repositories like Git etc. Also, Depends on your CI tool please select best strategy to provide these credentials without compromising the security.
Now to run this setup you need can either execute
- mvn clean install jib:build
- mvn clean install jib:dockerBuild
The 1st command will directly try to push the image to remote docker registry. The 2nd command will create the image locally as well in addition to pushing to remote registry.
If you want to inspect the contents of your container image you can use below commands.
docker create --name="tmp_$$" image:tag
docker export tmp_$$ | tar t > docker_contains - you can view this file by opening in any text editor
docker rm tmp_$$
You can refer to this stackoverlfow thread for detailed information on these commands.
After implementing JIB for containerizing our application we saw significant performance improvement in terms of time.
Using Docker building image was taking anywhere between 70 to 80 seconds for building 600MB image but now with JIB it is taking less than 20sec. Below is a comparison between Docker and JIB from Google official documentation.
So this is how we implemented JIB for our multi-module maven project and simplified our CI process along with achieving some performance benefit in terms of time.
I hope this article will benefit people who are looking to implement the similar thing in their project.