How can I update a property in a Maven POM?

Is there a simple way to rewrite a Maven property entry to a specific value

Since version 2.5 we can use set-property (documentation):

mvn versions:set-property -Dproperty=your.property -DnewVersion=arbitrary_value

As documented, the set-property goal does not perform any 'sanity checks' on the value you specify, so it should always work, but you should use with care.


How to update property in existing POM:

Try to use filtering in maven-resource-plugin:

  1. specify version in property file;
  2. add custom filter with path to this file (in child pom.xml, where dependency should be injected);
  3. update version in property file;
  4. run build.

Advantages:

  • it should work;
  • version is specified only once;
  • property file could be added under version control;
  • process-resources is one of the first maven lifecycle steps.

Disadvantages:

  • well, pom.xml still uses placeholder;
  • additional work to automatically update property file from initial build (too complicated, I suppose there should be easier solutions).

How to provide propery on build time:

You could specify any property by build parameter.

For example, I have property in my pom.xml like:

<properties>
    <build.date>TODAY</build.date>
</properties>

To change it during build I simply use parameter:

mvn compile -Dbuild.date=10.10.2010

I'm pretty sure it will work for version as well. Also, properties from top level projects are inherited by childs.


The newVersion parameter is badly documented (as is most of this plugin). By checking the integration tests, I see it takes a Maven version range not a simple version number. Also, it does not allow you to provide any value - it must be a valid one that Maven can resolve. The parameter would be better if it was called constrainRange

For anyone else in future, try this:

mvn versions:update-property -Dproperty=frontend.version -DnewVersion=[0.13.2]  

If you need to update to a snapshot make sure you set the property allowSnapshots to true

mvn versions:update-property -Dproperty=frontend.version -DnewVersion=[0.13.2] -DallowSnapshots=true

I had the same problem and found nothing that changes pom properties in the file. I ended up using sed like you suggested:

cat pom.xml | sed -e "s%<util.version>0.0.1-SNAPSHOT</util.version>%<util.version>$bamboo_planRepository_branch</util.version>%" > pom.xml.transformed;
rm pom.xml;
mv pom.xml.transformed pom.xml;