Home > front end >  Maven - Updating the version in parent tag in pom.xml
Maven - Updating the version in parent tag in pom.xml

Time:09-29

In Maven, I would like to update the version inside a parent tag with the good practice :

<parent>
        <groupId>com.mycompany.app.core</groupId>
        <artifactId>my-app-parent</artifactId>
        <version>3.0.1</version>
</parent>

In my context, I want to update the version 3.0.1-SNAPSHOT to 3.0.1 (release) and then 3.0.2-SNAPSHOT (after the release).

I have find a way for updating the version in python.

The script works fine, here is the code :

#!/usr/bin/python
import xml.etree.ElementTree as ET
import sys
import os 
 
 
if len(sys.argv) != 4 :
   print("Wrong number of params")
   os._exit(1)
 
# Script param
project_pom_file = str(sys.argv[1])
old_version=str(sys.argv[2])
new_version = str(sys.argv[3])
 
print 'Pom file : '   project_pom_file
print 'Old version of project : ', old_version
print 'New version of project : ', new_version
 
ET.register_namespace('', "http://maven.apache.org/POM/4.0.0")
# Parsing pom.xml
xmlTree = ET.parse(project_pom_file)
root = xmlTree.getroot()
# namespace
 
ns = {'mvn-apache': 'http://maven.apache.org/POM/4.0.0'}
     
for e in xmlTree.findall("mvn-apache:parent" , ns):
   artifactId = e.find('mvn-apache:artifactId' , ns)
   version = e.find('mvn-apache:version' , ns)
   if artifactId.text == 'my-app-parent' and version.text != old_version : 
       print "ERROR : Old version not match "   version.text
       # FAIL
       os._exit(1)
   elif artifactId.text == 'my-app-parent' and version.text == old_version  :
       print ( 'GroupID=com.mycompany.app.core and artifactId=my-app-parent found')
       print 'Updating version '   old_version   ' to '   new_version
       #Change the version
       version.text = new_version
       
       #Write the modified xml file.        
       xmlTree.write(project_pom_file  , xml_declaration = True, encoding = 'UTF-8', method = 'xml')

We can run the script like this :

rebase.py <file> <old_version> <new_version>

The script check if the old version match with the parameter, it's a security I have added.

I don't like the way of parsing and regenerate the XML file, we can lost data like comments. I know there are lot of maven plugin but I haven't find a relevant one. Is there a way to update the parent version with a Maven command line ?

I have tried this :

 mvn versions:set -DgroupId=com.mycompany.app.core -DartifactId=my-app-parent -DoldVersion=3.* -DnewVersion=3.0.1

The command doesn't work fine for the parent tag.

Thanks for your help.

CodePudding user response:

As khmarbaise pointed out you can use update-parent goal so in your example it would be:

mvn versions:update-parent "-DparentVersion=[yourVersion]" -DallowSnapshots=true 
  • Related