ℹ Also available as Gradle Plugin
This extension will virtually set project versions, based on current Git branch or Git tag.
ℹ The pom files will not be modified, versions are modified in memory only.
pom.xml
create or update ${basedir}/.mvn/extensions.xml
file
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>me.qoomon</groupId>
<artifactId>maven-git-versioning-extension</artifactId>
<version>4.10.0</version>
</extension>
</extensions>
ℹ Consider CI/CD section when running this extension in a CI/CD environment
You can configure the final version format for specific branches and tags separately.
Create ${basedir}/.mvn/maven-git-versioning-extension.xml
.
Example: maven-git-versioning-extension.xml
<gitVersioning>
<branch>
<pattern>master</pattern>
<versionFormat>${version}</versionFormat>
</branch>
<branch>
<pattern><![CDATA[feature/(?<feature>.+)]]></pattern>
<versionFormat>${feature}-SNAPSHOT</versionFormat>
</branch>
<tag>
<pattern><![CDATA[v(?<tagVersion>[0-9].*)]]></pattern>
<versionFormat>${tagVersion}</versionFormat>
</tag>
<commit>
<versionFormat>${commit.short}</versionFormat>
</commit>
</gitVersioning>
optional <updatePom>
global enable(true
)/disable(false
) version update in original pom file.
optional <preferTags>
global enable(true
)/disable(false
) prefer tag rules over branch rules if both match.
<branch>
specific version format definition.
<pattern>
An arbitrary regex to match branch names (has to be a full match pattern e.g. feature/.+
)<versionFormat>
An arbitrary string, see Version Format & Placeholders<property>
A property definition to update the value of a property
<pattern>
An arbitrary regex to match property names<valueFormat>
The new value format of the property, see Version Format & Placeholders<valuePattern>
An arbitrary regex to match and use capture group values of property value<updatePom>
Enable(true
) or disable(false
) version update in original pom fill (will override global <updatePom>
value)git checkout <BRANCH>
<tag>
specific version format definition.
<pattern>
An arbitrary regex to match tag names (has to be a full match pattern e.g. v[0-9].*
)<versionFormat>
An arbitrary string, see Version Format & Placeholders<property>
A property definition to update the value of a property
<pattern>
An arbitrary regex to match property names<valueFormat>
The new value format of the property, see Version Format & Placeholders<valuePattern>
An arbitrary regex to match and use capture group values of property value<updatePom>
Enable(true
) or disable(false
) version update in original pom fill (will override global <updatePom>
value)git checkout <TAG>
<commit>
specific version format definition.
<versionFormat>
An arbitrary string, see Version Format & Placeholders<property>
A property definition to update the value of a property
<pattern>
An arbitrary regex to match property names<valueFormat>
The new value format of the property, see Version Format & Placeholders<valuePattern>
An arbitrary regex to match and use capture group values of property valuegit checkout <COMMIT>
and no matching version tag is pointing to HEADℹ /
characters within final version will be replaced by -
**
${ref}
${ref.slug}
${ref}
with all /
replaced by -
${branch}
(only available within branch configuration)
HEAD
${tag}
(only available within tag configuration)
HEAD
, if multiple tags point at HEAD
latest version is selected${commit}
HEAD
commit hash${commit.short}
HEAD
commit hash (7 characters)${commit.timestamp}
HEAD
commit timestamp (epoch seconds)${commit.timestamp.datetime}
HEAD
commit timestamp formatted as yyyyMMdd.HHmmss
Pattern Groups
group name
or group index
e.g.pattern = 'feature/(?<feature>.+)'
versionFormat = '${feature}-SNAPSHOT'
pattern = 'v([0-9].*)'
versionFormat = '${1}'
${version}
version
set in pom.xml
${version.release}
version
set in pom.xml
without -SNAPSHOT
postfix${property.name}
${property.value}
Disable Extension
export VERSIONING_DISABLE=true
maven ... -Dversioning.disable=true
Provide branch or tag name
export VERSIONING_GIT_BRANCH=$PROVIDED_BRANCH_NAME
export VERSIONING_GIT_TAG=$PROVIDED_TAG_NAME
maven ... -Dgit.branch=$PROVIDED_BRANCH_NAME
maven ... -Dgit.tag=$PROVIDED_TAG_NAME
ℹ Especially useful for CI builds see Miscellaneous Hints
Update pom.xml
export VERSIONING_UPDATE_POM=true
maven ... -Dversioning.updatePom=true
Prefer Tags for Versioning instead of Branches
export VERSIONING_PREFER_TAGS=true
maven ... -Dversioning.preferTags=true
git.commit
e.g. '0fc20459a8eceb2c4abb9bf0af45a6e8af17b94b'git.ref
value of branch of tag name, always set
git.ref.slug
like git.ref
with all /
replaced by -
git.branch
e.g. 'feature/next-big-thing', only set for branch versioninggit.tag
e.g. 'v1.2.3', only set for tag versioninggit.commit.timestamp
e.g. '1560694278'git.commit.timestamp.datetime
e.g. '2019-11-16T14:37:10Z'git.dirty
repository dirty state indictor true
or false
mvn --non-recursive exec:exec -Dexec.executable='echo' -Dexec.args='${project.version}' -q
The reproducible builds feature (https://maven.apache.org/guides/mini/guide-reproducible-builds.html) newly introduced in maven can be easily supported with this extension by using the latest commit timestamp as build timestamps.
<project.build.outputTimestamp>${git.commit.timestamp.datetime}</project.build.outputTimestamp>
For a flawless experience you need to disable this extension during project import.
Disable it by adding -Dversioning.disable=true
to Maven Importer VM options (Preferences > Build, Execution, Deployment > Build Tools > Maven > Importing > VM options for importer).
Most CI/CD systems do checkouts in a detached HEAD state so no branch information is available, however they provide environment variables with this information. You can provide those, by using Parameters & Environment Variables. Below you'll find some setup example for common CI/CD systems.
execute this snippet before running your maven
command
if [[ "$GITHUB_REF" = refs/heads/* ]]; then
export VERSIONING_GIT_BRANCH=${GITHUB_REF#refs/heads/};
elif [[ "$GITHUB_REF" = refs/tags/* ]];
export VERSIONING_GIT_TAG=${GITHUB_REF#refs/tags/};
fi
execute this snippet before running your maven
command
before_script:
- if [ -n "$CI_COMMIT_TAG" ]; then
export VERSIONING_GIT_TAG=$CI_COMMIT_TAG;
else
export VERSIONING_GIT_BRANCH=$CI_COMMIT_REF_NAME;
fi
execute this snippet before running your maven
command
if [[ "$GIT_BRANCH" = origin/tags/* ]]; then
export VERSIONING_GIT_TAG=${GIT_BRANCH#origin/tags/};
else
export VERSIONING_GIT_BRANCH=${GIT_BRANCH#origin/};
fi
- mvn install
# run integration tests after install,
# integration tests will run with LATEST version of extension installed
- mvn failsafe:integration-test
${ref.slug}
alike ${ref}
with all /
replaced by -
git.ref.slug
alike git.ref
with all /
replaced by -
3.6.2
<property>
replacement configuration
simplify <property>
replacement configuration
new config
<gitVersioning>
<branch>
<pattern>master</pattern>
<versionFormat>${version}</versionFormat>
<property>
<pattern>revision</pattern>
<valueFormat>${branch-SNAPSHOT}</valueFormat>
</property>
</branch>
</gitVersioning>
old config
<gitVersioning>
<branch>
<pattern>master</pattern>
<versionFormat>${version}</versionFormat>
<property>
<pattern>revision</pattern>
<value>
<format>${branch-SNAPSHOT}</format>
</value>
</property>
</branch>
</gitVersioning>
${git.dirty}
project propertygit.commit.timestamp
git.commit.timestamp.datetime
<update>
) to update version in original pom file. see Configure Extensiongit.ref
value of branch of tag name, always set<configuration>
-> <gitVersioning>
MAVEN_PROJECT_BRANCH
-> VERSIONING_GIT_BRANCH
MAVEN_PROJECT_TAG
-> VERSIONING_GIT_TAG
-Dproject.branch
-> -Dgit.branch
-Dproject.tag
-> -Dgit.tag
-DgitVersioning
- disable the extension by a parameter is no longer supportedproject.branch
-> git.branch
project.tag
-> git.tag
project.commit
-> git.commit