Class: PomLoader::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/pom-loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

note: mvn_repo_dir has absolutely no impact on anything.

Parameters:

  • options (Hash) (defaults to: {})

    overrides default mvn pom settings

Options Hash (options):

  • :pom_dir (String) — default: Dir.pwd

    The directory where your pom.xml file lives

  • :mvn_repo_dir (String) — default: Dir.home + '/.m2/repository'

    The directory where your .m2 repository lives

  • :target_classes_dir (String) — default: pom_dir + '/target/classes'

    The target classes directory

  • :log4j_xml_file (String) — default: ''

    Optional log4j configuration

  • :mvn_exe (String) — default: 'mvn'

    the maven executable command



38
39
40
41
42
43
44
# 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

Instance Attribute Details

#loadedObject

Returns the value of attribute loaded.



28
29
30
# File 'lib/pom-loader.rb', line 28

def loaded
  @loaded
end

#log4j_xml_fileObject

Returns the value of attribute log4j_xml_file.



28
29
30
# File 'lib/pom-loader.rb', line 28

def log4j_xml_file
  @log4j_xml_file
end

#mvn_exeObject

Returns the value of attribute mvn_exe.



28
29
30
# File 'lib/pom-loader.rb', line 28

def mvn_exe
  @mvn_exe
end

#mvn_repo_dirObject

Returns the value of attribute mvn_repo_dir.



28
29
30
# File 'lib/pom-loader.rb', line 28

def mvn_repo_dir
  @mvn_repo_dir
end

#pom_changed_atObject

Returns the value of attribute pom_changed_at.



28
29
30
# File 'lib/pom-loader.rb', line 28

def pom_changed_at
  @pom_changed_at
end

#pom_dirObject

Returns the value of attribute pom_dir.



28
29
30
# File 'lib/pom-loader.rb', line 28

def pom_dir
  @pom_dir
end

#target_classes_dirObject

Returns the value of attribute target_classes_dir.



28
29
30
# File 'lib/pom-loader.rb', line 28

def target_classes_dir
  @target_classes_dir
end

Instance Method Details

#add_jars_to_classpathObject



86
87
88
89
90
91
92
93
94
95
96
97
# 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_classpathObject



75
76
77
78
79
80
81
82
83
84
# 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_pomObject



64
65
66
67
68
69
70
71
72
73
# 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

#loadObject

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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_loggerObject



114
115
116
117
118
119
120
121
# 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_pomObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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