-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvn-parse-pom.py
executable file
·62 lines (57 loc) · 2.23 KB
/
mvn-parse-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
#!/usr/bin/env python3
import sys
import argparse
import xml.etree.ElementTree
MAVEN_NS = { 'maven': 'http://maven.apache.org/POM/4.0.0' }
def parse(pom_file, format):
root = xml.etree.ElementTree.parse(pom_file).getroot()
def text(name, search_parent=True, default=None):
elements = root.findall('maven:%s' % name, MAVEN_NS)
if not elements:
elements = root.findall(name)
if not elements and search_parent:
parent = root.findall('maven:parent', MAVEN_NS)
if not parent:
parent = root.findall('parent')
if parent:
if len(parent) > 1:
raise Exception('more than one element found: parent')
elements = parent[0].findall('maven:%s' % name, MAVEN_NS)
if not elements:
elements = parent[0].findall(name)
if elements:
if len(elements) > 1:
raise Exception('more than one element found: %s' % name)
return elements[0].text
if not default:
raise Exception('element not found: %s' % name)
return default
groupId = text('groupId')
groupPath = groupId.replace('.', '/')
artifactId = text('artifactId')
version = text('version')
packaging = text('packaging', False, 'jar')
if format == 'gav':
print('%s:%s:%s' % (groupId, artifactId, version))
elif format == 'artifact':
print('%s-%s.%s' % (artifactId, version, packaging))
elif format == 'path':
print('%s/%s/%s' % (groupPath, artifactId, version))
else:
raise Exception('unknown format: %s' % format)
def main():
parser = argparse.ArgumentParser(prog='mvn-parse-pom')
parser.add_argument('pom', metavar='<pom>', nargs='+', help='POM file')
parser.add_argument('-F', '--format', metavar='<format>', default='gav',
help='Output format: gav, artifact or path')
args = parser.parse_args()
exit_code = 0
for pom_file in args.pom:
try:
parse(pom_file, args.format)
except Exception as e:
print('error parsing %s: %s' % (pom_file, e), file=sys.stderr)
exit_code = 1
sys.exit(exit_code)
if __name__ == '__main__':
main()