Upgrade Maven POM Versions Recursively
More often than not, my projects end up as multi-module maven builds, with a top-level pom.xml in the trunk directory and sub pom.xml files in each module. For big projects, I even end up with more that two levels of POM inheritance. All this adds up to how many pom.xml files the project contains, and to the burden of updating the version in each of them.
This is why we created this little find+perl one-liner on my current project, to enable a quick and easy pom.xml version element upgrade. Here is the one-liner:
find . -name "pom.xml" -type f -exec \
perl -0777 -pi -e \
's#(<project.*?<version>)1.9-SNAPSHOT</version>#${1}1.9.0</version>#s' {} \;
The above line upgrades from version 1.9-SNAPSHOT to a final 1.9.0, which is what we do when preparing a release where SNAPSHOT was used on trunk and a real final version number is to be used for the release.
Here is a bit of explanation of the one-liner:
- The find finds files only (-type f) with the name pom.xml recursively and executes the perl command-line on each
- The perl option -0777 sets the input record delimiter character ($/) to a non-existing character, to slurp all of the file into one long string, which enables us to match and replace into a single long line
- The -p option loops all files
- The -i does inplace editing of files (aka: The replace on the match is done directly into the file matched in)
- And finally, the /s modifier on the end of the pattern makes it a single match and in combination with -0777 also substitution
So, why do this, when we have the maven release plugin? Because the maven release plugin scares me! Have you seen all it does with your repo and all? Take a look at the actions done by the release:prepare goal. These actions includes changing poms, committing changes, tagging, changing poms again, … I want to be more in control than that
August 24, 2008
В·
polesen В·
3 Comments
Tags: maven, pom В· Posted in: Tools

3 Responses
The Maven Release Plugin automates a number of tasks for you and can be very beneficial. I always recommend running the release:prepare goal separately from the release:perform goal so that you can inspect the changes it has made before running the release:perform goal. Some people even commit the release.properties file that the release:prepare goal creates so that releases can be fully and easily replicated in the future if necessary. The find/perl combination you present performs some of the same tasks but is probably more comfortable and familiar. I used to do a similar thing but since the Maven Release Plugin has been improved and works well, I no longer use separate tools for releases.
Well, I can keep in mind a short command line like ‘mvn release:prepare’ but not this perl script
. I used to do similar trick with my poms and other manual hack with releases, but for a while maven release plugin works perfectly for me.
Recent Links Tagged With "maven" - JabberTags - October 30, 2008
[...] public links >> maven Down in the Cellar Saved by kaiba1234 on Wed 29-10-2008 Upgrade Maven POM Versions Recursively Saved by Matt1608 on Wed 29-10-2008 Comment on Pardon the appearance while we draft the next… [...]
Leave a Reply