Here's a little trick to set a Maven project's version from the command line and environment variables, using a build profile.
Assuming your POM looks something like this:
<artifactId>example</artifactId>
<version>${project.version}</version>
...
<properties>
<project.version>1.0.0</version>
</properties>
...
<profiles>
<profile>
<id>version-from-env</id>
<activation>
<property>
<name>env.VERSION</name>
</property>
</activation>
<properties>
<project.version>${env.VERSION}</project.version>
</properties>
</profile>
</profiles>
When you build, and you set an environment variable called VERSION
, the value of that env var will apply as the project's version. Also works as a -D
arg, and falls back to 1.0.0
if nothing is set.
The profile can be set with the -P
flag, e.g. -P version-from-env
.
For more advanced version inference, see my Maven build plugin, maven-git-version