Skip to content

Instantly share code, notes, and snippets.

@retanoj
Last active February 15, 2020 05:04
Show Gist options
  • Save retanoj/9fa3605627f4bb79576b324b4f7caab5 to your computer and use it in GitHub Desktop.
Save retanoj/9fa3605627f4bb79576b324b4f7caab5 to your computer and use it in GitHub Desktop.
获取jar包中maven管理的依赖库
import zipfile
def extractDependencyFromJar(jar_file: str) -> list:
"""
获取jar包中maven管理的依赖库
:param jar_file: jar绝对路径
:return: list 依赖列表 [ groupId:artifactId:version ...]
"""
dependency_list = []
zfile = zipfile.ZipFile(jar_file)
for file in zfile.namelist():
if file.startswith('META-INF/maven') and file.endswith('pom.properties'):
content = zfile.read(file).decode('utf-8').split('\n')
groupId, artifactId, version = "", "", ""
for line in content:
if line.startswith('groupId'):
groupId = line.strip().split('=')[1]
elif line.startswith('artifactId'):
artifactId = line.strip().split('=')[1]
elif line.startswith('version'):
version = line.strip().split('=')[1]
dependency_list.append('{groupId}:{artifactId}:{version}'.format(
groupId=groupId, artifactId=artifactId, version=version
))
return dependency_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment