Automatic version number with TeamCity and Maven


I’m on a new project and have the opportunity to make things right from the beginning. I’ve been trying to include version number and build number into the packaged WAR file. As a bonus I’ve made the version available through a web page like:

Version: MyApp-1.0 (build number 7)

My project is a web service built with maven. TeamCity (5.1.1) is used for building. I use IntelliJ IDEA version 9 as my IDE. With version number I refer to the version in my pom.xml, for instance:


Build number refers to the automatically incremented build number in TeamCity.

These are the steps I took:

1. In the TeamCity Build configuration, make sure Build number format includes {0}.

2. Create/edit your project file src/main/resources/

    version=${}-${pom.version} (build number ${build.number}) and pom.version are Maven attributes defined in the beginning of your pom.xml. build.number is an environment variable set by TeamCity at build time.

3. Edit pom.xml:

When building on TeamCity this property is set as an environment variable by TeamCity and then maven replaces the placeholder in
When compiling locally we have to set this property here or Spring will fail to resolve the placeholder in This works in IntelliJ (only?) with its feature to filter maven resources.
A cleaner solution is to set up an envrionment variable in the IDE with this name, but this requires new developers to configure the IDE.
  <build.number>Unknown/local build</build.number>

This will make Maven replace the variables in with maven properties and environemnt variable available at build time.

4. Now the packaged will contain current build number from TeamCity and maven artifact name and version from your pom.xml !

As a bonus I made the version available via a jsp called status.jsp. Getting the version property from into a jsp can be made in several ways of course. Since my project don’t contain any web stuff (only web services) I created the jsp in a hacky way:

  <p>Version: <%= BusinessServiceImpl.instance.version %></p>

And BusinessServiceImpl:

public String version;
public static StaffDiscountServiceImpl instance;

public StaffDiscountServiceImpl(} {
  instance = this;

The version attribute is autowired and defined in Spring’s applicationContext.xml:

<context:property-placeholder location="classpath:/ "/>
<bean id="version" >
  <constructor-arg value="${version}" />

There’s certainly better ways of doing this jsp stuff (comments, please!) but this was simple and it works.


2 Responses to “Automatic version number with TeamCity and Maven”

  1. Artem said

    Hi! Maybe it will be easier to put a plain text file with build number and put it into webapp directory?

  2. Joe said

    Is there any way to get the checkout location the same way? Like ${scmBranch}? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: