Skip to content

Instantly share code, notes, and snippets.

@derkosak

derkosak/m2server.rb

Last active Dec 16, 2015
Embed
What would you like to do?
Very (very) simple maven artifact server written with Buildr and Sinatra
require 'rubygems' if RUBY_VERSION < '1.9'
require 'sinatra'
require 'fileutils'
require 'buildr'
# The REPO_DIR variable should point to the same folder
# configured for the local artifact repository used by
# Buildr (a.k.a. the local maven repository).
REPO_DIR = dir=File.expand_path("#{ENV['TEMP']}/repo/")
# Fake up a build file. Identifies directory from which settings are loaded
Buildr.application.instance_variable_set("@rakefile",'./noexist')
Buildr.settings.build["repositories"] = {
'local'=>REPO_DIR,
"remote" => ['http://uk.maven.org/maven2','http://www.ibiblio.org/maven2/',"http://repo1.maven.org/maven2"]}
def search_for_artifact(spec)
art=nil
begin
art=Buildr.artifact(spec)
art.invoke unless art==nil
rescue Exception => e
puts e.message
end
art
end
# Test the artifact search.
# Should download the artifact if needed, and return the local path to the JAR file.
puts search_for_artifact('commons-cli:commons-cli:jar:1.0')
# Fake artifact, should not be found and return nil
puts search_for_artifact('nonexistant')
# exit
# Retrieves an artifact from the local repository
get '/*' do
puts "\n#{params[:splat].join()}\n"
complete_path=params[:splat].join()
begin
unless complete_path.include?('..')
dir=File.expand_path("#{REPO_DIR}/#{File.dirname(complete_path)}")
file_name=File.basename(complete_path)
complete_file="#{dir}/#{file_name}"
puts complete_file
File.open("#{dir}/#{file_name}", 'rb') do |file|
if File::exists? file
content_type 'application/octet-stream'
attachment(file_name)
response.write(file.read)
file.close
else
404
end
end
else
403
end
rescue Exception => e
puts "Error reading artifact: #{e.message}"
500
end
end
# Unused, for debugging
post '/*' do
puts "\n#{params[:splat].join(',')}\n"
end
# Upload a artifact to the local repository (deploy)
put '/*' do
puts "\n#{params[:splat].join(',')}\n"
complete_path=params[:splat].join()
begin
unless complete_path.include?('..')
dir=File.expand_path("#{REPO_DIR}/#{File.dirname(complete_path)}")
FileUtils.mkdir_p(dir)
file_name=File.basename(complete_path)
complete_file="#{dir}/#{file_name}"
puts complete_file
file=File.new(complete_file, 'w+b')
file.write(request.body.read)
file.close
200
else
403
end
rescue Exception => e
puts "Error saving artifact: #{e.message}"
500
end
end
@derkosak

This comment has been minimized.

Copy link
Owner Author

@derkosak derkosak commented Apr 22, 2013

The REPO_DIR variable should point to the same folder configured for the local artifact repository used by Buildr (local maven repository).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.