Skip to content

Instantly share code, notes, and snippets.

@octylFractal
Last active July 28, 2016 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octylFractal/4c65dedd57835bde17dde975be42e76f to your computer and use it in GitHub Desktop.
Save octylFractal/4c65dedd57835bde17dde975be42e76f to your computer and use it in GitHub Desktop.
A script that will scan maven repositories for an artifact.
#!/usr/bin/env python3
from pathlib import Path
import requests
import sys
from bs4 import BeautifulSoup
def xml_parser(text):
return BeautifulSoup(text, 'xml')
def resolve(target, repos):
group, artifact, version = target.split(':')
group = group.replace('.', '/')
located = []
for repo in repos:
if isinstance(repo, Path):
# File resolve
p = repo/group/artifact/version
#print('Checking', p)
if p.exists():
located.append(str(p))
continue
else:
# URL resolve
url = '/'.join((repo, group, artifact, version))
#print('Pulling', url)
res = requests.get(url)
#print(res)
if res.status_code != 404:
located.append(url)
continue
print('Not in', repo)
if located:
return located
print('404 Not found.')
sys.exit(1)
MAVEN_HOME = Path.home()/'.m2'
def parse_settings_xml():
settingsxml = MAVEN_HOME/'settings.xml'
if not settingsxml.exists():
return []
with settingsxml.open('r') as f:
soup = xml_parser(f)
repos = soup.find_all('repository')
return [repo.find('url').string for repo in repos]
def read_list(name, replacements):
cmd = lambda: input('Entry for ' + name + ': ').strip()
ls = []
usr_in = cmd()
while usr_in:
usr_in = replacements.get(usr_in, usr_in)
ls.append(usr_in.rstrip('/'))
usr_in = cmd()
return ls
def main():
repos = read_list('Repos', {
'central': 'https://repo1.maven.org/maven2/'
})
# maven local goes first
repos.insert(0, MAVEN_HOME/'repository')
xml_repos = parse_settings_xml()
print('Adding', xml_repos, 'from settings.xml')
repos += xml_repos
target = input('Maven target (group:artifact:version): ')
print('\nResolving...')
resolve_list = resolve(target, repos)
print('Found at the following locations:')
for entry in resolve_list:
print(entry)
if __name__ == '__main__':
main()
#!/usr/bin/env bash
## This script is a helper for those who just wish to search Maven Central - ./<script> group:artifact:version
echo "central
$1" | ./maven-resolver.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment