Skip to content

Instantly share code, notes, and snippets.

@relistan
Created January 18, 2024 18:28
Show Gist options
  • Save relistan/a643427fdd41814bd1b688aa80190495 to your computer and use it in GitHub Desktop.
Save relistan/a643427fdd41814bd1b688aa80190495 to your computer and use it in GitHub Desktop.
Download Maven JARs from XML snippet
#!/usr/bin/env ruby
# This script accepts a blob of XML on ARGF/stdin like the following, and
# downloads the jar to the current directory.
#
# <dependency>
# <groupId>com.datastax.cassandra</groupId>
# <artifactId>cassandra-driver-core</artifactId>
# <version>3.0.8</version>
# <classifier>shaded</classifier>
# <!-- Because the shaded JAR uses the original POM, you still need
# to exclude this dependency explicitly: -->
# <exclusions>
# <exclusion>
# <groupId>io.netty</groupId>
# <artifactId>*</artifactId>
# </exclusion>
# </exclusions>
# </dependency>
require 'excon'
require 'nokogiri'
BASE_URL = "https://repo1.maven.org/maven2/"
class MavenDependency
def initialize(xml_str)
doc = Nokogiri::Slop(xml_str)
@group_id = doc.dependency.groupId.content
@artifact_id = doc.dependency.artifactId.content
@version = doc.dependency.version.content
@classifier = doc.dependency.classifier.content
end
def download
puts "Downloading #{url}"
response = Excon.get(url)
abort "Failed to download. Status #{response.status}" if response.status != 200
File.write(filename, response.body)
puts "Success: #{filename} downloaded"
end
private
def filename
[@artifact_id, @version, @classifier].compact.join('-') + '.jar'
end
def url
url = (@group_id.split(/\./) + [@artifact_id, @version]).join('/') + '/'
"#{BASE_URL}#{url}#{filename}"
end
end
dep = MavenDependency.new(ARGF.read)
dep.download
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment