Rake::JavaExtensionTask

Attributes

classpath[RW]
debug[RW]
source_version[RW]

Provide source compatibility with specified release

target_version[RW]

Generate class files for specific VM version

Public Instance Methods

define() click to toggle source
    # File lib/rake/javaextensiontask.rb, line 38
38:     def define
39:       super
40: 
41:       define_java_platform_tasks
42:     end
init(name = nil, gem_spec = nil) click to toggle source
    # File lib/rake/javaextensiontask.rb, line 28
28:     def init(name = nil, gem_spec = nil)
29:       super
30:       @source_pattern = '**/*.java'
31:       @classpath      = nil
32:       @java_compiling = nil
33:       @debug          = false
34:       @source_version = '1.5'
35:       @target_version = '1.5'
36:     end
java_compiling(&block) click to toggle source
    # File lib/rake/javaextensiontask.rb, line 24
24:     def java_compiling(&block)
25:       @java_compiling = block if block_given?
26:     end
platform() click to toggle source
    # File lib/rake/javaextensiontask.rb, line 20
20:     def platform
21:       @platform ||= 'java'
22:     end

Private Instance Methods

define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION) click to toggle source
     # File lib/rake/javaextensiontask.rb, line 45
 45:     def define_compile_tasks(for_platform = nil, ruby_ver = RUBY_VERSION)
 46:       # platform usage
 47:       platf = for_platform || platform
 48: 
 49:       # lib_path
 50:       lib_path = lib_dir
 51: 
 52:       # tmp_path
 53:       tmp_path = "#{@tmp_dir}/#{platf}/#{@name}"
 54: 
 55:       # cleanup and clobbering
 56:       CLEAN.include(tmp_path)
 57:       CLOBBER.include("#{lib_path}/#{binary(platf)}")
 58:       CLOBBER.include("#{@tmp_dir}")
 59: 
 60:       # directories we need
 61:       directory tmp_path
 62:       directory lib_dir
 63: 
 64:       # copy binary from temporary location to final lib
 65:       # tmp/extension_name/extension_name.{so,bundle} => lib/
 66:       task "copy:#{@name}:#{platf}" => [lib_path, "#{tmp_path}/#{binary(platf)}"] do
 67:         cp "#{tmp_path}/#{binary(platf)}", "#{lib_path}/#{binary(platf)}"
 68:       end
 69: 
 70:       not_jruby_compile_msg = WARNING: You're cross-compiling a binary extension for JRuby, but are usinganother interpreter. If your Java classpath or extension dir settings are notcorrectly detected, then either check the appropriate environment variables orexecute the Rake compilation task using the JRuby interpreter.(e.g. `jruby -S rake compile:java`)
 71:       warn_once(not_jruby_compile_msg) unless defined?(JRUBY_VERSION)
 72: 
 73:       file "#{tmp_path}/#{binary(platf)}" => "#{tmp_path}/.build" do
 74: 
 75:         class_files = FileList["#{tmp_path}/**/*.class"].
 76:           gsub("#{tmp_path}/", '')
 77: 
 78:         # avoid environment variable expansion using backslash
 79:         class_files.gsub!('$', '\$') unless windows?
 80: 
 81:         args = class_files.map { |path|
 82:           ["-C #{tmp_path}", path]
 83:         }.flatten
 84: 
 85:         sh "jar cf #{tmp_path}/#{binary(platf)} #{args.join(' ')}"
 86:       end
 87: 
 88:       file "#{tmp_path}/.build" => [tmp_path] + source_files do
 89:         classpath_arg = java_classpath_arg(@classpath)
 90:         debug_arg     = @debug ? '-g' : ''
 91: 
 92:         sh "javac #{java_extdirs_arg} -target #{@target_version} -source #{@source_version} -Xlint:unchecked #{debug_arg} #{classpath_arg} -d #{tmp_path} #{source_files.join(' ')}"
 93: 
 94:         # Checkpoint file
 95:         touch "#{tmp_path}/.build"
 96:       end
 97: 
 98:       # compile tasks
 99:       unless Rake::Task.task_defined?('compile') then
100:         desc "Compile all the extensions"
101:         task "compile"
102:       end
103: 
104:       # compile:name
105:       unless Rake::Task.task_defined?("compile:#{@name}") then
106:         desc "Compile #{@name}"
107:         task "compile:#{@name}"
108:       end
109: 
110:       # Allow segmented compilation by platform (open door for 'cross compile')
111:       task "compile:#{@name}:#{platf}" => ["copy:#{@name}:#{platf}"]
112:       task "compile:#{platf}" => ["compile:#{@name}:#{platf}"]
113: 
114:       # Only add this extension to the compile chain if current
115:       # platform matches the indicated one.
116:       if platf == RUBY_PLATFORM then
117:         # ensure file is always copied
118:         file "#{lib_path}/#{binary(platf)}" => ["copy:#{name}:#{platf}"]
119: 
120:         task "compile:#{@name}" => ["compile:#{@name}:#{platf}"]
121:         task "compile" => ["compile:#{platf}"]
122:       end
123:     end
define_java_platform_tasks() click to toggle source
     # File lib/rake/javaextensiontask.rb, line 131
131:     def define_java_platform_tasks
132:       # lib_path
133:       lib_path = lib_dir
134: 
135:       if @gem_spec && !Rake::Task.task_defined?("java:#{@gem_spec.name}")
136:         task "java:#{@gem_spec.name}" do |t|
137: 
138:           # FIXME: truly duplicate the Gem::Specification
139:           spec = gem_spec.dup
140: 
141:           # adjust to specified platform
142:           spec.platform = Gem::Platform.new('java')
143: 
144:           # clear the extensions defined in the specs
145:           spec.extensions.clear
146: 
147:           # add the binaries that this task depends on
148:           ext_files = []
149: 
150:           # go through native prerequisites and grab the real extension files from there
151:           t.prerequisites.each do |ext|
152:             ext_files << ext
153:           end
154: 
155:           # include the files in the gem specification
156:           spec.files += ext_files
157: 
158:           # expose gem specification for customization
159:           if @java_compiling
160:             @java_compiling.call(spec)
161:           end
162: 
163:           # Generate a package for this gem
164:           gem_package = Rake::GemPackageTask.new(spec) do |pkg|
165:             pkg.need_zip = false
166:             pkg.need_tar = false
167:           end
168:         end
169: 
170:         # add binaries to the dependency chain
171:         task "java:#{@gem_spec.name}" => ["#{lib_path}/#{binary(platform)}"]
172: 
173:         # ensure the extension get copied
174:         unless Rake::Task.task_defined?("#{lib_path}/#{binary(platform)}") then
175:           file "#{lib_path}/#{binary(platform)}" => ["copy:#{name}:#{platform}"]
176:         end
177: 
178:         task 'java' => ["java:#{@gem_spec.name}"]
179:       end
180: 
181:       task 'java' do
182:         task 'compile' => 'compile:java'
183:       end
184:     end
java_classpath_arg(*args) click to toggle source

Discover the Java/JRuby classpath and build a classpath argument

@params

  *args:: Additional classpath arguments to append

Copied verbatim from the ActiveRecord-JDBC project. There are a small myriad of ways to discover the Java classpath correctly.

     # File lib/rake/javaextensiontask.rb, line 204
204:     def java_classpath_arg(*args)
205:       jruby_cpath = nil
206:       if RUBY_PLATFORM =~ /java/
207:         begin
208:           cpath  = Java::java.lang.System.getProperty('java.class.path').split(File::PATH_SEPARATOR)
209:           cpath += Java::java.lang.System.getProperty('sun.boot.class.path').split(File::PATH_SEPARATOR)
210:           jruby_cpath = cpath.compact.join(File::PATH_SEPARATOR)
211:         rescue => e
212:         end
213:       end
214:       unless jruby_cpath
215:         jruby_cpath = ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] &&
216:           Dir.glob("#{File.expand_path(ENV['JRUBY_HOME'])}/lib/*.jar").
217:             join(File::PATH_SEPARATOR)
218:       end
219:       raise "JRUBY_HOME or JRUBY_PARENT_CLASSPATH are not set" unless jruby_cpath
220:       jruby_cpath += File::PATH_SEPARATOR + args.join(File::PATH_SEPARATOR) unless args.empty?
221:       jruby_cpath ? "-cp \"#{jruby_cpath}\"" : ""
222:     end
java_extdirs_arg() click to toggle source

Discover Java Extension Directories and build an extdirs argument

     # File lib/rake/javaextensiontask.rb, line 189
189:     def java_extdirs_arg
190:       extdirs = Java::java.lang.System.getProperty('java.ext.dirs') rescue nil
191:       extdirs = ENV['JAVA_EXT_DIR'] unless extdirs
192:       java_extdir = extdirs.nil? ? "" : "-extdirs \"#{extdirs}\""
193:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.