jgitver plugin, get version in pom

1.4k views Asked by At

I'm using jgitver library to automatically generate semver using state of the Git repository.

I have a multi module project where modules are interdependent, meaning if I make change to one module then other modules should start referring to the new version of the changed module.

All the modules are in the same Git repository, so any change in the Git changes the version of all modules. Following is an example of my project structure

.git   (Common repository)
module1
common-module
pom.xml (Parent POM)

Following is an example of how I have included a dependency module

<-- POM of module1, includes common-module-->

    <dependency>
        <groupId> com.test</groupId>
        <artifactId>common-module</artifactId>
        <version>1.0.0</version> <!-- version generated by jgitver -->
    </dependency>

Since at any time version generated by jgitver would be same for module1 and common-module, as they're in the same Git repository, I don't want to hard code the version across all modules. As if the version changes then I would have to manually change the version in all modules for common-module dependency.

Is there a variable like ${project.version} that can be used to refer to current version generated by jgitver?

I'm hoping to configure the module1 as below

<dependency>
    <groupId> com.test</groupId>
    <artifactId>common-module</artifactId>
    <version>${JGITVER_GENERATED_VERSION_VARIABLE}</version> <!-- version generated by jgitver -->
</dependency>
2

There are 2 answers

0
Matthieu BROUILLARD On

Sorry to be a bit late on this question I did not saw it before.

If you are in the same multi-module project, then usage of jgitver should not change the way you declare dependencies between sub-modules. Even with jgitver in the party you should still reference your sub-modules using project.version

<dependency> <groupId> com.test</groupId> <artifactId>common-module</artifactId> <version>${project.version}</version> <!-- use a reference to the POM model --> </dependency>

For the IDE, the problem is a bit different because they do not like that much the dynamic behavior of jgitver (same for Eclipse & Intellij IDEA at least). You can have a look at the wiki page about jgitver IDE usage by using profiles and/or deactivation ; you can then use your IDE normally but you will have to delegate to either maven or gradle a normal build for release/deployments.

Hope that helps.

--
Matthieu, author and maintainer of jgitver

0
11thdimension On

It turns out there is a property which can be used to specify the calculated version jgitver.calculated_version since 0.2.0-alpha2 release. However it's supposedly not needed since the same 0.2.0-alpha2 release.

Sub modules which want to specify the calculated version as the version of the dependency are supposed to use project.version. Static value remains 0 and when project is built using maven, project.version is resolved to correct value.

I came across this after facing following issue Multi module project: Building only a sub module does not resolve versions for deps

project.version works as long as it's project is built from the parent, it doesn't work if the project is built from the sub module directory itself. project.version resolves to 0 and hence results in an error common-module-0.jar missing. The issue has been closed and it's jgitver is supposed to work from sub module also since 0.2.0-alpha2. However it doesn't work with certain versions of maven.

On the github page of the project it's mentioned that minimum version of maven should be 3.3.2 Ref: jgitver-maven-plugin requires at least maven-3.3.2 to work correctly.. However for me jgitver 1.1.4 doesn't work with maven 3.3.5 and build keeps on resulting in error common-module-0 missing when building submodules only. I can confirm that it works for maven 3.3.9. I came to know that because I was working from two different machines with two different versions of maven. I have updated maven to current version 3.5.2 and it works fine from the submodules.

Note: It seems that my IDE STS 3.8.4/Eclipse Mars.2 (4.5.2) isn't able to resolve the calculated version of project from project.versionand it tries to resolve common-module-0. It's resolved only as long as this project is open in Eclipse. May be that's how it's supposed to work.