lib/java/java.rb in buildr-1.1.3 vs lib/java/java.rb in buildr-1.2.0

- old
+ new

@@ -6,12 +6,10 @@ # Base module for all things Java. module Java # Options accepted by #java and other methods here. JAVA_OPTIONS = [ :verbose, :classpath, :name, :java_args, :properties ] - # Classpath dependencies available when running JUnit. - JUNIT_REQUIRES = [ "junit:junit:jar:3.8.1", "jmock:jmock:jar:1.1.0" ] # Returned by Java#rjb, you can use this object to set the RJB 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 @@ -19,21 +17,21 @@ # 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 + class RjbWrapper #:nodoc: include Singleton def initialize() #:nodoc: @classpath = [Java.tools_jar].compact @onload = [] onload do onload do - Rjb.load(Buildr.artifacts(classpath).each { |task| task.invoke if task.respond_to?(:invoke) }. - map(&:to_s).join(File::PATH_SEPARATOR)) + 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 end end end # The classpath used when loading RJB. @@ -59,11 +57,11 @@ @onload.each { |block| block.call self } @onload.clear end def method_missing(sym, *args, &block) #:nodoc: - Rjb.send sym, *args, &block + ::Rjb.send sym, *args, &block end end class << self @@ -74,11 +72,11 @@ # # For example: # puts Java.version # => 1.5.0_10 def version() - @version ||= `"#{path_to_bin("java")}" -version 2>&1`.scan(/java version "(.*)"/)[0][0] + @version ||= Java.rjb { |rjb| rjb.import("java.lang.System").getProperty("java.version") } end # :call-seq: # tools_jar() => path # @@ -117,11 +115,11 @@ name = options[:name] || "java #{args.first}" cmd_args = [path_to_bin("java")] classpath = classpath_from(options) cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? options[:properties].each { |k, v| cmd_args << "-D#{k}=#{v}" } if options[:properties] - cmd_args += options[:java_args].flatten if options[:java_args] + cmd_args += (options[:java_args] || Buildr.options.java_args).flatten cmd_args += args.flatten.compact unless Rake.application.options.dryrun puts "Running #{name}" if verbose block = lambda { |ok, res| fail "Failed to execute #{name}, see errors above" unless ok } unless block puts cmd_args.join(" ") if Rake.application.options.trace @@ -269,30 +267,35 @@ # # The last argument may be a Hash with additional options: # * :classpath -- One or more file names, tasks or artifact specifications. # These are all expanded into artifacts, and all tasks are invoked. # * :properties -- Hash of system properties (e.g. "path"=>base_dir). + # * :java_args -- Any additional arguments to pass (e.g. -hotspot, -xms) # * :verbose -- If true, prints the command and all its argument. + # + # *Deprecated:* Please use JUnitTask instead.Use the test task to run JUnit and other test frameworks. def junit(*args) + warn_deprecated "Use the test task to run JUnit and other test frameworks" options = Hash === args.last ? args.pop : {} options[:verbose] ||= Rake.application.options.trace || false rake_check_options options, :verbose, :classpath, :properties, :java_args - classpath = classpath_from(options) + junit_artifacts + classpath = classpath_from(options) + JUnitTask::requires tests = args.flatten failed = tests.inject([]) do |failed, test| begin java "junit.textui.TestRunner", test, :classpath=>classpath, :properties=>options[:properties], - :name=>"#{test}", :verbose=>options[:verbose] + :name=>"#{test}", :verbose=>options[:verbose], :java_args=>options[:java_args] failed rescue failed << test end end [ tests - failed, failed ] end + # :call-seq: # rjb() => RjbWrapper # rjb() { ... } # # This method can be used in two ways. Without a block, returns the RjbWrapper @@ -334,18 +337,10 @@ def classpath_from(options) classpath = (options[:classpath] || []).collect Buildr.artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.map(&:to_s) end - # :call-seq: - # junit_artifacts() => files - # - # Returns the JUnit artifacts as paths, after downloading and installing them (if necessary). - def junit_artifacts() - @junit_artifacts ||= Buildr.artifacts(JUNIT_REQUIRES).each { |task| task.invoke }.map(&:to_s) - end - def darwin?() #:nodoc: RUBY_PLATFORM =~ /darwin/i end end @@ -373,7 +368,36 @@ end end include Java + + class Options + + # :call-seq: + # java_args => array + # + # Returns the Java arguments. + def java_args() + @java_args ||= (ENV["JAVA_OPTIONS"] || ENV["java_options"] || "").split(" ") + end + + # :call-seq: + # java_args = array|string|nil + # + # Sets the Java arguments. These arguments are used when creating a JVM, including for use with RJB + # for most tasks (e.g. Ant, compile) and when forking a separate JVM (e.g. JUnit tests). You can also + # use the JAVA_OPTIONS environment variable. + # + # For example: + # options.java_args = "-verbose" + # Or: + # $ set JAVA_OPTIONS = "-Xms1g" + # $ buildr + def java_args=(args) + args = args.split if String === args + @java_args = args.to_a + end + + end end