module Buildr::ActsAsArtifact

Mixin with a task to make it behave like an artifact. Implemented by the packaging tasks.

An artifact has an identifier, group identifier, type, version number and optional classifier. All can be used to locate it in the local repository, download from or upload to a remote repository.

The to_spec and to_hash methods allow it to be used everywhere an artifact is accepted.

Constants

ARTIFACT_ATTRIBUTES

Attributes

classifier[R]

Optional artifact classifier.

group[R]

The group identifier.

id[R]

The artifact identifier.

type[R]

The file type. (Symbol)

version[R]

The version number.

Public Instance Methods

install() click to toggle source
# File lib/buildr/packaging/artifact.rb, line 155
def install
  invoke
  in_local_repository = Buildr.repositories.locate(self)
  if pom && pom != self && classifier.nil?
    pom.invoke
    pom.install
  end
  if name != in_local_repository
    mkpath File.dirname(in_local_repository)
    cp name, in_local_repository, :preserve => false
    info "Installed #{name} to #{in_local_repository}"
  end
end
javadoc_artifact → Artifact click to toggle source

Convenience method that returns the associated javadoc artifact

# File lib/buildr/packaging/artifact.rb, line 130
def javadoc_artifact
  javadoc_spec = to_spec_hash.merge(:classifier=>'javadoc')
  javadoc_task = OptionalArtifact.define_task(Buildr.repositories.locate(javadoc_spec))
  javadoc_task.send :apply_spec, javadoc_spec
  javadoc_task
end
pom → Artifact click to toggle source

Convenience method that returns a POM artifact.

# File lib/buildr/packaging/artifact.rb, line 110
def pom
  return self if type == :pom
  Buildr.artifact(:group=>group, :id=>id, :version=>version, :type=>:pom)
end
pom_xml → string click to toggle source

Creates POM XML for this artifact.

# File lib/buildr/packaging/artifact.rb, line 141
def pom_xml
  Proc.new do
    xml = Builder::XmlMarkup.new(:indent=>2)
    xml.instruct!
    xml.project do
      xml.modelVersion  '4.0.0'
      xml.groupId       group
      xml.artifactId    id
      xml.version       version
      xml.classifier    classifier if classifier
    end
  end
end
snapshot?() click to toggle source
# File lib/buildr/packaging/artifact.rb, line 75
def snapshot?
  version =~ /-SNAPSHOT$/
end
sources_artifact → Artifact click to toggle source

Convenience method that returns a sources artifact.

# File lib/buildr/packaging/artifact.rb, line 119
def sources_artifact
  sources_spec = to_spec_hash.merge(:classifier=>'sources')
  sources_task = OptionalArtifact.define_task(Buildr.repositories.locate(sources_spec))
  sources_task.send :apply_spec, sources_spec
  sources_task
end
to_hash()
Alias for: to_spec_hash
to_spec → String click to toggle source

Returns the artifact specification, in the structure:

<group>:<artifact>:<type>:<version>

or

<group>:<artifact>:<type>:<classifier>:<version>
# File lib/buildr/packaging/artifact.rb, line 102
def to_spec
  classifier ? "#{group}:#{id}:#{type}:#{classifier}:#{version}" : "#{group}:#{id}:#{type}:#{version}"
end
to_spec_hash → Hash click to toggle source

Returns the artifact specification as a hash. For example:

com.example:app:jar:1.2

becomes:

{ :group=>'com.example',
  :id=>'app',
  :type=>:jar,
  :version=>'1.2' }
# File lib/buildr/packaging/artifact.rb, line 89
def to_spec_hash
  base = { :group=>group, :id=>id, :type=>type, :version=>version }
  classifier ? base.merge(:classifier=>classifier) : base
end
Also aliased as: to_hash
uninstall() click to toggle source
# File lib/buildr/packaging/artifact.rb, line 169
def uninstall
  installed = Buildr.repositories.locate(self)
  rm installed if File.exist?(installed)
  pom.uninstall if pom && pom != self && classifier.nil?
end
upload click to toggle source
upload(url)
upload(options)

Uploads the artifact, its POM and digital signatures to remote server.

In the first form, uses the upload options specified by repositories.release_to. In the second form, uses a URL that includes all the relevant information. In the third form, uses a hash with the options :url, :username, :password, and :permissions. All but :url are optional.

# File lib/buildr/packaging/artifact.rb, line 186
def upload(upload_to = nil)
  upload_task(upload_to).invoke
end
upload_task(upload_to = nil) click to toggle source
# File lib/buildr/packaging/artifact.rb, line 190
def upload_task(upload_to = nil)
  upload_to ||= Buildr.repositories.release_to
  upload_to = { :url=>upload_to } unless Hash === upload_to
  raise ArgumentError, 'Don\t know where to upload, perhaps you forgot to set repositories.release_to' unless upload_to[:url]

  # Set the upload URI, including mandatory slash (we expect it to be the base directory).
  # Username/password may be part of URI, or separate entities.
  uri = URI.parse(upload_to[:url].clone)
  uri.path = uri.path + '/' unless uri.path[-1] == '/'
  uri.user = upload_to[:username] if upload_to[:username]
  uri.password = upload_to[:password] if upload_to[:password]

  path = group.gsub('.', '/') + "/#{id}/#{version}/#{File.basename(name)}"

  unless task = Buildr.application.lookup(uri+path)
    deps = [self]
    deps << pom.upload_task( upload_to ) if pom && pom != self && classifier.nil?

    task = Rake::Task.define_task uri + path => deps do
      # Upload artifact relative to base URL, need to create path before uploading.
      options = upload_to[:options] || {:permissions => upload_to[:permissions]}
      info "Deploying #{to_spec}"
      URI.upload uri + path, name, options
    end
  end
  task
end

Protected Instance Methods

apply_spec(spec) click to toggle source

Apply specification to this artifact.

# File lib/buildr/packaging/artifact.rb, line 221
def apply_spec(spec)
  spec = Artifact.to_hash(spec)
  ARTIFACT_ATTRIBUTES.each { |key| instance_variable_set("@#{key}", spec[key]) }
  self
end
group_path() click to toggle source
# File lib/buildr/packaging/artifact.rb, line 227
def group_path
  group.gsub('.', '/')
end