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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Easily Calculate Your Project Version With Git

Easily Calculate Your Project Version With Git

How a user created a project to automatically create version names with every Git commit using Maven and Gradle plugins.

Matthieu Brouillard user avatar by
Matthieu Brouillard
·
Jun. 28, 16 · Tutorial
Like (11)
Save
Tweet
Share
10.67K Views

Join the DZone community and get the full member experience.

Join For Free

One Problem

Having a good project naming convention and tooling is not as easy as it could or should be. One trying to establish such a policy should take care of several needs:

  • Be compliant with the tooling (Maven, Gradle, ...).

  • Follow standards when it matters: semver, for example, in regards of version naming.

  • Be compatible with good CI pratices (work in branch, CI tests & integration, ...).

  • Do not pollute the code base (yes I'm looking at you maven-release-plugin with your several commits and builds!).

  • Be flexible to adapt to one's needs.

In the past month, I have seen attempts from people trying to use Git core functionalities to achieve flexible or automatic project version naming.

Most of those attempts are based on 'git describe'. One real drawback of 'git describe', making it useless, is that it is not semver compliant. If you use it to name your project versions, then you will first name your project with `1.0.0` by tagging it and a commit later your project will be `1.0.0-1`. Unfortunately, in semver and more or less in all build tools, 1.0.0-1 < 1.0.0 which is not what you get with 'git describe'.

Still, the idea looks good. We want our project to take one particular commit and label that with a version name. We do not want to have to modify our codebase just to label and tag our codebase.

One Answer

With this in mind, I created the project jgitver and it's associated plugins for the two major build tools I use: Maven (with jgitver-maven-plugin) and Gradle (with gradle-jgitver-plugin).

Instead of writing hundreds of lines, let's just have a look at what those library/plugins offer via some animated GIFs.

When used with default Maven mode:

Image title

When used with default Gradle mode:

Image title

By using jgitver you have an automatic consistent project naming scheme that does not pollute your git history, is compatible with branches and CI (no multiple deployment of the same version name coming from multiple branches), and is fully controlable (via lightweight or annotated tags and plugin parameters).

Then you will discover that jgitver usage opens up new pathways for your release workflows. No more need to change your projects files to release your project. Release is reduced to the strict minimum that it should always have been:

  • Tag or label a commit your are satisfied with.

  • Deploy or use the build result to any repository (private/public/...) you need.

And that's all! no need to modify the project version twice and pollute your project history, and no need to use cumbersome and error prone plugins (once again I'm looking at you, maven-release-plugin).

Usage

The usage is very convenient and just requires the plugins to be declared in your build files.

Maven Usage

<build>
    <extensions>
        <extension>
            <groupId>fr.brouillard.oss</groupId>
            <artifactId>jgitver-maven-plugin</artifactId>
            <version>0.1.0</version>
        </extension>
    </extensions>
</build>

Gradle Usage

plugins {
  id "fr.brouillard.oss.gradle.jgitver" version "0.2.0"
}

Of course both plugins can be adapted and configured to meet other needs.

Project Links and Documentation

For more details and configuration options follow the documentation of both projects:

  • jgitver-maven-plugin: https://github.com/jgitver/jgitver-maven-plugin
  • gradle-jgitver-plugin: https://github.com/jgitver/gradle-jgitver-plugin

  • jgitver library: https://github.com/jgitver/jgitver

Feel free to comment or email me about the projects and the needs they try to cover.

I'd be very thankful for every comment on the subject and any new idea that could pop up from any discussion.

Note: jgitver-maven-plugin works with all major IDEs, but due to IDEA-155733, a very recent version of IntelliJ is required (version > 145.1616.11).

Related Refcard:

Git

Git

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Front-End Troubleshooting Using OpenTelemetry
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Create a REST API in C# Using ChatGPT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: