Class: Buildr::Repositories

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/core/artifact.rb

Overview

Holds the path to the local repository, URLs for remote repositories, and settings for the deployment repository.

Instance Method Summary collapse

Instance Method Details

#add(hash) ⇒ Object

Adds more remote repositories from a hash. See #remote.

For example:

repositories.remote.add :ibiblio=>"http://www.ibiblio.org/maven2"


208
209
210
# File 'lib/core/artifact.rb', line 208

def add(hash)
  remote.merge!(hash)
end

#deploy_toObject

Returns the current deployment repository configuration. This is a more convenient way to specify deployment in the Rakefile, and override it locally. For example:

# Rakefile
repositories.deploy_to[:url] ||= "sftp://example.com"
# buildr.rb
repositories.deploy_to[:url] = "sftp://acme.org"


262
263
264
# File 'lib/core/artifact.rb', line 262

def deploy_to()
  @deploy_to ||= {}
end

#deploy_to=(options) ⇒ Object

Specifies the deployment repository. Accepts a hash with the different repository settings (e.g. url, username, password). Anything else is interepted as the URL.

For example:

repositories.deploy_to = { :url=>"sftp://example.com/var/www/maven/",
                           :username="john", :password=>"secret" }

or:

repositories.deploy_to = "sftp://john:[email protected]/var/www/maven/"


250
251
252
253
# File 'lib/core/artifact.rb', line 250

def deploy_to=(options)
  options = { :url=>options } unless Hash === options
  @deploy_to = options
end

#download(spec) ⇒ Object

Attempts to download the artifact from one of the remote repositories and store it in the local repository. Returns the path if downloaded, otherwise raises an exception.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/core/artifact.rb', line 215

def download(spec)
  spec = Artifact.to_hash(spec) unless Hash === spec
  path = locate(spec)
  
  puts "Downloading #{Artifact.to_spec(spec)}" if Rake.application.options.trace
  return path if remote.any? do |repo_id, repo_url|
    begin
      rel_path = spec[:group].gsub(".", "/") +
        "/#{spec[:id]}/#{spec[:version]}/#{Artifact.hash_to_file_name(spec)}"
      Transports.perform URI.parse(repo_url.to_s) do |http|
        mkpath File.dirname(path), :verbose=>false
        http.download(rel_path, path)
        begin
          http.download(rel_path.ext("pom"), path.ext("pom"))
        rescue Transports::NotFound
        end
      end
      true
    rescue Exception=>error
      warn error if Rake.application.options.trace
      false
    end
  end
  fail "Failed to download #{Artifact.to_spec(spec)}, tried the following repositories:\n#{repositories.remote.values.join("\n")}"
end

#localObject

Returns the path to the local repository.

The default path is .m2/repository relative to the home directory. You can change the location of the local repository by using a symbol link or by setting a different path. If you set a different path, do it in the buildr.rb file instead of the Rakefile.



167
168
169
# File 'lib/core/artifact.rb', line 167

def local()
  @local ||= ENV["local_repo"] || File.join(ENV["HOME"], ".m2", "repository")
end

#local=(dir) ⇒ Object

Sets the path to the local repository.



172
173
174
# File 'lib/core/artifact.rb', line 172

def local=(dir)
  @local = dir ? File.expand_path(dir) : nil
end

#locate(spec) ⇒ Object

Locates an artifact in the local repository based on its specification.

For example:

locate :group=>"log4j", :id=>"log4j", :version=>"1.1"
  => ~/.m2/repository/log4j/log4j/1.1/log4j-1.1.jar


181
182
183
184
# File 'lib/core/artifact.rb', line 181

def locate(spec)
  spec = Artifact.to_hash(spec)
  File.join(local, spec[:group].split("."), spec[:id], spec[:version], Artifact.hash_to_file_name(spec))
end

#remoteObject

Returns a hash of all the remote repositories. The key is the repository identifier, and the value is the repository base URL.



188
189
190
# File 'lib/core/artifact.rb', line 188

def remote()
  @remote ||= {}
end

#remote=(hash) ⇒ Object

Sets the remote repositories from a hash. See #remote.



193
194
195
196
197
198
199
200
201
202
# File 'lib/core/artifact.rb', line 193

def remote=(hash)
  case hash
  when nil
    @remote = {}
  when Hash
    @remote = hash.clone
  else
    raise ArgumentError, "Expecting a hash" unless Hash === hash
  end
end