Class: Buildr::Ivy::IvyConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/buildr/ivy_extension.rb

Constant Summary collapse

TARGETS =
[:compile, :test, :package]
TYPES =
[:conf, :include, :exclude]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ IvyConfig

Store the current project and initialize ivy ant wrapper



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/buildr/ivy_extension.rb', line 38

def initialize(project)
  @project = project
  if project.parent.nil?
    @extension_dir = @project.base_dir
    @post_resolve_task_list = []
  else
    @extension_dir = @project.parent.ivy.extension_dir
    @base_ivy = @project.parent.ivy unless own_file?
  end
  @target_config = Hash.new do
    |hash, key| hash[key] = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args, &block) ⇒ Object

:call-seq: for types:

project.ivy.include(:compile => [/\.jar/, /\.gz/], :package => 'cglib.jar')
project.ivy.exclude(:test => 'cglib.jar')
project.ivy.conf(:compile => 'compile', :test => 'test', :package => 'prod')

for targets:

project.ivy.compile(:conf => 'compile', :exclude => /cglib.jar/)
project.ivy.test(:conf => 'test')
project.ivy.package(:conf => 'prod', :include => /.*.jar/, :exclude => /cglib.jar/)

or verbose:

project.ivy.compile_conf or project.ivy.conf_compile
project.ivy.compile_include or project.ivy.include_compile

the same for the other possible options.

Uses #method_missing to handle the options. Generic handling of settings for target and type. All calls in the form target_type({}) or type_target({}) are handled via this method see #TARGETS #TYPES for more information about valid targets and types.



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/buildr/ivy_extension.rb', line 382

def method_missing(methodname, *args, &block)
  if block.nil? && valid_config_call?(methodname)
    target, type = target(methodname), type(methodname)
    if target && type
      handle_variable(target, type, *args)
    elsif target && args.size == 1 && args.last.kind_of?(Hash)
      args[0].each { |type, value| handle_variable(target, type, *value) }
      self
    elsif type && args.size == 1 && args.last.kind_of?(Hash)
      args[0].each { |target, value| handle_variable(target, type, *value) }
      self
    else
      raise "Could not recognize config call for method '#{methodname}', args=#{args.inspect}"
    end
  else
    super.method_missing(methodname, *args, &block)
  end
end

Instance Attribute Details

#extension_dirObject

Returns the value of attribute extension_dir.



33
34
35
# File 'lib/buildr/ivy_extension.rb', line 33

def extension_dir
  @extension_dir
end

#post_resolve_task_listObject (readonly)

Returns the value of attribute post_resolve_task_list.



35
36
37
# File 'lib/buildr/ivy_extension.rb', line 35

def post_resolve_task_list
  @post_resolve_task_list
end

#resolvedObject

Returns the value of attribute resolved.



33
34
35
# File 'lib/buildr/ivy_extension.rb', line 33

def resolved
  @resolved
end

Instance Method Details

#__publish__Object

Publishs the project as defined in ivy file if it has not been published already



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/buildr/ivy_extension.rb', line 149

def __publish__
  if @base_ivy
    @base_ivy.__publish__
  else
    unless @published
      options = {:status => status, :pubrevision => revision, :artifactspattern => "#{publish_from}/[artifact].[ext]"}
      options = publish_options * options
      ant.publish options
      @published = true
    end
  end
end

#__resolve__Object

Resolves the configured file once.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/buildr/ivy_extension.rb', line 118

def __resolve__
  if @base_ivy
    @base_ivy.__resolve__
  else
    unless @resolved
      @resolved = ant.resolve :file => file
      @project.send(:info, "Calling '#{post_resolve_tasks.size}' post_resolve tasks for '#{@project.name}'")
      post_resolve_tasks.each { |p| p.call(self) }
    end
  end
end

#antObject

Returns the correct ant instance to use, if project has its own ivy file uses the ivy file of project, if not uses the ivy file of parent project.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/buildr/ivy_extension.rb', line 62

def ant
  unless @ant
    if own_file?
      @ant = ::Ivy4r.new(@project.ant('ivy'))
      @ant.lib_dir = lib_dir if lib_dir
      @ant.project_dir = @extension_dir
    else
      @ant = @project.parent.ivy.ant
    end
  end
  @ant
end

#configureObject

Configures the ivy instance with additional properties and loading the settings file if it was provided



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/buildr/ivy_extension.rb', line 104

def configure
  if @base_ivy
    @base_ivy.configure
  else
    unless @configured
      ant.property['ivy.status'] = status
      ant.property['ivy.home'] = home
      properties.each {|key, value| ant.property[key.to_s] = value }
      @configured = ant.settings :file => settings if settings
    end
  end
end

#deps(*confs) ⇒ Object

Returns the artifacts for given configurations as array this is a post resolve task.



82
83
84
85
86
87
88
89
# File 'lib/buildr/ivy_extension.rb', line 82

def deps(*confs)
  configure
  confs = confs.reject {|c| c.nil? || c.blank? }
  unless confs.empty?
    pathid = "ivy.deps." + confs.join('.')
    ant.cachepath :conf => confs.join(','), :pathid => pathid
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/buildr/ivy_extension.rb', line 52

def enabled?
  @enabled ||= Ivy.setting('enabled') || true
end

#fileObject



174
175
176
# File 'lib/buildr/ivy_extension.rb', line 174

def file
  @ivy_file ||= Ivy.setting('ivy.file') || 'ivy.xml'
end

#file_projectObject

Returns name of the project the ivy file belongs to.



76
77
78
# File 'lib/buildr/ivy_extension.rb', line 76

def file_project
  own_file? ? @project : @base_ivy.file_project
end

#filter(*confs) ⇒ Object

Filter artifacts for given configuration with provided filter values, this is a post resolve task like #deps. project.ivy.filter('server', 'client', :include => /b.*.jar/, :exclude => [/a\.jar/, /other.*\.jar/])



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/buildr/ivy_extension.rb', line 344

def filter(*confs)
  filter = confs.last.kind_of?(Hash) ? confs.pop : {}
  unless (filter.keys - [:include, :exclude]).empty?
    raise ArgumentError, "Invalid filter use :include and/or :exclude only: given #{filter.keys.inspect}"
  end
  includes, excludes = filter[:include] || [], filter[:exclude] || []

  artifacts = deps(*confs.flatten)
  if artifacts
    artifacts = artifacts.find_all do |lib|
      lib = File.basename(lib)
      includes = includes.reject {|i| i.nil? || i.blank? }
      should_include = includes.empty? || includes.any? {|include| include === lib }
      should_include && !excludes.any? {|exclude| exclude === lib}
    end
  end

  artifacts
end

#homeObject



162
163
164
# File 'lib/buildr/ivy_extension.rb', line 162

def home
  @ivy_home_dir ||= Ivy.setting('home.dir') || "#{@extension_dir}/ivy-home"
end

#infoObject

Returns ivy info for configured ivy file using a new ant instance.



92
93
94
95
96
97
98
99
100
101
# File 'lib/buildr/ivy_extension.rb', line 92

def info
  if @base_ivy
    @base_ivy.info
  else
    ant.settings :id => 'ivy.info.settingsref'
    result = ant.info :file => file, :settingsRef => 'ivy.info.settingsref'
    @ant = nil
    result
  end
end

#lib_dirObject



166
167
168
# File 'lib/buildr/ivy_extension.rb', line 166

def lib_dir
  @lib_dir ||= Ivy.setting('lib.dir')
end

#local_repository(*local_repository) ⇒ Object

Sets the local repository for ivy files



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/buildr/ivy_extension.rb', line 268

def local_repository(*local_repository)
  if local_repository.empty?
    if own_file?
      @local_repository ||= Ivy.setting('local.repository.dir') || "#{home}/repository"
    else
      @project.parent.ivy.local_repository
    end
  else
    raise "Could not set 'local_repository' for '#{@project.name}' without own ivy file!" unless own_file?
    raise "local_repository value invalid #{local_repository.join(', ')}" unless local_repository.size == 1
    @local_repository = local_repository[0]
    self
  end
end

#manifestObject

Returns the additional infos for the manifest file.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/buildr/ivy_extension.rb', line 131

def manifest
  if @base_ivy
    @base_ivy.manifest
  else
    {
      'organisation' => @resolved['ivy.organisation'],
      'module' => @resolved['ivy.organisation'],
      'revision' => revision
    }
  end
end

#name(*name_mappings) ⇒ Object Also known as: publish

:call-seq: ivy.publish(package(:jar) => ‘new_name_without_version_number.jar’) #deprecated! ivy.name(package(:jar) => ‘new_name_without_version_number.jar’)

Maps a package to a different name for publishing. This name is used instead of the default name for publishing use a hash with the package as key and the newly mapped name as value. I.e. ivy.name(package(:jar) => 'new_name_without_version_number.jar') Note that this method is additive, a second call adds the names to the first.



291
292
293
294
295
296
297
298
299
# File 'lib/buildr/ivy_extension.rb', line 291

def name(*name_mappings)
  if name_mappings.empty?
    @name_mappings ||= {}
  else
    raise "name_mappings value invalid #{name_mappings.join(', ')}" unless name_mappings.size == 1
    @name_mappings = @name_mappings ? @name_mappings + name_mappings[0] : name_mappings[0].dup
    self
  end
end

#own_file?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/buildr/ivy_extension.rb', line 56

def own_file?
  @own_file ||= File.exists?(@project.path_to(file))
end

#post_resolve(&block) ⇒ Object

Adds given block as post resolve action that is executed directly after #resolve has been called. Yields this ivy config object into block. project.ivy.post_resolve { |ivy| p "all deps:" + ivy.deps('all').join(", ") }



337
338
339
# File 'lib/buildr/ivy_extension.rb', line 337

def post_resolve(&block)
  post_resolve_tasks << block if block
end

#properties(*properties) ⇒ Object

Sets the additional properties for the ivy process use a Hash with the properties to set.



257
258
259
260
261
262
263
264
265
# File 'lib/buildr/ivy_extension.rb', line 257

def properties(*properties)
  if properties.empty?
    @properties ||= {}
  else
    raise "properties value invalid #{properties.join(', ')}" unless properties.size == 1
    @properties = properties[0]
    self
  end
end

#publish_from(*publish_dir) ⇒ Object

Sets the directory to publish artifacts from.



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/buildr/ivy_extension.rb', line 303

def publish_from(*publish_dir)
  if publish_dir.empty?
    if own_file?
      @publish_from ||= Ivy.setting('publish.from') || @project.path_to(:target)
    else
      @project.parent.ivy.publish_from
    end
  else
    raise "Could not set 'publish_from' for '#{@project.name}' without own ivy file!" unless own_file?
    raise "publish_from value invalid #{publish_dir.join(', ')}" unless publish_dir.size == 1
    @publish_from = publish_dir[0]
    self
  end
end

#publish_options(*options, &block) ⇒ Object

Sets the publish options to use for the project. The options are merged with the default options including value set via #publish_from and overwrite all of them.

To set the options this method can be used in different ways.

  1. project.ivy.publish_options(options) to set the options directly

  2. project.ivy.publish_options { |ivy| [calculate options] } use the block for dynamic calculation of options. You can access ivy4r via ivy.ant.[method]



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/buildr/ivy_extension.rb', line 235

def publish_options(*options, &block)
  raise "Invalid call with parameters and block!" if options.size > 0 && block
  if options.empty? && block.nil?
    if @publish_options_calc
      @publish_options ||= @publish_options_calc.call(self)
    else
      @publish_options ||= Ivy.setting('publish.options')
    end
  else
    raise "Could not set 'publish_options' for '#{@project.name}' without own ivy file!" unless own_file?
    if options.size > 0 && block.nil?
      raise "publish options value invalid #{options.join(', ')}" unless options.size == 1
      @publish_options = options[0]
      self
    else
      @publish_options_calc = block
      self
    end
  end
end

#reportObject

Creates the standard ivy dependency report



144
145
146
# File 'lib/buildr/ivy_extension.rb', line 144

def report
  ant.report :todir => report_dir
end

#report_dir(*report_dir) ⇒ Object

Sets the directory to create dependency reports in.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/buildr/ivy_extension.rb', line 319

def report_dir(*report_dir)
  if report_dir.empty?
    if own_file?
      @report_dir ||= Ivy.setting('report.dir') || @project.path_to(:reports, 'ivy')
    else
      @project.parent.ivy.report_dir
    end
  else
    raise "Could not set 'report_dir' for '#{@project.name}' without own ivy file!" unless own_file?
    raise "publish_from value invalid #{report_dir.join(', ')}" unless report_dir.size == 1
    @report_dir = report_dir[0]
    self
  end
end

#revision(*revision, &block) ⇒ Object

Sets the revision to use for the project, this is useful for development revisions that have an appended timestamp or any other dynamic revisioning.

To set a different revision this method can be used in different ways.

  1. project.ivy.revision(revision) to set the revision directly

  2. project.ivy.revision { |ivy| [calculate revision] } use the block for dynamic calculation of the revision. You can access ivy4r via ivy.ant.[method]



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

def revision(*revision, &block)
  raise "Invalid call with parameters and block!" if revision.size > 0 && block
  if revision.empty? && block.nil?
    if @revision_calc
      @revision ||= @revision_calc.call(self)
    else
      @revision ||= @project.version
    end
  elsif block.nil?
    raise "revision value invalid #{revision.join(', ')}" unless revision.size == 1
    @revision = revision[0]
    self
  else
    @revision_calc = block
    self
  end
end

#settingsObject



170
171
172
# File 'lib/buildr/ivy_extension.rb', line 170

def settings
  @settings ||= Ivy.setting('settings.file') || "#{@extension_dir}/ant-scripts/ivysettings.xml"
end

#status(*status, &block) ⇒ Object

Sets the status to use for the project, this is useful for custom status handling like integration, alpha, gold.

To set a different status this method can be used in different ways.

  1. project.ivy.status(status) to set the status directly

  2. project.ivy.status { |ivy| [calculate status] } use the block for dynamic calculation of the status. You can access ivy4r via ivy.ant.[method]



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/buildr/ivy_extension.rb', line 210

def status(*status, &block)
  raise "Invalid call with parameters and block!" if status.size > 0 && block
  if status.empty? && block.nil?
    if @status_calc
      @status ||= @status_calc.call(self)
    else
      @status ||= Ivy.setting('status') || 'integration'
    end
  elsif status.empty? && block.nil?
    raise "status value invalid #{status.join(', ')}" unless status.size == 1
    @status = status[0]
    self
  else
    @status_calc = block
    self
  end
end