Class: Buildr::Artifact
- Inherits:
-
Rake::FileTask
- Object
- Rake::FileTask
- Buildr::Artifact
- Includes:
- Buildr, 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
Constant Summary collapse
- DEFAULT_TYPE =
The default artifact type.
:jar
Constants included from ActsAsArtifact
Buildr::ActsAsArtifact::ARTIFACT_ATTRIBUTES
Constants included from Buildr
ScalaCheck, ScalaSpecs, ScalaTest, VERSION
Constants included from Ant
Instance Attribute Summary
Attributes included from ActsAsArtifact
#classifier, #group, #id, #type, #version
Class Method Summary collapse
-
.hash_to_file_name(hash) ⇒ Object
:call-seq: hash_to_file_name(spec_hash) => file_name.
-
.list ⇒ Object
:call-seq: list => specs.
-
.lookup(spec) ⇒ Object
:call-seq: lookup(spec) => Artifact.
-
.register(*tasks) ⇒ Object
:call-seq: register(artifacts) => artifacts.
-
.to_hash(spec) ⇒ Object
:call-seq: to_hash(spec_hash) => spec_hash to_hash(spec_string) => spec_hash to_hash(artifact) => spec_hash.
-
.to_spec(hash) ⇒ Object
:call-seq: to_spec(spec_hash) => spec_string.
Instance Method Summary collapse
-
#content(string = nil) ⇒ Object
:call-seq: content(string) => self.
-
#from(path) ⇒ Object
:call-seq: from(path) => self.
-
#initialize(*args) ⇒ Artifact
constructor
:nodoc:.
Methods included from ActsAsArtifact
#install, #javadoc_artifact, #pom, #pom_xml, #snapshot?, #sources_artifact, #to_spec, #to_spec_hash, #uninstall, #upload, #upload_task
Methods included from Buildr
application, application=, #artifact, #artifact_ns, #artifacts, #concat, #define, environment, #filter, #group, #help, help, #install, #integration, options, #options, #project, #projects, #read, #repositories, settings, #struct, #transitive, #unzip, #upload, #write, #zip
Methods included from Ant
Methods inherited from Rake::FileTask
Constructor Details
#initialize(*args) ⇒ Artifact
:nodoc:
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/buildr/packaging/artifact.rb', line 346 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 if download_needed? task info "Downloading #{to_spec}" download pom.invoke rescue nil if pom && pom != self && classifier.nil? 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.
338 339 340 341 342 |
# File 'lib/buildr/packaging/artifact.rb', line 338 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 |
.list ⇒ Object
:call-seq:
list => specs
Returns an array of specs for all the registered artifacts. (Anything created from artifact, or package).
269 270 271 272 |
# File 'lib/buildr/packaging/artifact.rb', line 269 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).
260 261 262 263 |
# File 'lib/buildr/packaging/artifact.rb', line 260 def lookup(spec) @artifacts ||= {} @artifacts[to_spec(spec)] end |
.register(*tasks) ⇒ Object
278 279 280 281 282 283 284 |
# File 'lib/buildr/packaging/artifact.rb', line 278 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.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/buildr/packaging/artifact.rb', line 297 def to_hash(spec) if spec.respond_to?(:to_spec) to_hash spec.to_spec elsif Hash === spec 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.
327 328 329 330 331 332 |
# File 'lib/buildr/packaging/artifact.rb', line 327 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
#content(string = nil) ⇒ Object
:call-seq:
content(string) => self
Use this when you want to install or upload an artifact from a given content, for example:
readme = artifact('com.example:readme:txt:1.0').content(<<-EOF
Please visit our website at http://example.com/readme
<<EOF
install readme
If the argument is not a string, it will be converted to a string using to_s
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/buildr/packaging/artifact.rb', line 390 def content(string = nil) return @content unless string unless @content enhance do write name, @content end class << self # Force overwriting target since we don't have source file # to check for timestamp modification def needed? true end end end @content = string pom.content pom_xml unless pom == self || pom.has_content? self end |
#from(path) ⇒ Object
370 371 372 373 374 375 376 377 378 |
# File 'lib/buildr/packaging/artifact.rb', line 370 def from(path) @from = path.is_a?(Rake::Task) ? path : File.(path.to_s) enhance [@from] do mkpath File.dirname(name) cp @from.to_s, name end pom.content pom_xml unless pom == self || pom.has_content? self end |