class PomLoader::Loader

Attributes

loaded[RW]
log4j_xml_file[RW]
mvn_exe[RW]
mvn_repo_dir[RW]
pom_changed_at[RW]
pom_dir[RW]
target_classes_dir[RW]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options overrides default mvn pom settings @option options [String] :pom_dir (Dir.pwd) The directory where your pom.xml file lives @option options [String] :mvn_repo_dir (Dir.home + '/.m2/repository') The directory where your .m2 repository lives @option options [String] :target_classes_dir (pom_dir + '/target/classes') The target classes directory @option options [String] :log4j_xml_file ('') Optional log4j configuration @option options [String] :mvn_exe ('mvn') the maven executable command

note: mvn_repo_dir has absolutely no impact on anything.

# File lib/pom-loader.rb, line 38
def initialize(options = {})
  @pom_dir = options[:pom_dir] || Dir.pwd
  @mvn_repo_dir = options[:mvn_repo_dir] || (Dir.home + '/.m2/repository')
  @target_classes_dir = options[:target_classes_dir] || (@pom_dir + '/target/classes')
  @log4j_xml_file = options[:log4j_xml_file] || "#{@target_classes_dir}/com/homeaway/log4j.xml"
  @mvn_exe = options[:mvn_exe] || ENV['MVN2_EXE'] || 'mvn'
end

Public Instance Methods

add_jars_to_classpath() click to toggle source
# File lib/pom-loader.rb, line 86
def add_jars_to_classpath
  time = Benchmark.realtime do
    File.readlines("#{@pom_dir}/target/build-classpath.txt").each do |line|
      line.split(':').each do |jar|
        $CLASSPATH << jar
      end
    end

    $CLASSPATH << @target_classes_dir if Dir.exists? @target_classes_dir
  end
  puts "Added java paths to $CLASSPATH in #{time}"
end
build_classpath() click to toggle source
# File lib/pom-loader.rb, line 75
def build_classpath
  time = Benchmark.realtime do
    build_classpath = "#{@pom_dir}/target/build-classpath.txt"
    if !File.exists?(build_classpath) || File.ctime(build_classpath) < @pom_changed_at
      process_output = `#{@mvn_exe} -f #{@pom_dir}/pom.xml dependency:build-classpath -DincludeTypes=jar -Dmdep.outputFile=#{build_classpath}`
      puts process_output unless $?.success?
    end
  end
  puts "Built build-classpath.txt in #{time}s"
end
build_effective_pom() click to toggle source
# File lib/pom-loader.rb, line 64
def build_effective_pom
  time = Benchmark.realtime do
    effective_pom_path = "#{@pom_dir}/target/effective-pom.xml"
    if !File.exists?(effective_pom_path) || File.ctime(effective_pom_path) < @pom_changed_at
      process_output = `#{mvn_exe} -f #{@pom_dir}/pom.xml help:effective-pom -Doutput=#{effective_pom_path}`
      puts process_output unless $?.success?
    end
  end
  puts "Built effective-pom.xml in #{time}s"
end
load() click to toggle source

Load dependency jars onto classpath specified by a maven pom.xml file

# File lib/pom-loader.rb, line 47
def load
  if File.exists? "#{pom_dir}/pom.xml"
    @pom_changed_at = File.ctime("#{pom_dir}/pom.xml")
    puts "Loading java environment from #{@pom_dir}/pom.xml"
    build_effective_pom
    build_classpath
    add_jars_to_classpath
    parse_effective_pom
    load_logger if File.exists? @log4j_xml_file
    @loaded = true
  else
    @loaded = false
    puts "Not loading java environment. pom dir=#{@pom_dir}"
  end
  self
end
load_logger() click to toggle source
# File lib/pom-loader.rb, line 114
def load_logger
  require_java
  time = Benchmark.realtime do
    puts 'adding log4j file ' + @log4j_xml_file
    org.apache.log4j.xml.DOMConfigurator.configure @log4j_xml_file
  end
  puts "Loaded log4j in #{time}s"
end
parse_effective_pom() click to toggle source
# File lib/pom-loader.rb, line 99
def parse_effective_pom
  require_java
  time = Benchmark.realtime do
    pns = 'http://maven.apache.org/POM/4.0.0'
    pom_doc = Nokogiri::XML(File.open("#{@pom_dir}/target/effective-pom.xml"))
    pom_doc.xpath('//pom:systemProperty', 'pom' => pns).each do |n|
      name = n.xpath('pom:name', 'pom' => pns).first.text
      value = n.xpath('pom:value', 'pom' => pns).first.text
      puts "Adding System Property: #{name} value: #{value}"
      java.lang.System.setProperty name, value
    end
  end
  puts "Parsed effective-pom.xml in #{time}s"
end

Private Instance Methods

require_java() click to toggle source
# File lib/pom-loader.rb, line 125
def require_java
  raise 'Sorry you do not seem to be running on java so I cannot proceed.' unless RUBY_PLATFORM == 'java'
  require 'java'
end