-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_pom.py
149 lines (97 loc) · 4.01 KB
/
change_pom.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import shutil
import xml.dom.minidom
import getopt
def err(message):
print("%s 发生了错误!!!" %(message))
sys.exit(1)
def replaceOsSep(src):
return src.replace("/",os.sep).replace("\\",os.sep)
def setWorkPath():
base_path = os.path.abspath( os.path.join( __file__ ,"../" ) )
os.chdir( base_path)
def printHelp():
print('''
替换 xml 文件中的值。
python change_pom.py -f 根pom.xml所在路径(默认当前) -g 新的值
''')
def getArgs():
groupId = ""
file = ""
try:
opts, args = getopt.getopt(sys.argv[1:], "g:f:h", ["groupId=","file="])
except getopt.GetoptError:
printHelp()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printHelp()
sys.exit()
elif opt in ("-f", "--file"):
file = arg
elif opt in ("-g", "--groupId"):
groupId = arg
return file,groupId
def getModulesData():
dom = xml.dom.minidom.parse('pom.xml')
root = dom.documentElement
groupId = root.getElementsByTagName('groupId')[0].childNodes[0].data;
artifactId = root.getElementsByTagName('artifactId')[0].childNodes[0].data;
mns = filter(lambda x: x.nodeType == 1, root.getElementsByTagName('modules')[0].childNodes);
modules = list( map(lambda x: x.childNodes[0].data ,mns) )
return groupId,artifactId,modules
def resetGroupId(newGroupId):
dom = xml.dom.minidom.parse('pom.xml')
root = dom.documentElement
groupIdDom = root.getElementsByTagName('groupId')[0].childNodes[0];
if newGroupId:
groupIdDom.data = newGroupId
with open('pom.xml','w',encoding='UTF-8') as fh:
fh.write(dom.toxml())
fh.flush()
print("重新设置了根 pom.xml")
def resetSubGroupId(module,fileName,groupId,artifactId,newGroupId,modules):
if module:
os.chdir(module);
dom = xml.dom.minidom.parse(fileName)
root = dom.documentElement
groupIdDom = root.getElementsByTagName('groupId')[0].childNodes[0];
mns = list(filter(lambda x: x.nodeType == 1, root.getElementsByTagName('parent')[0].childNodes));
t_groupIdDom= list(filter( lambda x: x.tagName == "groupId",mns ))[0].childNodes[0]
if t_groupIdDom.data == groupId and newGroupId:
t_groupIdDom.data = newGroupId
groupIdDom.data = newGroupId
mns2 = list(filter(lambda x: x.nodeType == 1, root.getElementsByTagName('dependencies')[0].childNodes));
for dependency in mns2:
for module2 in modules:
t2_groupIdDom = list(filter( lambda x: x.tagName == "groupId", filter(lambda x:x.nodeType == 1, dependency.childNodes ) ))[0].childNodes[0]
t2_artifactIdDom = list(filter( lambda x: x.tagName == "artifactId", filter(lambda x:x.nodeType == 1, dependency.childNodes ) ))[0].childNodes[0]
if t2_groupIdDom.data == groupId and \
t2_artifactIdDom.data == module2 and\
newGroupId:
t2_groupIdDom.data = newGroupId
with open(fileName,'w',encoding='UTF-8') as fh:
fh.write(dom.toxml())
fh.flush()
print("重新设置了: %s"%(module))
if __name__=='__main__':
file,newGroupId = getArgs()
setWorkPath();
if file:
os.chdir(file)
groupId,artifactId,modules = getModulesData()
print("-------------------------------------------------------------------------------")
print(os.linesep)
resetGroupId(newGroupId)
resetSubGroupId("","pom.xml",groupId,artifactId, newGroupId,modules)
resetSubGroupId("","pom_component.xml",groupId,artifactId, newGroupId,modules)
resetSubGroupId("","pom_starter.xml",groupId,artifactId, newGroupId,modules)
for module in modules:
resetSubGroupId(module,"pom.xml",groupId,artifactId, newGroupId,modules)
os.chdir("../")
print("")
print("设置 %s %s 完成!"%(newGroupId,artifactId))
print("-------------------------------------------------------------------------------")