lib/java/java.rb in buildr-1.2.6 vs lib/java/java.rb in buildr-1.2.7

- old
+ new

@@ -1,70 +1,96 @@ -require "rjb" +require "rjb" if RUBY_PLATFORM != 'java' +require "java" if RUBY_PLATFORM == 'java' require "core/project" module Buildr # Base module for all things Java. module Java # Options accepted by #java and other methods here. JAVA_OPTIONS = [ :verbose, :classpath, :name, :java_args, :properties ] - # Returned by Java#rjb, you can use this object to set the RJB classpath, specify blocks to be invoked + # Returned by Java#wrapper, you can use this object to set the classpath, specify blocks to be invoked # after loading RJB, and load RJB itself. # # RJB can be loaded exactly once, and once loaded, you cannot change its classpath. Of course you can # call libraries that manage their own classpath, but the lazy way is to just tell RJB of all the # classpath dependencies you need in advance, before loading it. # # For that reason, you should not load RJB until the moment you need it. You can call #load or call - # Java#rjb with a block. For the same reason, you may need to specify code to execute when loading - # (see #onload). - class RjbWrapper #:nodoc: + # Java#wrapper with a block. For the same reason, you may need to specify code to execute when loading + # (see #setup). + # + # JRuby doesn't have the above limitation, but uses the same API regardless. + class JavaWrapper #:nodoc: include Singleton def initialize() #:nodoc: @classpath = [Java.tools_jar].compact - @onload = [] - onload do - onload do - classpath = Buildr.artifacts(@classpath).each { |task| task.invoke if task.respond_to?(:invoke) }.map(&:to_s) - ::Rjb.load classpath.join(File::PATH_SEPARATOR), Buildr.options.java_args.flatten + @setup = [] + setup do + setup do + classpath = Buildr.artifacts(@classpath). + each { |task| task.invoke if task.respond_to?(:invoke) }. + map(&:to_s) + + if RUBY_PLATFORM != 'java' + ::Rjb.load classpath.join(File::PATH_SEPARATOR), + Buildr.options.java_args.flatten + else + classpath.each do |jlib| + require jlib + end + end end end end - # The classpath used when loading RJB. - attr_accessor :classpath + def classpath + if RUBY_PLATFORM == 'java' + # in order to get a complete picture, we need to add a few jars to the + # list. + java.lang.System.getProperty('java.class.path').split(':') + + @classpath + else + @classpath + end + end - # :call-seq: - # onload { |rjb| ... } - # - # Adds a block to call when loading RJB and returns self. - # - # You can only load RJB once, and you may need to do some tasks after the initial load. - # For example, the Ant module requires Antwrap which can only be loaded after RJB. - def onload(&block) - @onload << block + attr_writer :classpath + + def setup(&block) + @setup << block self end - # :call-seq: - # load() - # - # Loads RJB. You can also call Java#ejb with a block to get the same effect. def load() - @onload.each { |block| block.call self } - @onload.clear + @setup.each { |block| block.call self } + @setup.clear end + def import(jlib) + if RUBY_PLATFORM == 'java' + Java.send(jlib) + else + ::Rjb.import jlib + end + end + def method_missing(sym, *args, &block) #:nodoc: - ::Rjb.send sym, *args, &block + # these aren't the same, but depending on method_missing while + # supporting two unrelated systems is asking for trouble anyways. + if RUBY_PLATFORM == 'java' + Java.send sym, *args, &block + else + ::Rjb.send sym, *args, &block + end end end - + class << self # :call-seq: # version() => string # @@ -72,11 +98,11 @@ # # For example: # puts Java.version # => 1.5.0_10 def version() - @version ||= Java.rjb { |rjb| rjb.import("java.lang.System").getProperty("java.version") } + @version ||= Java.wrapper { |jw| jw.import("java.lang.System").getProperty("java.version") } end # :call-seq: # tools_jar() => path # @@ -158,12 +184,12 @@ cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? cmd_args += files unless Rake.application.options.dryrun puts "Running apt" if verbose puts (["apt"] + cmd_args).join(" ") if Rake.application.options.trace - Java.rjb do |rjb| - rjb.import("com.sun.tools.apt.Main").process(cmd_args) == 0 or + Java.wrapper do |jw| + jw.import("com.sun.tools.apt.Main").process(cmd_args) == 0 or fail "Failed to process annotations, see errors above" end end end @@ -195,12 +221,12 @@ cmd_args += options[:javac_args].flatten if options[:javac_args] cmd_args += files unless Rake.application.options.dryrun puts "Compiling #{files.size} source files in #{name}" if verbose puts (["javac"] + cmd_args).join(" ") if Rake.application.options.trace - Java.rjb do |rjb| - rjb.import("com.sun.tools.javac.Main").compile(cmd_args) == 0 or + Java.wrapper do |jw| + jw.import("com.sun.tools.javac.Main").compile(cmd_args) == 0 or fail "Failed to compile, see errors above" end end end @@ -246,12 +272,12 @@ cmd_args += args.flatten.uniq name = options[:name] || Dir.pwd unless Rake.application.options.dryrun puts "Generating Javadoc for #{name}" if verbose puts (["javadoc"] + cmd_args).join(" ") if Rake.application.options.trace - Java.rjb do |rjb| - rjb.import("com.sun.tools.javadoc.Main").execute(cmd_args) == 0 or + Java.wrapper do |jw| + jw.import("com.sun.tools.javadoc.Main").execute(cmd_args) == 0 or fail "Failed to generate Javadocs, see errors above" end end end @@ -290,30 +316,31 @@ [ tests - failed, failed ] end # :call-seq: - # rjb() => RjbWrapper - # rjb() { ... } + # wrapper() => JavaWrapper + # wrapper() { ... } # - # This method can be used in two ways. Without a block, returns the RjbWrapper - # object which you can use to configure the RJB classpath or call other RJB methods. - # With a block, loads RJB and yields to the block, returning its result. + # This method can be used in two ways. Without a block, returns the + # JavaWrapper object which you can use to configure the classpath or call + # other methods. With a block, loads RJB or sets up JRuby and yields to + # the block, returning its result. # # For example: - # Java.rjb.classpath << REQUIRES - # Java.rjb.onload { require "antwrap" } + # Java.wrapper.classpath << REQUIRES + # Java.wrapper.setup { require "antwrap" } # # def execute(name, options) # options = options.merge(:name=>name, :base_dir=>Dir.pwd, :declarative=>true) - # Java.rjb { AntProject.new(options) } + # Java.wrapper { AntProject.new(options) } # end - def rjb() + def wrapper() if block_given? - RjbWrapper.instance.load - yield RjbWrapper.instance + JavaWrapper.instance.load + yield JavaWrapper.instance else - RjbWrapper.instance + JavaWrapper.instance end end # :call-seq: # path_to_bin(cmd?) => path