Module: Buildr::Ivy::IvyExtension

Includes:
Extension
Defined in:
lib/buildr/ivy_extension.rb

Overview

The Ivy Buildr extension adding the new tasks for ivy.

To use ivy in a buildfile do something like:

ENV['BUILDR_EXT_DIR'] ||= '../Ivy'
require 'buildr/ivy_extension'
  define 'ivy_project' do
  [...]
  ivy.compile_conf('compile').test_conf('test').package_conf('prod', 'server')
  [...]
end
  • This will add the compile configuration to compile and test tasks

  • Add the test configuration to test compilation and execution

  • include the artifacts from prod and server to any generated war or ear

  • The ENV variable is needed to automatically configure the load path for ivy libs. It assumes that you have the following dir structure [BUILDR_EXT_DIR]/ivy-home/jars

For more configuration options see IvyConfig.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_copy_tasks_for_publish(project) ⇒ Object



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/buildr/ivy_extension.rb', line 648

def add_copy_tasks_for_publish(project)
  ivy_project = project
  until ivy_project.ivy.own_file?
    ivy_project = ivy_project.parent
  end
  project.packages.each do |pkg|
    target_file = project.ivy.publish[pkg] || File.basename(pkg.name).gsub(/-#{project.version}/, '')
    taskname = ivy_project.path_to(ivy_project.ivy.publish_from, target_file)
    if taskname != pkg.name
      project.file taskname => pkg.name do
        verbose "Ivy copying '#{pkg.name}' to '#{taskname}' for publishing"
        FileUtils.mkdir_p File.dirname(taskname) unless File.directory?(File.dirname(taskname))
        FileUtils.cp pkg.name, taskname
      end
    end

    ivy_project.task 'ivy:publish' => taskname
  end
end

.add_ivy_deps_to_java_tasks(project) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/buildr/ivy_extension.rb', line 527

def add_ivy_deps_to_java_tasks(project)
  resolve_target = project.ivy.file_project.task('ivy:resolve')
  project.task :compiledeps => resolve_target do
    includes = project.ivy.compile_include
    excludes = project.ivy.compile_exclude
    types = project.ivy.compile_type
    confs = [project.ivy.compile_conf].flatten
    if deps = project.ivy.filter(confs, :type => types, :include => includes, :exclude => excludes)
      project.compile.with [deps, project.compile.dependencies].flatten
      sort_dependencies(project.compile.dependencies, deps, project.path_to(''))
      info "Ivy adding compile dependencies '#{confs.join(', ')}' to project '#{project.name}'"
    end
  end
      
  project.task :compile => "#{project.name}:compiledeps"
      
  project.task :testdeps => resolve_target do
    includes = project.ivy.test_include
    excludes = project.ivy.test_exclude
    types = project.ivy.test_type
    confs = [project.ivy.test_conf, project.ivy.compile_conf].flatten.uniq
    if deps = project.ivy.filter(confs, :type => types, :include => includes, :exclude => excludes)
      project.test.with [deps, project.test.dependencies].flatten
      sort_dependencies(project.test.dependencies, deps, project.path_to(''))
      sort_dependencies(project.test.compile.dependencies, deps, project.path_to(''))
      info "Ivy adding test dependencies '#{confs.join(', ')}' to project '#{project.name}'"
    end
  end
  project.task "test:compile" => "#{project.name}:testdeps"
      
  project.task :docdeps => resolve_target do
    confs = [project.ivy.test_conf, project.ivy.compile_conf].flatten.uniq
    if deps = project.ivy.deps(confs)
      project.doc.with deps
      info "Ivy adding doc dependencies '#{confs.join(', ')}' to project '#{project.name}'"
    end
  end
  project.doc.enhance( ["#{project.name}:docdeps"] )
      
  [project.task(:eclipse), project.task(:idea), project.task(:idea7x)].each do |task|
    [task, task.prerequisites].flatten.each{|p| p.enhance ["#{project.name}:compiledeps", "#{project.name}:testdeps"]}
  end
end

.add_manifest_to_distributeables(project) ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/buildr/ivy_extension.rb', line 600

def add_manifest_to_distributeables(project)
  pkgs = project.packages.find_all { |pkg| ['jar', 'war', 'ear'].member? pkg.type.to_s }
  pkgs.each do |pkg|
    name = "#{pkg.name}manifest"
    task = project.task name => project.ivy.file_project.task('ivy:resolve') do
      if pkg.manifest # source jars have no manifest, only add to existing manifest files
        pkg.with :manifest => pkg.manifest.merge(project.manifest.merge(project.ivy.manifest))
        info "Adding manifest entries to package '#{pkg.name}'"
      else
        info "Could not merge info to package '#{pkg.to_s}' it has no manifest!"
      end
    end
    project.task :build => task
  end
end

.add_prod_libs_to_distributeables(project) ⇒ Object



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/buildr/ivy_extension.rb', line 616

def add_prod_libs_to_distributeables(project)
  pkgs = project.packages.find_all { |pkg| ['war'].member? pkg.type.to_s }
  pkgs.each do |pkg|
    task = project.task "#{pkg.name}deps" => project.ivy.file_project.task('ivy:resolve') do
      includes = project.ivy.package_include
      excludes = project.ivy.package_exclude
      types = project.ivy.package_type
      confs = project.ivy.package_conf
      if deps = project.ivy.filter(confs, :type => types, :include => includes, :exclude => excludes)
        pkg.with :libs => [deps, pkg.libs].flatten
        info "Adding production libs from conf '#{confs.join(', ')}' to WAR '#{pkg.name}' in project '#{project.name}'"
      end
    end
    project.task :build => task
  end
      
  pkgs = project.packages.find_all { |pkg| ['ear'].member? pkg.type.to_s }
  pkgs.each do |pkg|
    task = project.task "#{pkg.name}deps" => project.ivy.file_project.task('ivy:resolve') do
      includes = project.ivy.package_include
      excludes = project.ivy.package_exclude
      types = project.ivy.package_type
      confs = project.ivy.package_conf
      if deps = project.ivy.filter(confs, :type => types, :include => includes, :exclude => excludes)
        pkg.add deps, :type => :lib, :path => ''
        info "Adding production libs from conf '#{confs.join(', ')}' to EAR '#{pkg.name}' in project '#{project.name}'"
      end
    end
    project.task :build => task
  end
end

.sort_dependencies(deps, ivy_deps, project_path) ⇒ Object

Sorts the dependencies in #deps replacing the old order. Sorting is done as follows:

  1. all dependencies that belong to the project identified by #project_path, .i.e. instrumented-classes, resources in the order the are contained in the array

  2. all ivy dependencies identified by #ivy_deps

  3. all dependencies added automatically by buildr



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/buildr/ivy_extension.rb', line 577

def sort_dependencies(deps, ivy_deps, project_path)
  old_deps = deps.dup
  belongs_to_project = /#{project_path}/
  deps.sort! do |a, b|
    a_belongs_to_project = belongs_to_project.match(a.to_s)
    b_belongs_to_project = belongs_to_project.match(b.to_s)
    a_ivy = ivy_deps.member? a
    b_ivy = ivy_deps.member? b

    if a_belongs_to_project && !b_belongs_to_project
      -1
    elsif !a_belongs_to_project && b_belongs_to_project
      1
    elsif a_ivy && !b_ivy
      -1
    elsif !a_ivy && b_ivy
      1
    else
      old_deps.index(a) <=> old_deps.index(b)
    end
  end
end

Instance Method Details

#ivyObject

Returns the ivy configuration for the project. Use this to configure Ivy. see IvyConfig for more details about configuration options.



671
672
673
# File 'lib/buildr/ivy_extension.rb', line 671

def ivy
  @ivy_config ||= IvyConfig.new(self)
end