-
Notifications
You must be signed in to change notification settings - Fork 11
/
modify_versions.py
98 lines (67 loc) · 2.74 KB
/
modify_versions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import glob
import argparse
import xml.etree.ElementTree as ET
ET.register_namespace('', "http://maven.apache.org/POM/4.0.0")
ns = "{http://maven.apache.org/POM/4.0.0}"
ROOT_POM = "gmql"
def modifyChildrenPom(pom_xml, new_version, groupId):
# modify the parent section
pom_xml.find("./{}parent/{}version".format(ns, ns)).text = new_version
# modify the dependencies
for depenendencyNode in pom_xml.findall("./{}dependencies/".format(ns)):
d_group_id = depenendencyNode.find("./{}groupId".format(ns)).text
if groupId == d_group_id:
depenendencyNode.find("./{}version".format(ns)).text = new_version
def modifyRootPom(root_pom_xml, new_version):
versionNode = getVersionNode(root_pom_xml)
versionNode.text = new_version
def getNewVersion(old_version, branch_name):
if (old_version.endswith("SNAPSHOT")) & (branch_name != 'master'):
parts = old_version.split("-")
new_version = parts[0] + "-" + branch_name + "-" + parts[1]
else:
new_version = old_version
return new_version
def getArtifactId(pom_f_xml):
return pom_f_xml.find("./{}artifactId".format(ns)).text
def getGroupId(pom_f_xml):
return pom_f_xml.find("./{}groupId".format(ns)).text
def getVersionNode(pom_f_xml):
return pom_f_xml.find("./{}version".format(ns))
def getVersion(pom_f_xml):
return getVersionNode(pom_f_xml).text
def isRootPom(pom_f_xml):
return getArtifactId(pom_f_xml) == ROOT_POM
def main():
parser = argparse.ArgumentParser(description='Modify the versions in the pom files')
parser.add_argument('branch_name', help="Name of the branch of the current build")
branch_name = parser.parse_args().branch_name
if (branch_name.startswith("\"") & branch_name.endswith("\"")) | \
(branch_name.startswith("'") & branch_name.endswith("'")):
branch_name = branch_name[1:-1]
print("The current branch is {}".format(branch_name))
# root pom
root_pom_path = "./pom.xml"
root_pom_xml = ET.parse(root_pom_path)
root_version = getVersion(root_pom_xml)
root_group_id = getGroupId(root_pom_xml)
root_artifact_id = getArtifactId(root_pom_xml)
print("Root POM:\n\
\tGroupId: {}\n\
\tVersion: {}\n\
\tArtifactId: {}".format(root_group_id, root_version, root_artifact_id))
new_version = getNewVersion(root_version, branch_name)
print("New Version: {}".format(new_version))
modifyRootPom(root_pom_xml, new_version)
root_pom_xml.write(root_pom_path)
print("{:<30}{} --> {}".format(root_pom_path, root_version, new_version))
# all the others
other_pom_paths = glob.glob("./*/pom.xml")
print("Changing POM files")
for pom_path in other_pom_paths:
pom_xml = ET.parse(pom_path)
modifyChildrenPom(pom_xml, new_version, root_group_id)
pom_xml.write(pom_path)
print("{:<30}{} --> {}".format(pom_path, root_version, new_version))
if __name__ == '__main__':
main()