module Buildr module Java JAVA_OPTIONS = [ :verbose, :noop, :cp, :classpath, :name, :java_args ] 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 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] verbose { puts "Running #{name}" } 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, :sourcepath 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 unless classpath.empty? sourcepath = (options[:sourcepath] || []).collect.join(File::PATH_SEPARATOR) args << "-sourcepath" << sourcepath unless sourcepath.empty? args += files args << { :verbose=>options[:verbose] } unless options[:noop] verbose { puts "Running apt" } sh(path_to_bin("apt"), *args) { |ok, res| fail "Failed to execute apt, see errors above" unless ok } end 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 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? verbose { puts "Compiling #{files.size} source files in #{name}" } sh(path_to_bin("javac"), *cmd_args) { |ok, res| fail "Failed to compile, see errors above" unless ok } end end protected def self.path_to_bin(name) 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).join(File::PATH_SEPARATOR) end end def java(*args) Java.java(*args) end end