Class: Buildr::Artifact

Inherits:
Rake::FileCreationTask
  • Object
show all
Includes:
ActsAsArtifact
Defined in:
lib/buildr/packaging/artifact.rb

Overview

A file task referencing an artifact in the local repository.

This task includes all the artifact attributes (group, id, version, etc). It points to the artifact’s path in the local repository. When invoked, it will download the artifact into the local repository if the artifact does not already exist.

Note: You can enhance this task to create the artifact yourself, e.g. download it from a site that doesn’t have a remote repository structure, copy it from a different disk, etc.

Direct Known Subclasses

OptionalArtifact

Constant Summary collapse

DEFAULT_TYPE =

The default artifact type.

:jar

Constants included from ActsAsArtifact

Buildr::ActsAsArtifact::ARTIFACT_ATTRIBUTES

Instance Attribute Summary

Attributes included from ActsAsArtifact

#classifier, #group, #id, #type, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActsAsArtifact

#install, #pom, #pom_xml, #snapshot?, #sources_artifact, #to_spec, #to_spec_hash, #uninstall, #upload

Constructor Details

#initialize(*args) ⇒ Artifact

:nodoc:



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/buildr/packaging/artifact.rb', line 305

def initialize(*args) #:nodoc:
  super
  enhance do |task|
    # Default behavior: download the artifact from one of the remote repositories
    # if the file does not exist. But this default behavior is counter productive
    # if the artifact knows how to build itself (e.g. download from a different location),
    # so don't perform it if the task found a different way to create the artifact.
    task.enhance do
      unless File.exist?(name)
        info "Downloading #{to_spec}"
        download
        pom.invoke rescue nil if pom && pom != self
      end
    end
  end
end

Class Method Details

.hash_to_file_name(hash) ⇒ Object

:call-seq:

hash_to_file_name(spec_hash) => file_name

Convert a hash spec to a file name.



297
298
299
300
301
# File 'lib/buildr/packaging/artifact.rb', line 297

def hash_to_file_name(hash)
  version = "-#{hash[:version]}" if hash[:version]
  classifier = "-#{hash[:classifier]}" if hash[:classifier]
  "#{hash[:id]}#{version}#{classifier}.#{hash[:type] || DEFAULT_TYPE}"
end

.listObject

:call-seq:

list => specs

Returns an array of specs for all the registered artifacts. (Anything created from artifact, or package).



228
229
230
231
# File 'lib/buildr/packaging/artifact.rb', line 228

def list
  @artifacts ||= {}
  @artifacts.keys
end

.lookup(spec) ⇒ Object

:call-seq:

lookup(spec) => Artifact

Lookup a previously registered artifact task based on its specification (String or Hash).



219
220
221
222
# File 'lib/buildr/packaging/artifact.rb', line 219

def lookup(spec)
  @artifacts ||= {}
  @artifacts[to_spec(spec)]
end

.register(*tasks) ⇒ Object

:call-seq:

register(artifacts) => artifacts

Register an artifact task(s) for later lookup (see #lookup).



237
238
239
240
241
242
243
# File 'lib/buildr/packaging/artifact.rb', line 237

def register(*tasks)
  @artifacts ||= {}
  fail 'You can only register an artifact task, one of the arguments is not a Task that responds to to_spec' unless
    tasks.all? { |task| task.respond_to?(:to_spec) && task.respond_to?(:invoke) }
  tasks.each { |task| @artifacts[task.to_spec] = task }
  tasks
end

.to_hash(spec) ⇒ Object

:call-seq:

to_hash(spec_hash) => spec_hash
to_hash(spec_string) => spec_hash
to_hash(artifact) => spec_hash

Turn a spec into a hash. This method accepts a String, Hash or any object that responds to the method to_spec. There are several reasons to use this method:

  • You can pass anything that could possibly be a spec, and get a hash.

  • It will check that the spec includes the group identifier, artifact identifier and version number and set the file type, if missing.

  • It will always return a new specs hash.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/buildr/packaging/artifact.rb', line 256

def to_hash(spec)
  if spec.respond_to?(:to_spec)
    to_hash spec.to_spec
  elsif Hash === spec
    rake_check_options spec, :id, :group, :type, :classifier, :version
    # Sanitize the hash and check it's valid.
    spec = ARTIFACT_ATTRIBUTES.inject({}) { |h, k| h[k] = spec[k].to_s if spec[k] ; h }
    fail "Missing group identifier for #{spec.inspect}" unless spec[:group]
    fail "Missing artifact identifier for #{spec.inspect}" unless spec[:id]
    fail "Missing version for #{spec.inspect}" unless spec[:version]
    spec[:type] = (spec[:type] || DEFAULT_TYPE).to_sym
    spec
  elsif String === spec
    group, id, type, version, *rest = spec.split(':').map { |part| part.empty? ? nil : part }
    unless rest.empty?
      # Optional classifier comes before version.
      classifier, version = version, rest.shift
      fail "Expecting <group:id:type:version> or <group:id:type:classifier:version>, found <#{spec}>" unless rest.empty?
    end
    to_hash :group=>group, :id=>id, :type=>type, :version=>version, :classifier=>classifier
  else
    fail 'Expecting a String, Hash or object that responds to to_spec'
  end
end

.to_spec(hash) ⇒ Object

:call-seq:

to_spec(spec_hash) => spec_string

Convert a hash back to a spec string. This method accepts a string, hash or any object that responds to to_spec.



286
287
288
289
290
291
# File 'lib/buildr/packaging/artifact.rb', line 286

def to_spec(hash)
  hash = to_hash(hash) unless Hash === hash
  version = ":#{hash[:version]}" if hash[:version]
  classifier = ":#{hash[:classifier]}" if hash[:classifier]
  "#{hash[:group]}:#{hash[:id]}:#{hash[:type] || DEFAULT_TYPE}#{classifier}#{version}"
end

Instance Method Details

#from(path) ⇒ Object

:call-seq:

from(path) => self

Use this when you want to install or upload an artifact from a given file, for example:

test = artifact('group:id:jar:1.0').from('test.jar')
install test

See also Buildr#install and Buildr#upload.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/buildr/packaging/artifact.rb', line 329

def from(path)
  path = File.expand_path(path.to_s)
  enhance [path] do
    verbose false do
      mkpath File.dirname(name)
      pom.invoke unless type == :pom
      cp path, name
      info "Installed #{path} as #{to_spec}"
    end
  end
  unless type == :pom
    pom.enhance do
      verbose false do
        mkpath File.dirname(pom.name)
        File.open(pom.name, 'w') { |file| file.write pom.pom_xml }
      end
    end
  end
  self
end