lib/java/artifact.rb in buildr-1.2.7 vs lib/java/artifact.rb in buildr-1.2.8

- old
+ new

@@ -35,10 +35,14 @@ # The version number. attr_reader :version # Optional artifact classifier. attr_reader :classifier + def snapshot? + version =~ /-SNAPSHOT$/ + end + # :call-seq: # to_spec_hash() => Hash # # Returns the artifact specification as a hash. For example: # com.example:app:jar:1.2 @@ -125,10 +129,14 @@ def apply_spec(spec) spec = Artifact.to_hash(spec) ARTIFACT_ATTRIBUTES.each { |key| instance_variable_set("@#{key}", spec[key]) } self end + + def group_path + group.gsub(".", "/") + end end # A file task referencing an artifact in the local repository. @@ -293,29 +301,69 @@ # # This method attempts to download the artifact from each repository in the order in # which they are returned from #remote, until successful. It always downloads the POM first. def download() puts "Downloading #{to_spec}" if Rake.application.options.trace - remote = Buildr.repositories.remote + remote = Buildr.repositories.remote.map { |repo_url| URI === repo_url ? repo_url : URI.parse(repo_url) } + remote = remote.each { |repo_url| repo_url.path += "/" unless repo_url.path[-1] == "/" } fail "No remote repositories defined!" if remote.empty? - remote.find do |repo_url| - repo_url = URI.parse(repo_url) unless URI === repo_url - repo_url.path += "/" unless repo_url.path[-1] == "/" + exact_success = remote.find do |repo_url| begin - path = group.gsub(".", "/") + "/#{id}/#{version}/#{File.basename(name)}" + path = "#{group_path}/#{id}/#{version}/#{File.basename(name)}" mkpath File.dirname(name), :verbose=>false URI.download repo_url + path, name true rescue URI::NotFoundError false rescue Exception=>error puts error if verbose puts error.backtrace.join("\n") if Rake.application.options.trace false end - end or fail "Failed to download #{to_spec}, tried the following repositories:\n#{remote.join("\n")}" + end + + if exact_success + return + elsif snapshot? + download_m2_snapshot(remote) + else + fail_download(remote) + end end + def download_m2_snapshot(remote_uris) + remote_uris.find do |repo_url| + snapshot_url = current_snapshot_repo_url(repo_url) + if snapshot_url + begin + URI.download snapshot_url, name + rescue URI::NotFoundError + false + end + else + false + end + end or fail_download(remote_uris) + end + + def current_snapshot_repo_url(repo_url) + begin + metadata_path = "#{group_path}/#{id}/#{version}/maven-metadata.xml" + metadata_xml = StringIO.new + URI.download repo_url + metadata_path, metadata_xml + metadata = REXML::Document.new(metadata_xml.string).root + timestamp = REXML::XPath.first(metadata, "//timestamp").text + build_number = REXML::XPath.first(metadata, "//buildNumber").text + snapshot_of = version[0, version.size - 9] + repo_url + "#{group_path}/#{id}/#{version}/#{id}-#{snapshot_of}-#{timestamp}-#{build_number}.#{type}" + rescue URI::NotFoundError + nil + end + end + + def fail_download(remote_uris) + fail "Failed to download #{to_spec}, tried the following repositories:\n#{remote_uris.join("\n")}" + end end # Holds the path to the local repository, URLs for remote repositories, and settings for release server. #