Created
August 20, 2014 19:58
-
-
Save bdkosher/740b354ebe99cf7fa49b to your computer and use it in GitHub Desktop.
Utility script for scanning a lib directory full of jar files and generate the Maven dependency list from it via screen scraping mvnrepository.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2') | |
@Grab(group='org.springframework', module='spring-web', version='3.2.10.RELEASE') | |
import static org.springframework.web.util.UriUtils.encodeQuery | |
import groovy.transform.TupleConstructor | |
class MvnRepository { | |
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser()) | |
String findGroupId(String artifactId) { | |
String groupId = null | |
new URL("http://mvnrepository.com/search.html?query=${encodeQuery(artifactId, 'UTF-8')}").withReader { page -> | |
def html = parser.parse(page) | |
html.body.div.div[2].p.each { result -> | |
if (groupId == null && artifactId == result.a[2].text()) { | |
groupId = result.a[1].text() | |
} | |
} | |
} | |
groupId | |
} | |
} | |
@TupleConstructor(excludes='mvnRepo') | |
class LibScanner { | |
private mvnRepo = new MvnRepository() | |
String dir | |
void eachJar(Closure closure) { | |
new File(dir).eachFileMatch(~/.*\.jar$/) { file -> | |
if (!file.name.contains('-javadoc.') && !file.name.contains('-sources.')) { | |
closure(file) | |
} | |
} | |
} | |
void eachJarGAV(Closure closure) { | |
eachJar { jar -> | |
def name = jar.name - '.jar' | |
int separatorIdx = name.lastIndexOf('-') | |
def artifactId = name.substring(0, separatorIdx) | |
def groupId = mvnRepo.findGroupId(artifactId) ?: 'UNKNOWN' | |
def version = name.substring(separatorIdx) - '-' | |
closure(groupId, artifactId, version) | |
} | |
} | |
void printPomDependencies() { | |
eachJarGAV { g, a, v -> | |
println """ | |
<dependency> | |
<groupId>$g</groupId> | |
<artifactId>$a</artifactId> | |
<version>$v</version> | |
</dependency> | |
""" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment