lib/java/java.rb in buildr-0.16.0 vs lib/java/java.rb in buildr-0.18.0
- old
+ new
@@ -1,9 +1,12 @@
+require 'stringio'
+
module Buildr
module Java
JAVA_OPTIONS = [ :verbose, :noop, :cp, :classpath, :name, :java_args ]
+ JUNIT_REQUIRES = [ "junit:junit:jar:3.8.1", "jmock:jmock:jar:1.1.0" ]
def self.java(*args)
options = Hash === args.last ? args.pop : {}
options[:verbose] ||= Rake.application.options.trace || false
fu_check_options options, *JAVA_OPTIONS
@@ -42,10 +45,23 @@
puts "Running apt" if verbose
sh(path_to_bin("apt"), *args) { |ok, res| fail "Failed to execute apt, see errors above" unless ok }
end
end
+ # Gets the version of the java VM
+ def self.version()
+ @version ||= `java -version 2>&1`.scan(/java version "(.*)"/)[0][0]
+ end
+
+ def self.tools()
+ unless @tools
+ tools = File.join(ENV['JAVA_HOME'], "lib", "tools.jar")
+ @tools = File.exist?(tools) ? tools : []
+ end
+ @tools
+ end
+
class AptTask < Rake::FileTask
def initialize(*args)
super
enhance do |task|
@@ -92,18 +108,40 @@
puts "Compiling #{files.size} source files in #{name}" if verbose
sh(path_to_bin("javac"), *cmd_args) { |ok, res| fail "Failed to compile, see errors above" unless ok }
end
end
+ def self.junit(*args)
+ options = Hash === args.last ? args.pop : {}
+ options[:verbose] ||= Rake.application.options.trace || false
+ fu_check_options options, :verbose, :noop, :cp, :classpath, :sourcepath, :output, :javac_args, :name
+
+ classpath = classpath_from(options) + junit_artifacts
+ tests = args.flatten
+ failed = tests.inject([]) do |failed, test|
+ begin
+ java "junit.textui.TestRunner", test, :classpath=>classpath, :name=>"tests in #{test}"
+ failed
+ rescue
+ failed << test
+ end
+ end
+ [ tests - failed, failed ]
+ end
+
protected
- def self.path_to_bin(name)
+ def self.path_to_bin(name = "java")
ENV["JAVA_HOME"] ? File.join(ENV["JAVA_HOME"], "bin", name) : name
end
def self.classpath_from(options)
classpath = (options[:classpath] || []).collect | (options[:cp] || []).collect
artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.map(&:to_s)
+ end
+
+ def self.junit_artifacts()
+ @junit_artifacts ||= artifacts(JUNIT_REQUIRES).each { |task| task.invoke }
end
end
def java(*args)