DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Taming the Virtual Threads: Embracing Concurrency With Pitfall Avoidance
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • Microservices With Apache Camel and Quarkus (Part 3)

Trending

  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • What’s Got Me Interested in OpenTelemetry—And Pursuing Certification
  • MCP Servers: The Technical Debt That Is Coming
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Versioning Multiple Microservices in a Monorepo Using Maven

Versioning Multiple Microservices in a Monorepo Using Maven

Let's make microservices a bit less complicated.

By 
Dror Helper user avatar
Dror Helper
·
Jan. 24, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.1K Views

Join the DZone community and get the full member experience.

Join For Free

It 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:

XML
 




x


 
1
<properties>
2
    . . .
3
 
           
4
    <!-- Versions -->
5
    <service1.version>1.1-SNAPSHOT</service1.version>
6
    <service2.version>2.1-SNAPSHOT</service2.version>
7
    <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:

XML
 




xxxxxxxxxx
1
34


 
1
<properties>
2
    . . .
3
    
4
    <!-- auto version related -->
5
    <service.version>0</service.version>
6
    <version.update.enable>generate-sources</version.update.enable>
7
    <version.phase>none</version.phase>
8
    <service.name>-invalid-</service.name>    
9
    . . .
10
 
           
11
</properties>
12
<build>
13
    <plugins>
14
        <plugin>
15
            <groupId>org.codehaus.mojo</groupId>
16
            <artifactId>versions-maven-plugin</artifactId>
17
            <version>2.7</version>
18
            <executions>
19
                <execution>
20
                    <phase>${version.phase}</phase>
21
                    <goals>
22
                        <goal>set</goal>
23
                    </goals>
24
                    <id>update-version</id>
25
                    <configuration>
26
                        <generateBackupPoms>false</generateBackupPoms>
27
                        <artifactId>${service.name}*</artifactId>
28
                        <newVersion>${service.version}</newVersion>
29
                    </configuration>
30
                </execution>
31
            </executions>
32
        </plugin>
33
    </plugins>
34
</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:

XML
 




xxxxxxxxxx
1


 
1
<properties>
2
    <service.name>service1</service.name>
3
    <version.phase>${version.update.enable}</version.phase>
4
    <service.version>${service1.version}</service.version>
5
</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...

microservice Apache Maven Monorepo

Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Taming the Virtual Threads: Embracing Concurrency With Pitfall Avoidance
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • Microservices With Apache Camel and Quarkus (Part 3)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!