jan-h
9/23/2015 - 9:25 AM

Find Jars on Maven Central with PowerShell

Find Jars on Maven Central with PowerShell

#http://stackoverflow.com/a/27516242/1678525
$log = 'jarfind.log'
$pom = 'jarfind.pom'

Get-Date | Tee-Object -FilePath $log

$jars = gci .\ -Filter *.jar

"<dependencies>" | Tee-Object -FilePath $pom

foreach ($jar in $jars)
{
    $sha = Get-FileHash -Algorithm SHA1 -Path $jar.FullName | select -ExpandProperty hash
    $name = $jar.Name
    $json = Invoke-RestMethod "http://search.maven.org/solrsearch/select?q=1:%22$($sha)%22&rows=20&wt=json"
    "Found $($json.response.numfound) jars with sha1 matching that of $($name)..." | Tee-Object -FilePath $log -Append
    $jarinfo = $json.response.docs
    $jarinfo | Tee-Object -FilePath $log -Append
    If($json.response.numFound -ne 0)
    {
        $groupId = $json.response.docs.g
        $artefactId = $json.response.docs.a
        $versionId = $json.response.docs.v
        $dependency  = "    <dependency>`r`n"
        $dependency += "        <groupId>$groupId</groupId>`r`n"
        $dependency += "        <artifactId>$artefactId</artifactId>`r`n"
        $dependency += "        <version>$versionId</version>`r`n"
        $dependency += "    </dependency>"
        $dependency | Tee-Object -FilePath $pom -Append
    }
}
"</dependencies>" | Tee-Object -FilePath $pom -Append