Crowie
Crowie

Reputation: 3272

Setting variables defined in a POM dynamically with Maven

I'm working on a task to dynamically build an 'About' (static HTML) page for my application and would like to use the build number for the project from TeamCity to set it. We want a different version generated and deployed with each deployment. This leads me to want to store values to variables used in my build.

So my challenge as I see it is:

How do I allocate a value to a variable dynamically during a Maven build phase and then have that available for further parts of the build (e.g. generating a file).


The background of what I'm trying to do is as follows (with the current variable problem highlighted):

  1. Get value from Team City Build Server via REST call
  2. Allocate value to Maven variable
  3. Use variable during file generation
  4. Profit

However it seems most links refer to obtaining values from properties files, not setting them up dynamically during build execution.


Lastly - I figure I'll do a bit of this with the Ant-Run plugin

Upvotes: 2

Views: 1473

Answers (1)

Software Engineer
Software Engineer

Reputation: 16140

If you use ${buildNumber} in a processed resource, maven will automatically pick up the build number from team city and replace the variable with the appropriate value (without using REST or any other complicated technique). Obviously, this won't happen in local builds, but my team doesn't have a problem with that. It would be easy to fix this using a build profile that assigns some value to the variable when the build is in your local environment rather than in team city.

We also use %build.vcs.number% as the build number format to force team city to use the svn build number, rather than its own arbitrary build number.

Upvotes: 1

Related Questions