解决maven jar包冲突,使用python脚本检查

       在mvn工程中,经常因为多级引入导致jar包冲突,然后会使用 mvn dependency:tree>dependency.txt 导出mvn依赖树关系 , 然后从dependency.txt文件中解析出冲突的jar包即可。解析的python脚本如下:

 

#encoding=utf-8
import re

maven_pattern = "(\s*)\\[INFO\\](.+):(.+):(.+)"

dependency_map = {}


class Dependency(object):

    def __init__(self, groupId, artifactId, version):
        self.groupId = groupId
        self.artifactId = artifactId
        self.version = version

    def like(self, other):
        if self.groupId == other.groupId and self.artifactId == other.artifactId:
            return True

        return False

    def hash_value(self):
        return hash(self.groupId+":"+self.artifactId)

    def __str__(self):
        return "%s:%s:%s" % (self.groupId, self.artifactId, self.version)


def is_maven_line(line):

    if re.match(maven_pattern, line) is None:
        return False
    else:
        return True


def parse_dependency(line):
    if not isinstance(line, str):
        return

    splits = line.split(" ")
    if len(splits) < 3:
        return

    if splits[1] == '---' or splits[2] == 'Finished':  # root的直接忽略
        return

    dependency_info_line = splits[-1]
    dependency_info = dependency_info_line[:-1]

    lists = dependency_info.split(":")
    if len(lists) < 4:
        return
    d = Dependency(lists[0], lists[1], lists[3])
    key = d.hash_value()
    if key in dependency_map:
        print(dependency_map.get(key))
        print(d)
    else:
        dependency_map[key] = d
    # print("%s %s %s" % (groupId, artifactId, version))


if __name__ == "__main__":
    f = open("dependency.txt", 'r')
    try:
        lines = f.readlines()
        for line in lines:
            if is_maven_line(line):
                parse_dependency(line)
    finally:
        f.close()

执行效果如下:

你可能感兴趣的:(python,java)