Versioning Multiple Micro-Services in a Monorepo using Maven

It is crucial that each piece of code you deploy have a unique version, it help you you track what your client is running, mark deployments with breaking changes and make 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 micro services it it 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 developer 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 it’s dependencies has changed. I’ve also wanted to make sure that it’s easy to add new services to our system.

Then I found about the Versions Maven Plugin which can be used to automatically update versions to your maven projects and it automatically make sure to update all the other projects that dependent on it.

In my example project (source in GitHub) I have three services. Each service has a server and contract projects and service2 depends on service1 contracts and services3 depends on both service1 and service2.

Each service has a folder with it’s own pom file with sub folder 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 versions changes so I’ve added the following in the aggregator pom.xml:

<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 non existent 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:

<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 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 update for all dependent versions with a single update, which cause all of the effected services to be built once committed.

Happy coding…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.