Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a Maven project version from script

First of all, I do have some sort of understanding that the following might not be the generally accepted way to do things.

We have a Maven 2 project that has a version number which should be updated each week or so, during a new release. During this process, I've tried to eliminate all the things one has to remember and I've made a bash script that handles the process interactively.

However, my problem is updating the pom version from the command line. I can do this with sed but I don't think it is very convenient. I was wondering if there is any maven plugin that would be able to modify the pom.xml directly from the command line. The version is set in the properties section of the pom. Would it be possible to write a plugin that would change the properties?

Thanks in advance.

Update

It seems that my issue was with project versions defined as properties (that were applied when filtering) which seems now a bit dumb.

One thing that I'm still looking for an answer is how to get the version of certain project reliably to the command line. Previously I had a "pretty unique" property that I got using grep, but now the <version> element is not unique as in child project there is at least two of these. I would need some sort of XML parser if Maven has no solutions, but my goal is to make the script as independent as possible.

I'm not sure if I should've created a new question from this, but I didn't. Getting the version is very closely related to the setting the version.

like image 578
mkko Avatar asked Aug 19 '10 05:08

mkko


2 Answers

I was wondering if there is any maven plugin that would be able to modify the pom.xml directly from the command line.

The Versions Maven Plugin can do this. Check the following goal:

  • versions:set can be used to set the project version from the command line, updating the details of any child modules as necessary.
like image 57
Pascal Thivent Avatar answered Sep 28 '22 03:09

Pascal Thivent


From Maven POM reference:

env.X: Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH environment variable. Note: While environment variables themselves are case-insensitive on Windows, lookup of properties is case-sensitive. In other words, while the Windows shell returns the same value for %PATH% and %Path%, Maven distinguishes between ${env.PATH} and ${env.Path}. As of Maven 2.1.0, the names of environment variables are normalized to all upper-case for the sake of reliability.

That means that you can have an environment variable like $MYMAVENPROJECTVERSION and read it as this:

<version>${env.MYMAVENPROJECTVERSION}</version>

You can update this environment variable every week, before running build.

Hope this will help you.

like image 21
sourcerebels Avatar answered Sep 28 '22 02:09

sourcerebels