Last active
September 29, 2015 23:57
-
-
Save ajtucker/1689247 to your computer and use it in GitHub Desktop.
Search Sonatype's Maven repository for artifacts matching *.jar files under a given directory, based on their SHA1 sum.
This file contains hidden or 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
| #!/bin/sh | |
| exec scala -savecompiled "$0" "$@" | |
| !# | |
| /* | |
| * Search Sonatype's Maven repository for artifacts matching *.jar | |
| * files under a given directory, based on their SHA1 sum. | |
| * | |
| */ | |
| import java.security.{MessageDigest, DigestInputStream} | |
| import java.io._ | |
| import scala.xml._ | |
| import scala.collection.JavaConversions._ | |
| var dependencies: List[(String, Node)] = Nil | |
| var notFound: List[String] = Nil | |
| def sha1sum(file: File): String = { | |
| val md = MessageDigest.getInstance("SHA1") | |
| val dis = new DigestInputStream( | |
| new BufferedInputStream( | |
| new FileInputStream(file) | |
| ), md) | |
| while (dis.read != -1) {} | |
| md.digest.toList.map(b => "%02x".format(b)).mkString | |
| } | |
| def jarsUnder(dir: File): List[(String, String)] = { | |
| val jars = for (file <- dir.listFiles.toList) yield { | |
| if (file.isDirectory) { | |
| jarsUnder(file) | |
| } else if (file.getName.endsWith(".jar")) { | |
| List((file.getName, sha1sum(file))) | |
| } else { | |
| Nil | |
| } | |
| } | |
| jars.flatten | |
| } | |
| val searchUrl = "https://repository.sonatype.org/service/local/identify/sha1/" | |
| if ((args.size == 0) || (args(0) == "-h")) { | |
| println(""" | |
| PomPom searches the Sonatype Maven repository for JAR artifacts matching *.jar | |
| files under a given directory. The matching is based on SHA1 checksums. | |
| The output is given as an XML fragment to be used in a Maven POM file. | |
| Usage: pompom libdirectory [libdirectory*] | |
| """) | |
| sys.exit | |
| } | |
| val maven = (args(0) != "-i") | |
| val fileNames = if (maven) args else args.tail | |
| val deps = <dependencies>{ | |
| for { | |
| dir <- fileNames.map(new File(_)) | |
| if dir.isDirectory | |
| (jar, digest) <- jarsUnder(dir) | |
| } yield { | |
| try { | |
| val result = XML.load(searchUrl + digest) | |
| if (maven) { | |
| <dependency> | |
| {result \ "groupId"} | |
| {result \ "artifactId"} | |
| {result \ "version"} | |
| </dependency> | |
| } else { | |
| <dependency org={(result \ "groupId").text} name={(result \ "artifactId").text} rev={(result \ "version").text} /> | |
| } | |
| } catch { | |
| case e: FileNotFoundException => | |
| Comment(" Not found: %s ".format(jar)) | |
| } | |
| } | |
| }</dependencies> | |
| val pp = new PrettyPrinter(76, 2) | |
| println(pp.format(deps)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment