Skip to content

Instantly share code, notes, and snippets.

@JerryPreissler
Created June 25, 2013 19:03
Show Gist options
  • Save JerryPreissler/5861368 to your computer and use it in GitHub Desktop.
Save JerryPreissler/5861368 to your computer and use it in GitHub Desktop.
Short jruby function to require all jar files from a maven classpath.
# Require the jar files from a Maven classpath in JRuby
#
# ==== Attributes
# * +path+ to the project root directory where Maven should be run
#
def maven_require(path)
save_dir = Dir.getwd
throw "could not change to dir #{path}" unless Dir.exists?(path)
begin
Dir.chdir(path)
data = %x[mvn -X clean compile]
cp = nil
lines = data.split("\n")
lines.each { |line|
if line =~ /classpathElements = \[(.*)\]/
cp = $1
break
end
}
if cp
entries = cp.split(",")
entries.each {|entry|
if entry =~ /(\s*)(.*)\.jar/
jar_path = $2 + ".jar"
require jar_path
puts "required #{jar_path}"
end # if entry
} # entries.each
end # if cp
ensure
Dir.chdir(save_dir) if save_dir
end
return "done"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment