Skip to content

Instantly share code, notes, and snippets.

@ccampo133
Created May 11, 2020 15:28
Show Gist options
  • Save ccampo133/d53f3ab89a89ceded1746dfc791212b8 to your computer and use it in GitHub Desktop.
Save ccampo133/d53f3ab89a89ceded1746dfc791212b8 to your computer and use it in GitHub Desktop.
Dynamically set a Maven project version from the command line

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment