Class: Jarbler::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/jarbler/builder.rb

Instance Method Summary collapse

Instance Method Details

#build_jarString

Execute all functions needed to build the jar file Should be executed in application directory of Rails/Ruby application

Returns:

  • (String)

    Ruby minor version of the JRuby jars with patch level set to 0



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/jarbler/builder.rb', line 14

def build_jar
  debug "Running with Ruby version '#{RUBY_VERSION}' on platform '#{RUBY_PLATFORM}'. Engine '#{RUBY_ENGINE}' version '#{RUBY_ENGINE_VERSION}'"

  @config = nil # Ensure config is read from file or default. Necessary for testing only because of caching
  staging_dir = Dir.mktmpdir # create a temporary directory for staging
  app_root = Dir.pwd
  debug "Project dir: #{app_root}"

  ruby_minor_version = copy_jruby_jars_to_staging(staging_dir) # Copy the jruby jars to the staging directory
  exec_command "javac -nowarn -Xlint:deprecation #{config.java_opts} -d #{staging_dir} #{__dir__}/JarMain.java" # Compile the Java files

  # Copy the application project to the staging directory
  FileUtils.mkdir_p("#{staging_dir}/app_root")
  config.includes.each do |dir|
    file_utils_copy("#{app_root}/#{dir}", "#{staging_dir}/app_root") if File.exist?("#{app_root}/#{dir}")
  end

  # Get the needed Gems
  raise "Gemfile.lock not found in #{app_root}" unless File.exist?("#{app_root}/Gemfile.lock")

  # Copy the needed Gems to the staging directory
  copy_needed_gems_to_staging(staging_dir, ruby_minor_version)

  Dir.chdir(staging_dir) do
    # create the manifest file
    File.open('Manifest.txt', 'w') do |file|
      file.write("Comment: created by Jarbler (https://github.com/rammpeter/jarbler)\n")
      file.write("Main-Class: JarMain\n")
    end

    # Write java properties file for use in JarMain.java
    File.open('jarbler.properties', 'w') do |file|
      file.write("jarbler.executable=#{config.executable}\n")
      # write a list of strings into property file delimited by space
      java_executable_params = ''
      config.executable_params.each do |param|
        java_executable_params += "#{param} "
      end
      file.write("jarbler.executable_params=#{java_executable_params.strip}\n")
      file.write("jarbler.compile_ruby_files=#{config.compile_ruby_files}\n")
      file.write("jarbler.gem_home_suffix=jruby/#{ruby_minor_version}\n")  # Extension after BUNDLE_PATH for local Gems
    end

    # remove files and directories from excludes, if they exist (after copying the rails project and the gems)
    config.excludes.each do |exclude|
      to_remove = "app_root/#{exclude}"
      if File.exist?(to_remove)
        debug "Removing #{to_remove} from staging directory"
        FileUtils.rm_rf(to_remove)
      else
        debug "Not removing #{to_remove} from staging directory, because it does not exist"
      end
    end

    compile_ruby_files if config.compile_ruby_files

    exec_command "jar cfm #{config.jar_name} Manifest.txt *" # create the jar file

    # place the jar in project directory
    file_utils_copy(config.jar_name, app_root)
    puts "Created jar file #{app_root}/#{config.jar_name}"
    ruby_minor_version                                                      # Used in tests to know the target Gem dir
  end
rescue Exception => e
  puts "Error: #{e.message}"
  puts e.backtrace.join("\n")
  raise
ensure
  # remove temporary directory staging_dir
  if ENV['DEBUG']
    puts "Temporary directory #{staging_dir} not removed because of debug mode"
  else
    FileUtils.remove_entry staging_dir if staging_dir
  end
end