Last active
April 21, 2016 05:50
-
-
Save Dispader/4ab885517ab74b5a6a13ebb95d8420c2 to your computer and use it in GitHub Desktop.
a small utility to download JAR files from Maven Central, first revision
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
#!/usr/bin/env groovy | |
if ( args.size() < 1 ) { | |
scriptFile = getClass().protectionDomain.codeSource.location.path.split('/').last() | |
System.out.println("usage: ${scriptFile} [dependency]+*") | |
System.exit(0) | |
} | |
args.each { | |
println "downloading ${it}" | |
new Dependency(it)?.download() | |
} | |
class Dependency { | |
private String group, artifact | |
private Dependency() {} | |
Dependency(String dep) { | |
this.group = dep?.split(':')[0] | |
this.artifact = dep?.split(':')[1] | |
} | |
void download() { | |
redirectFollowingDownload(this.url, this.filename) | |
} | |
private String getUrl() { | |
"http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=${this.group}&a=${this.artifact}&v=LATEST" | |
} | |
private String getFilename() { | |
"${this.artifact}.jar" | |
} | |
private def redirectFollowingDownload( String url, String filename ) { | |
while( url ) { | |
new URL( url ).openConnection().with { conn -> | |
conn.instanceFollowRedirects = false | |
url = conn.getHeaderField( "Location" ) | |
if( !url ) { | |
new File( filename ).withOutputStream { out -> | |
conn.inputStream.with { inp -> | |
out << inp | |
inp.close() | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
...and this revision takes items from the command line. We check for arguments, but not that the dependencies are valid ('one' produces errors, arguments like 'log4j:log4j' or 'com.google.guava:guava').
./jarinator log4j:log4j com.google.guava:guava
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This revision takes items from a collection.