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 name = options[:name] || "java #{args.first}" cmd_args = [] classpath = classpath_from(options) cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? cmd_args += options[:java_args].flatten if options[:java_args] cmd_args += args.flatten.compact cmd_args << { :verbose=>options[:verbose] } unless options[:noop] puts "Running #{name}" if verbose sh(path_to_bin("java"), *cmd_args) { |ok, res| fail "Failed to execute #{name}, see errors above" unless ok } end end def self.apt(*args) options = Hash === args.last ? args.pop : {} options[:verbose] ||= Rake.application.options.trace || false fu_check_options options, :verbose, :noop, :compile, :source, :output, :cp, :classpath files = args.collect { |arg| File.directory?(arg) ? FileList[File.join(arg, "**", "*.java")] : arg }.flatten args = [ options[:verbose] ? "-verbose" : "-nowarn" ] if options[:compile] args << "-d" << options[:output] else args << "-nocompile" << "-s" << options[:output] end args << "-source" << options[:source] if options[:source] classpath = classpath_from(options) args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? args += files args << { :verbose=>options[:verbose] } unless options[:noop] 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| Java.apt *task.prerequisites + [options.merge(:output=>task.name)] end end def options() @options ||= {} end def using(options) self.options.merge!(options) self end end def self.apt_task(args) output = args.keys.first files = args.values.first.collect { |f| File.directory?(f) ? FileList[f + "/**/*.java"] : f }.flatten AptTask.define_task(output=>files) end def self.javac(*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 files = args.flatten.each { |f| f.invoke if f.respond_to?(:invoke) }.map(&:to_s). collect { |arg| File.directory?(arg) ? FileList[File.join(arg, "**", "*.java")] : arg }.flatten name = options[:name] || Dir.pwd cmd_args = [] classpath = classpath_from(options) #classpath += options[:output] if options[:output] cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? cmd_args << "-sourcepath" << options[:sourcepath].join(File::PATH_SEPARATOR) if options[:sourcepath] cmd_args << "-d" << options[:output] if options[:output] cmd_args += options[:javac_args].flatten if options[:javac_args] cmd_args += files cmd_args << { :verbose=>options[:verbose] } unless options[:noop] || files.empty? 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 = "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) Java.java(*args) end end