Module: Buildr::ActsAsArtifact

Included in:
Artifact
Defined in:
lib/buildr/packaging/artifact.rb

Overview

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.

Constant Summary collapse

ARTIFACT_ATTRIBUTES =
[:group, :id, :type, :classifier, :version]
MAVEN_METADATA =
'maven-metadata.xml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#buildr_projectObject

Returns the value of attribute buildr_project.



79
80
81
# File 'lib/buildr/packaging/artifact.rb', line 79

def buildr_project
  @buildr_project
end

#classifierObject (readonly)

Optional artifact classifier.



77
78
79
# File 'lib/buildr/packaging/artifact.rb', line 77

def classifier
  @classifier
end

#groupObject (readonly)

The group identifier.



71
72
73
# File 'lib/buildr/packaging/artifact.rb', line 71

def group
  @group
end

#idObject (readonly)

The artifact identifier.



69
70
71
# File 'lib/buildr/packaging/artifact.rb', line 69

def id
  @id
end

#typeObject (readonly)

The file type. (Symbol)



73
74
75
# File 'lib/buildr/packaging/artifact.rb', line 73

def type
  @type
end

#versionObject (readonly)

The version number.



75
76
77
# File 'lib/buildr/packaging/artifact.rb', line 75

def version
  @version
end

Instance Method Details

#annotations_artifactObject

:call-seq:

annotations_artifact => Artifact

Convenience method that returns an annotations artifact. The annotations artifact is used by Intellij IDEA as a source of external annotations.



153
154
155
156
157
158
# File 'lib/buildr/packaging/artifact.rb', line 153

def annotations_artifact
  annotations_spec = to_spec_hash.merge(:classifier=>'annotations')
  annotations_task = OptionalArtifact.define_task(Buildr.repositories.locate(annotations_spec))
  annotations_task.send :apply_spec, annotations_spec
  annotations_task
end

#final_versionObject



85
86
87
88
# File 'lib/buildr/packaging/artifact.rb', line 85

def final_version
  return version unless snapshot?
  Time.now.strftime("%Y%m%d.%H%M%S")
end

#installObject



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/buildr/packaging/artifact.rb', line 203

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_artifactObject

:call-seq:

javadoc_artifact => Artifact

Convenience method that returns the associated javadoc artifact



141
142
143
144
145
146
# File 'lib/buildr/packaging/artifact.rb', line 141

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

#maven_metadata_xmlObject

:call-seq:

 => string

Creates Maven Metadata XML content for this artifact.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/buildr/packaging/artifact.rb', line 186

def 
  xml = Builder::XmlMarkup.new(:indent=>2)
  xml.instruct!
  xml. do
    xml.groupId       group
    xml.artifactId    id
    xml.version       version
    xml.versioning do
      xml.snapshot do
        xml.timestamp final_version
        xml.buildNumber 1
      end
      xml.lastupdated Time.now.strftime("%Y%m%d%H%M%S")
    end
  end
end

#pomObject

:call-seq:

pom => Artifact

Convenience method that returns a POM artifact.



121
122
123
124
# File 'lib/buildr/packaging/artifact.rb', line 121

def pom
  return self if type == :pom
  Buildr.artifact(:group=>group, :id=>id, :version=>version, :type=>:pom)
end

#pom_xmlObject

:call-seq:

pom_xml => string

Creates POM XML for this artifact.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/buildr/packaging/artifact.rb', line 164

def pom_xml
  if self.buildr_project
    Buildr::CustomPom.pom_xml(self.buildr_project, self)
  else
    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
end

#snapshot?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/buildr/packaging/artifact.rb', line 81

def snapshot?
  version =~ /-SNAPSHOT$/
end

#sources_artifactObject

:call-seq:

sources_artifact => Artifact

Convenience method that returns a sources artifact.



130
131
132
133
134
135
# File 'lib/buildr/packaging/artifact.rb', line 130

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_specObject

:call-seq:

to_spec => String

Returns the artifact specification, in the structure:

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

or

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


113
114
115
# File 'lib/buildr/packaging/artifact.rb', line 113

def to_spec
  classifier ? "#{group}:#{id}:#{type}:#{classifier}:#{version}" : "#{group}:#{id}:#{type}:#{version}"
end

#to_spec_hashObject Also known as: to_hash

:call-seq:

to_spec_hash => Hash

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' }


100
101
102
103
# File 'lib/buildr/packaging/artifact.rb', line 100

def to_spec_hash
  base = { :group=>group, :id=>id, :type=>type, :version=>version }
  classifier ? base.merge(:classifier=>classifier) : base
end

#uninstallObject



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

def uninstall
  installed = Buildr.repositories.locate(self)
  rm installed if File.exist?(installed)
  pom.uninstall if pom && pom != self && classifier.nil?
end

#upload(upload_to = nil) ⇒ Object

:call-seq:

upload
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 or repositories.snapshot_to if the subject is a snapshot artifact. 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.



235
236
237
# File 'lib/buildr/packaging/artifact.rb', line 235

def upload(upload_to = nil)
  upload_task(upload_to).invoke
end

#upload_task(upload_to = nil) ⇒ Object

Raises:

  • (ArgumentError)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/buildr/packaging/artifact.rb', line 239

def upload_task(upload_to = nil)
  upload_to ||= Buildr.repositories.snapshot_to if snapshot? && Buildr.repositories.snapshot_to != nil && Buildr.repositories.snapshot_to[:url] != 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] == '/'
  to_escape = "!\"\#$%&'()*+,-./:;<=>?@{}|~`'"
  uri.user = URI.encode(upload_to[:username], to_escape) if upload_to[:username]
  uri.password = URI.encode(upload_to[:password], to_escape) if upload_to[:password]

  path = group.gsub('.', '/') + "/#{id}/#{version}/#{upload_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
      if snapshot? && pom != self
          = group.gsub('.', '/') + "/#{id}/#{version}/#{MAVEN_METADATA}"
         URI.write uri + , , :permissions => upload_to[:permissions]
      end
    end
  end
  task
end