class Rake::JavaExtensionTask

def define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION)

def define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION)
  # platform usage
  platf = for_platform || platform
  # lib_path
  lib_path = lib_dir
  # tmp_path
  tmp_path = "#{@tmp_dir}/#{platf}/#{@name}"
  # cleanup and clobbering
  CLEAN.include(tmp_path)
  CLOBBER.include("#{lib_path}/#{binary(platf)}")
  CLOBBER.include("#{@tmp_dir}")
  # directories we need
  directory tmp_path
  directory lib_dir
  # copy binary from temporary location to final lib
  # tmp/extension_name/extension_name.{so,bundle} => lib/
  task "copy:#{@name}:#{platf}" => [lib_path, "#{tmp_path}/#{binary(platf)}"] do
    install "#{tmp_path}/#{binary(platf)}", "#{lib_path}/#{binary(platf)}"
  end
  file "#{tmp_path}/#{binary(platf)}" => "#{tmp_path}/.build" do
    class_files = FileList["#{tmp_path}/**/*.class"].
      gsub("#{tmp_path}/", '')
    # avoid environment variable expansion using backslash
    class_files.gsub!('$', '\$') unless windows?
    args = class_files.map { |path|
      ["-C #{tmp_path}", path]
    }.flatten
    sh "jar cf #{tmp_path}/#{binary(platf)} #{args.join(' ')}"
  end
  file "#{tmp_path}/.build" => [tmp_path] + source_files do
    not_jruby_compile_msg = <<-EOF
ING: You're cross-compiling a binary extension for JRuby, but are using
her interpreter. If your Java classpath or extension dir settings are not
ectly detected, then either check the appropriate environment variables or
ute the Rake compilation task using the JRuby interpreter.
. `jruby -S rake compile:java`)
    EOF
    warn_once(not_jruby_compile_msg) unless defined?(JRUBY_VERSION)
    javac_command_line = [
      "javac",
      "-target", @target_version,
      "-source", @source_version,
      java_lint_arg,
      "-d", tmp_path,
    ]
    javac_command_line.concat(java_encoding_args)
    javac_command_line.concat(java_extdirs_args)
    javac_command_line.concat(java_classpath_args)
    javac_command_line << "-g" if @debug
    javac_command_line.concat(source_files)
    sh(*javac_command_line)
    # Checkpoint file
    touch "#{tmp_path}/.build"
  end
  # compile tasks
  unless Rake::Task.task_defined?('compile') then
    desc "Compile all the extensions"
    task "compile"
  end
  # compile:name
  unless Rake::Task.task_defined?("compile:#{@name}") then
    desc "Compile #{@name}"
    task "compile:#{@name}"
  end
  # Allow segmented compilation by platform (open door for 'cross compile')
  task "compile:#{@name}:#{platf}" => ["copy:#{@name}:#{platf}"]
  task "compile:#{platf}" => ["compile:#{@name}:#{platf}"]
  # Only add this extension to the compile chain if current
  # platform matches the indicated one.
  if platf == RUBY_PLATFORM then
    # ensure file is always copied
    file "#{lib_path}/#{binary(platf)}" => ["copy:#{name}:#{platf}"]
    task "compile:#{@name}" => ["compile:#{@name}:#{platf}"]
    task "compile" => ["compile:#{platf}"]
  end
end