Class: Maven::Tools::GemProject

Inherits:
Model::Project show all
Defined in:
lib/maven/tools/gem_project.rb

Direct Known Subclasses

RailsProject

Instance Method Summary collapse

Methods inherited from Model::Project

#_name, #build, #dependency_management, #description, #description=, #developers, #execute_in_phase, #executions_in_phase, #licenses, #name, #name=, #parent, #plugin, #plugin?, #plugin_repositories, #plugin_repository, #profile, #profiles, #properties, #properties=, #repositories, #repository, #source_control, #version

Methods included from Model::Dependencies

#dependencies, #detect_gem, #gem?, included, #jar, #jar?, #pom, #pom?, #test_jar, #test_jar?

Methods inherited from Model::Coordinate

#==, #hash, #version?

Methods included from Coordinate

#gav, #group_artifact, #to_coordinate, #to_version

Methods inherited from Model::Tag

#_name, _tags, #comment, prepend_tags, tags, #to_xml

Constructor Details

#initialize(artifact_id = dir_name, version = "0.0.0", &block) ⇒ GemProject

Returns a new instance of GemProject.



47
48
49
50
# File 'lib/maven/tools/gem_project.rb', line 47

def initialize(artifact_id = dir_name, version = "0.0.0", &block)
  super("rubygems", artifact_id, version, &block)
  packaging "gem"
end

Instance Method Details

#add_defaults(args = {}) ⇒ Object



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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/maven/tools/gem_project.rb', line 239

def add_defaults(args = {})
  versions = VERSIONS
  versions = versions.merge(args) if args

  name "#{dir_name} - gem" unless name
  
  packaging "gem" unless packaging

  repository("rubygems-releases").url = "http://rubygems-proxy.torquebox.org/releases" unless repository("rubygems-releases").url

  has_prereleases = dependencies.detect { |d| d.type.to_sym == :gem && d.version =~ /[a-zA-Z]/ }
  if has_prereleases && repository("rubygems-prereleases").url.nil?
     repository("rubygems-prereleases") do |r|
       r.url = "http://rubygems-proxy.torquebox.org/prereleases"
  #     r.releases(:enabled => false)
       r.snapshots(:enabled => true)
     end
  end 

  # TODO go through all plugins to find out any SNAPSHOT version !!
  if versions[:jruby_plugins] =~ /-SNAPSHOT$/ || properties['jruby.plugins.version'] =~ /-SNAPSHOT$/
    plugin_repository("sonatype-snapshots") do |nexus|
      nexus.url = "http://oss.sonatype.org/content/repositories/snapshots"
      nexus.releases(:enabled => false)
      nexus.snapshots(:enabled => true)
    end
  end

  if packaging =~ /gem/ || plugin?(:gem)
    gem = plugin(:gem)
    gem.version = "${jruby.plugins.version}" unless gem.version
    if packaging =~ /gem/
      gem.extensions = true
      if @gemspec && !(self.gem?('jruby-openssl') || self.gem?('jruby-openssl-maven'))
        gem.gem('jruby-openssl')
      end
    end
    if File.exists?('lib') && File.exists?(File.join('src', 'main', 'java'))
      plugin(:jar) do |j|
        j.version = versions[:jar_plugin] unless j.version
        j.in_phase('prepare-package').execute_goal(:jar).with :outputDirectory => '${project.basedir}/lib', :finalName => '${project.artifactId}'
      end
    end
  end
  
  if @bundler_deps && @bundler_deps.size > 0
    plugin(:bundler)
    bdeps = []
    # first get the locked gems
    @bundler_deps.each do |args, dep|
      if @lock
        # add its dependencies as well to have the version
        # determine by the dependencyManagement
        @lock.dependency_hull(dep.artifact_id).map.each do |d|
          bdeps << d unless has_gem? d[0]
        end
      end
    end
    # any unlocked gems now
    @bundler_deps.each do |args, dep|
      bdeps << args unless has_gem? args[0]
    end
    
    # now add the deps to bundler plugin
    # avoid to setup bundler if it has no deps
    if bdeps.size > 0 
      plugin(:bundler) do |bundler|
        # install will be triggered on initialize phase
        bundler.execution.goals << "install"

        bdeps.each do |d|
          bundler.gem(d)
        end
      end
    end
  end

  if plugin?(:bundler)
    bundler = plugin(:bundler)
    bundler.version = "${jruby.plugins.version}" unless bundler.version
    unless gem?(:bundler)
      gem("bundler").scope :test
    end
  end

  if gem?('bundler') && !gem('bundler').version?
    gem('bundler').version = nil
    dependency_management.gem 'bundler', versions[:bundler_version]
  end

  if versions[:jruby_plugins]
    #add_test_plugin(nil, "test")
    add_test_plugin("rspec", "spec")
    add_test_plugin("cucumber", "features")
    add_test_plugin("minitest", "test")
    add_test_plugin("minitest", "spec", 'spec')
  end

  self.properties = {
    "project.build.sourceEncoding" => "UTF-8", 
    "gem.home" => "${project.build.directory}/rubygems", 
    "gem.path" => "${project.build.directory}/rubygems",
    "jruby.plugins.version" => versions[:jruby_plugins]
  }.merge(self.properties)

  has_plugin_gems = build.plugins.detect do |k, pl|
    pl.dependencies.detect { |d| d.type.to_sym == :gem } if pl.dependencies
  end
  
  if has_plugin_gems
    plugin_repository("rubygems-releases").url = "http://rubygems-proxy.torquebox.org/releases" unless plugin_repository("rubygems-releases").url
  
    # unless plugin_repository("rubygems-prereleases").url
    #   plugin_repository("rubygems-prereleases") do |r|
    #     r.url = "http://rubygems-proxy.torquebox.org/prereleases"
    #     r.releases(:enabled => false)
    #     r.snapshots(:enabled => true)
    #   end
    # end
  end
  # TODO
  configs = {
    :gem => [:initialize], 
    :rails3 => [:initialize, :pom], 
    :bundler => [:install]
  }.collect do |name, goals|
    if plugin?(name)
      {
        :pluginExecutionFilter => {
          :groupId => 'de.saumya.mojo',
          :artifactId => "#{name}-maven-plugin",
          :versionRange => '[0,)',
          :goals => goals
        },
        :action => { :ignore => nil }
      }
    end
  end
  configs.delete_if { |c| c.nil? }
  if configs.size > 0
    build.plugin_management do |pm|
      options = { 
        :lifecycleMappingMetadata => { 
          :pluginExecutions => Maven::Model::NamedArray.new(:pluginExecution) do |e|
            # sort them - handy for testing
            configs.sort {|m,n| m[:pluginExecutionFilter][:artifactId].to_s <=> n[:pluginExecutionFilter][:artifactId].to_s }.each { |c| e << c }
          end
        }
      }
      pm.plugins.get('org.eclipse.m2e:lifecycle-mapping', '1.0.0').configuration(options)
    end
  end

  if packaging =~ /gem/ || plugin?(:gem)
    profile('executable') do |exe|
      exe.jar('de.saumya.mojo:gem-assembly-descriptors', '${jruby.plugins.version}').scope :runtime
      exe.plugin(:assembly) do |a|
        a.version = versions[:assembly_plugin] unless a.version
        options = {
          :descriptorRefs => ['jar-with-dependencies-and-gems'],
          :archive => {:manifest => { :mainClass => 'de.saumya.mojo.assembly.Main' } }
        }
        a.configuration(options)
        a.in_phase(:package).execute_goal(:assembly)
        a.jar 'de.saumya.mojo:gem-assembly-descriptors', '${jruby.plugins.version}'
      end
    end
  end
end

#gem(*args, &block) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/maven/tools/gem_project.rb', line 468

def gem(*args, &block)
  dep = nil
  if args.last.is_a?(Hash)
    options = args.delete(args.last)
    unless options.key?(:git) || options.key?(:path)
      if (options[:platform].nil? && options[:platforms].nil?) || is_jruby_platform(*(options[:platform] || options[:platforms] || []))
        group = options[:group] || options[:groups]
        if group
          [group].flatten.each do |g|
            if dep
              profile(g).dependencies << dep
            else
              dep = profile(g).gem(args, &block)
            end
          end
        else
          self.gem(args, &block)
        end
      end
    end
  else
    stack.last.each do |c|
      if c == :default
        if @lock.nil? || args[0]== 'bundler'
          dep = add_gem(args, &block)
        else
          dep = add_gem(args[0], &block)

          # add its dependencies as well to have the version
          # determine by the dependencyManagement
          @lock.dependency_hull(args[0]).map.each do |d|
            add_gem d[0], nil
          end
        end
      else
        if @lock.nil?
          if dep
            profile(c).dependencies << dep
          else
            dep = profile(c).gem(args, &block)
          end
        else
          if dep
            profile(c).dependencies << dep
          else
            dep = profile(c).gem(args[0], nil, &block)
          end
          # add its dependencies as well to have the version
          # determine by the dependencyManagement
          @lock.dependency_hull(args[0]).map.each do |d|
            profile(c).gem d[0], nil unless gem? d[0]
          end
        end
      end
    end
  end
  if dep
    # first collect the missing deps it any
    @bundler_deps ||= []
    # use a dep with version so just create it from the args
    @bundler_deps << [args, dep]
  end
  dep
end

#gemspec(name = nil) ⇒ Object



437
438
439
440
441
442
443
444
445
# File 'lib/maven/tools/gem_project.rb', line 437

def gemspec(name = nil)
  if name
    load_gemspec(File.join(File.dirname(@current_file), name))
  else
    Dir[File.join(File.dirname(@current_file), "*.gemspec")].each do |file|
      load_gemspec(file)
    end
  end
end

#git(*args) ⇒ Object



454
455
# File 'lib/maven/tools/gem_project.rb', line 454

def git(*args)
end

#group(*args, &block) ⇒ Object



431
432
433
434
435
# File 'lib/maven/tools/gem_project.rb', line 431

def group(*args, &block)
  stack << args
  block.call if block
  stack.pop
end

#has_gem?(gem) ⇒ Boolean

Returns:

  • (Boolean)


409
410
411
# File 'lib/maven/tools/gem_project.rb', line 409

def has_gem?(gem)
  self.gem?(gem)
end

#load_gemfile(file) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/maven/tools/gem_project.rb', line 186

def load_gemfile(file)
  file = file.path if file.is_a?(File)
  if File.exists? file
    @current_file = file
    content = File.read(file)
    #loaded_files << file
    if @lock.nil?
      @lock = GemfileLock.new(file + ".lock")
      if @lock.size == 0
        @lock = nil
      else
        @lock.hull.each do |dep|
          dependency_management.gem dep
        end
      end
    end
    eval content

    # we have a Gemfile so we add the bundler plugin
    plugin(:bundler)

    # cleanup versions from deps
    if @lock
      dependencies.each do |d|
        if d.group_id == 'rubygems' && @lock.keys.member?( d.artifact_id ) 
          d.version = nil 
        end
      end
    end
  else
    self
  end
end

#load_gemspec(specfile) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/maven/tools/gem_project.rb', line 62

def load_gemspec(specfile)
  require 'rubygems'
  if specfile.is_a? ::Gem::Specification 
    spec = specfile
  else
    spec = ::Gem::Specification.load(specfile)
    @gemspec = specfile
  end
  raise "file not found '#{specfile}'" unless spec
  @current_file = specfile
  artifact_id spec.name
  version spec.version
  name spec.summary || "#{self.artifact_id} - gem"
  description spec.description if spec.description
  url spec.homepage if spec.homepage
  done_authors = []
  (spec.email || []).zip(spec.authors || []).map do |email, author|
    done_authors << author
    self.developers.new(author, email)
  end
  (spec.authors - done_authors).each do |author|
    self.developers.new(author, nil)
  end

  # flatten the array since copyright-header-1.0.3.gemspec has a double
  # nested array
  (spec.licenses + spec.files.select {|file| file.to_s =~ /license|gpl/i }).flatten.each do |license|
    # TODO make this better, i.e. detect the right license name from the file itself
    self.licenses.new(license)
  end

  config = {}
   if @gemspec
     relative = File.expand_path(@gemspec).sub(/#{File.expand_path('.')}/, '').sub(/^\//, '')
     add_param(config, "gemspec", relative)
   end
  add_param(config, "autorequire", spec.autorequire)
  add_param(config, "defaultExecutable", spec.default_executable)
  add_param(config, "testFiles", spec.test_files)
  #has_rdoc always gives true => makes not sense to keep it then
  #add_param(config, "hasRdoc", spec.has_rdoc)
  add_param(config, "extraRdocFiles", spec.extra_rdoc_files)
  add_param(config, "rdocOptions", spec.rdoc_options)
  add_param(config, "requirePaths", spec.require_paths, ["lib"])
  add_param(config, "rubyforgeProject", spec.rubyforge_project)
  add_param(config, "requiredRubygemsVersion", 
            spec.required_rubygems_version && spec.required_rubygems_version != ">= 0" ? "<![CDATA[#{spec.required_rubygems_version}]]>" : nil)
  add_param(config, "bindir", spec.bindir, "bin")
  add_param(config, "requiredRubyVersion", 
            spec.required_ruby_version && spec.required_ruby_version != ">= 0" ? "<![CDATA[#{spec.required_ruby_version}]]>" : nil)
  add_param(config, "postInstallMessage", 
            spec.post_install_message ? "<![CDATA[#{spec.post_install_message}]]>" : nil)
  add_param(config, "executables", spec.executables)
  add_param(config, "extensions", spec.extensions)
  add_param(config, "platform", spec.platform, 'ruby')

  # # TODO maybe calculate extra files
  # files = spec.files.dup
  # (Dir['lib/**/*'] + Dir['spec/**/*'] + Dir['features/**/*'] + Dir['test/**/*'] + spec.licenses + spec.extra_rdoc_files).each do |f|
  #   files.delete(f)
  #   if f =~ /^.\//
  #     files.delete(f.sub(/^.\//, ''))
  #   else
  #     files.delete("./#{f}")
  #   end
  # end
  #add_param(config, "extraFiles", files)
  add_param(config, "files", spec.files)
  
  plugin('gem').with(config) if config.size > 0

  spec.dependencies.each do |dep|
    scope = 
      case dep.type
      when :runtime
        "compile"
      when :development
        "test"
      else
        warn "unknown scope: #{dep.type}"
        "compile"
      end

    versions = dep.requirement.requirements.collect do |req|
      # use this construct to get the same result in 1.8.x and 1.9.x
      req.collect{ |i| i.to_s }.join
    end
    add_gem(dep.name, versions).scope = scope
    if @lock
      # add its dependencies as well to have the version
      # determine by the dependencyManagement
      @lock.dependency_hull(dep.name).map.each do |d|
        add_gem(d[0], d[1]).scope = scope unless gem? d[0]
      end
    end
  end

  spec.requirements.each do |req|
    begin
      eval req
    rescue => e
      # TODO requirements is a list !!!
      add_param(config, "requirements", req)
    rescue SyntaxError => e
      # TODO requirements is a list !!!
      add_param(config, "requirements", req)
    rescue NameError => e
      # TODO requirements is a list !!!
      add_param(config, "requirements", req)
    end
  end
end

#load_jarfile(file) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/maven/tools/gem_project.rb', line 220

def load_jarfile(file)
  jars = Jarfile.new(file)
  if jars.exists?
    container = ArtifactPassthrough.new do |a|
      artifactId, groupId, extension, version = a.split(/:/)
      send(extension.to_sym, "#{artifactId}:#{groupId}", version)
    end
    if !jars.exists_lock? || jars.mtime > jars.mtime_lock
      jars.populate_unlocked container
    end
    jars.populate_locked container
  end
end

#load_mavenfile(file) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/maven/tools/gem_project.rb', line 175

def load_mavenfile(file)
  file = file.path if file.is_a?(File)
  if File.exists? file
    @current_file = file
    content = File.read(file)
    eval content
  else
    self
  end
end

#path(*args) ⇒ Object



451
452
# File 'lib/maven/tools/gem_project.rb', line 451

def path(*args)
end

#platforms(*args, &block) ⇒ Object



462
463
464
465
466
# File 'lib/maven/tools/gem_project.rb', line 462

def platforms(*args, &block)
  if is_jruby_platform(*args)
    block.call
  end
end

#source(*args) ⇒ Object



447
448
449
# File 'lib/maven/tools/gem_project.rb', line 447

def source(*args)
  warn "ignore source #{args}" if !(args[0].to_s =~ /^https?:\/\/rubygems.org/) && args[0] != :rubygems
end