# coding = utf-8标签:xml,http,4.0,python,maven,pom,apache,org,POM From: https://www.cnblogs.com/jinxi7021/p/16952486.html
# @author :今夕
# @Time :2022.12.05 10:40
# @file :test.py.py
# @software :PyCharm
import os
import xml.etree.ElementTree as ET
#保留注释信息用
class CommentedTreeBuilder (ET.TreeBuilder):
def comment(self,data):
self.start(ET.Comment,{})
self.data(data)
self.end(ET.Comment)
#获取项目目录
dirs=os.listdir("C:/Disk/gitwork/inaszj/fc-service")
#遍历目录
for dir in dirs:
flag=os.path.isdir("C:/Disk/gitwork/inaszj/fc-service/%s"%dir)
if flag==True:
if dir!='Fc5GcService':#排除不需要的目录
print(dir)
with open("C:/Disk/gitwork/inaszj/fc-service/%s/pom.xml"%dir, 'tr', encoding='utf-8') as rf:
parser = ET.XMLParser(target=CommentedTreeBuilder())
tree = ET.parse(rf, parser=parser)
# tree遍历
for node in tree.findall('.//{http://maven.apache.org/POM/4.0.0}dependency'):
for node in tree.findall('.//{http://maven.apache.org/POM/4.0.0}dependency'):
groupIdNode = node.find('.{http://maven.apache.org/POM/4.0.0}groupId')
artifactNode = node.find('.{http://maven.apache.org/POM/4.0.0}artifactId')
if groupIdNode.text == 'csf':#修改groupid 是csf的依赖版本号
print(groupIdNode.text)
print(artifactNode.text)
node.find('.{http://maven.apache.org/POM/4.0.0}version').text = '3.0'
print(node.find('.{http://maven.apache.org/POM/4.0.0}version').text)
tree.write("C:/Disk/gitwork/inaszj/fc-service/%s/pom.xml"%dir, default_namespace='http://maven.apache.org/POM/4.0.0', encoding='UTF-8')