Versioning Multiple Microservices in a Monorepo Using Maven
Let's make microservices a bit less complicated.
Join the DZone community and get the full member experience.
Join For FreeIt is crucial that each piece of code you deploy have a unique version, it helps you track what your client is running, mark deployments with breaking changes, and makes your life so much easier — especially when trying to understand what changes are running at your client site in the middle of the night.
When you develop microservices, it is twice as important. Usually, you deploy your services individually and you need to know which version of which dependency is used by your service. Another requirement many developers face is how to automatically (or manually) update your dependencies when they change.
At one of my client's projects, we had a single repository with all of the services, which were built and deployed individually. As part of our continuous integration and deployment, we've wanted to build only the services that were either changed or one of its dependencies has changed. I've also wanted to make sure that it's easy to add new services to our system.
Then I found the Versions Maven Plugin which can be used to automatically update versions to your Maven projects and it automatically makes sure to update all the other projects that depend on it.
In my example project (source in GitHub) I have three services. Each service has a server and contract project and service2 depends on service1 contracts and services3 depends on both service1 and service2.
Each service has a folder with its own POM file with a subfolder for each sub-service.
I've needed a central place to manage all of services versions so I've added the following to the main (aggregator) pom.xml:
<properties>
. . .
<!-- Versions -->
<service1.version>1.1-SNAPSHOT</service1.version>
<service2.version>2.1-SNAPSHOT</service2.version>
<service3.version>3.1-SNAPSHOT</service3.version>
I've wanted to automatically update the versions in the service pom.xml file when a service version changes, so I've added the following in the aggregator pom.xml:
xxxxxxxxxx
<properties>
. . .
<!-- auto version related -->
<service.version>0</service.version>
<version.update.enable>generate-sources</version.update.enable>
<version.phase>none</version.phase>
<service.name>-invalid-</service.name>
. . .
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<phase>${version.phase}</phase>
<goals>
<goal>set</goal>
</goals>
<id>update-version</id>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<artifactId>${service.name}*</artifactId>
<newVersion>${service.version}</newVersion>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've added three new properties that would be set from each individual service:
- version.phase - this is when the versioning plugin would run. I needed it to run before compilation so I've defined a new property called version.update.enable with the correct version phase. The default is set to a nonexistent phase so that the versioning would only run for the services I need them to run.
- service.name - the service artifact name. For service1 it would be service1. I've used ${service.name}* because I've wanted all of the sub-projects (.server and .contracts) to have the same version.
- service.version - here I'll set the service version. I've done a trick here, as you'll see in a minute.
Now in each service (service1\pom.xml) we have to add the following lines:
xxxxxxxxxx
<properties>
<service.name>service1</service.name>
<version.phase>${version.update.enable}</version.phase>
<service.version>${service1.version}</service.version>
</properties>
Now each service defines it's name (same as directory), set the version.phase using version.update.enable (which is set to generate-sources) and set the value of the version to the property defined in the root's pom.xml.
Now if you update the version run mvn compile the version will be updated automatically:
Using this method, we were able to add versioning to all of our existing microservices and create new services easily - and it worked like a charm.
So there you have it, automatic version updates for all dependent versions with a single update, which causes all of the affected services to be built once committed.
Happy coding...
Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments