Last active
December 16, 2015 12:19
-
-
Save derkosak/5434126 to your computer and use it in GitHub Desktop.
Very (very) simple maven artifact server written with Buildr and Sinatra
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The REPO_DIR variable should point to the same folder configured for the local artifact repository used by Buildr (local maven repository).