lib/java/java.rb in buildr-0.21.0 vs lib/java/java.rb in buildr-0.22.0

- old
+ new

@@ -5,11 +5,11 @@ # Base module for all things Java. module Java # Options accepted by #java and other methods here. - JAVA_OPTIONS = [ :verbose, :classpath, :name, :java_args ] + 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. @@ -24,15 +24,17 @@ class RjbWrapper include Singleton def initialize() #:nodoc: - @classpath = [] + @classpath = [Java.tools_jar] @onload = [] onload do - Rjb.load(artifacts(classpath).each { |task| task.invoke if task.respond_to?(:invoke) }. - map(&:to_s).join(File::PATH_SEPARATOR)) + onload do + Rjb.load(Buildr.artifacts(classpath).each { |task| task.invoke if task.respond_to?(:invoke) }. + map(&:to_s).join(File::PATH_SEPARATOR)) + end end end # The classpath used when loading RJB. attr_accessor :classpath @@ -97,10 +99,11 @@ # # 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. # * :java_args -- Any additional arguments to pass (e.g. -hotspot, -xms) + # * :properties -- Hash of system properties (e.g. "path"=>base_dir). # * :name -- Shows this name, otherwise shows the first argument (the class name). # * :verbose -- If true, prints the command and all its argument. def java(*args) options = Hash === args.last ? args.pop : {} options[:verbose] ||= Rake.application.options.trace || false @@ -108,10 +111,11 @@ name = options[:name] || "java #{args.first}" cmd_args = [] 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 += args.flatten.compact cmd_args << { :verbose=>options[:verbose] } unless Rake.application.options.dryrun puts "Running #{name}" if verbose @@ -129,33 +133,33 @@ # * :source -- Specifies source compatibility with a given JVM release. # * :output -- Directory where to place the generated source files, or the # generated class files when compiling. # * :classpath -- One or more file names, tasks or artifact specifications. # These are all expanded into artifacts, and all tasks are invoked. - # * :verbose -- If true, prints the command and all its argument and also runs - # Apt in verbose mode (the default is -nowarn). def apt(*args) options = Hash === args.last ? args.pop : {} - options[:verbose] ||= Rake.application.options.trace || false - fu_check_options options, :verbose, :compile, :source, :output, :classpath + fu_check_options options, :compile, :source, :output, :classpath files = args.flatten.map(&:to_s). collect { |arg| File.directory?(arg) ? FileList["#{arg}/**/*.java"] : arg }.flatten - args = [ options[:verbose] ? "-verbose" : "-nowarn" ] + cmd_args = [ Rake.application.options.trace ? "-verbose" : "-nowarn" ] if options[:compile] - args << "-d" << options[:output].to_s + cmd_args << "-d" << options[:output].to_s else - args << "-nocompile" << "-s" << options[:output].to_s + cmd_args << "-nocompile" << "-s" << options[:output].to_s end - args << "-source" << options[:source] if options[:source] + cmd_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] } + cmd_args << "-cp" << classpath.join(File::PATH_SEPARATOR) unless classpath.empty? + cmd_args += files unless Rake.application.options.dryrun puts "Running apt" if verbose - sh(path_to_bin("apt"), *args) { |ok, res| fail "Failed to execute apt, see errors above" unless ok } + 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 + fail "Failed to process annotations, see errors above" + end end end # :call-seq: # javac(*files, options) @@ -167,32 +171,32 @@ # * :classpath -- One or more file names, tasks or artifact specifications. # These are all expanded into artifacts, and all tasks are invoked. # * :sourcepath -- Additional source paths to use. # * :javac_args -- Any additional arguments to pass (e.g. -extdirs, -encoding) # * :name -- Shows this name, otherwise shows the working directory. - # * :verbose -- If true, prints the command and all its argument. def javac(*args) options = Hash === args.last ? args.pop : {} - options[:verbose] ||= Rake.application.options.trace || false - fu_check_options options, :verbose, :classpath, :sourcepath, :output, :javac_args, :name + fu_check_options options, :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["#{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].to_s if options[:output] cmd_args += options[:javac_args].flatten if options[:javac_args] cmd_args += files - cmd_args << { :verbose=>options[:verbose] } unless Rake.application.options.dryrun 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 } + 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 + fail "Failed to compile, see errors above" + end end end # :call-seq: # javadoc(*files, options) @@ -202,54 +206,52 @@ # This method accepts the following special options: # * :output -- The output directory # * :classpath -- Array of classpath dependencies. # * :sourcepath -- Array of sourcepaths (paths or tasks). # * :name -- Shows this name, otherwise shows the working directory. - # * :verbose -- If you want an overload of details. # # All other options are passed to Javadoc as following: # * true -- As is, for example, :author=>true becomes -author # * false -- Prefixed, for example, :index=>false becomes -noindex # * string -- Option with value, for example, :windowtitle=>"My project" becomes -windowtitle "My project" # * array -- Option with set of values separated by spaces. def javadoc(*args) options = Hash === args.last ? args.pop : {} - options[:verbose] ||= Rake.application.options.trace || false - Tempfile.open("javadoc") do |opt_file| - options.reject { |key, value| [:output, :verbose, :name, :sourcepath, :classpath].include?(key) }. - each { |key, value| value.invoke if value.respond_to?(:invoke) }. - each do |key, value| - case value - when true, nil - opt_file.puts "-#{key}" - when false - opt_file.puts "-no#{key}" - when Array - opt_file.puts "-#{key} " << value.map { |val| %q{"#{val}"} }.join(" ") - when Hash - value.each { |k,v| opt_file.puts "-#{key} #{k} #{v}" } - else - opt_file.puts "-#{key} \"#{value}\"" - end + cmd_args = [ "-d", options[:output], Rake.application.options.trace ? "-verbose" : "-quiet" ] + options.reject { |key, value| [:output, :name, :sourcepath, :classpath].include?(key) }. + each { |key, value| value.invoke if value.respond_to?(:invoke) }. + each do |key, value| + case value + when true, nil + cmd_args << "-#{key}" + when false + cmd_args << "-no#{key}" + when Array + cmd_args << "-#{key}" + cmd_args += value.map(&:to_s) + when Hash + value.each { |k,v| cmd_args << "-#{key}" << k.to_s << v.to_s } + else + cmd_args << "-#{key}" << value.to_s end - [:sourcepath, :classpath].each do |option| - options[option].to_a.flatten.tap do |paths| - opt_file.puts "-#{option} " << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty? - end end - opt_file.puts args.flatten.uniq.join(" ") - opt_file.flush - cmd_args = [ "-d", options[:output], options[:verbose] ? "-verbose" : "-quiet", "@#{opt_file.path}"] - cmd_args << { :verbose=>options[:verbose] } - name = options[:name] || Dir.pwd - unless Rake.application.options.dryrun - puts "Generating Javadoc for #{name}" if verbose - puts File.read(opt_file.path) if options[:verbose] - sh(path_to_bin("javadoc"), *cmd_args) { |ok, res| fail "Failed to generate Javadocs, see errors above" unless ok } + [:sourcepath, :classpath].each do |option| + options[option].to_a.flatten.tap do |paths| + cmd_args << "-#{option}" << paths.flatten.map(&:to_s).join(File::PATH_SEPARATOR) unless paths.empty? end end + 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 + fail "Failed to generate Javadocs, see errors above" + end + end end # :call-seq: # junit(*classes, options) => [ passed, failed ] # @@ -258,21 +260,23 @@ # of all classes that failed. # # 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). # * :verbose -- If true, prints the command and all its argument. def junit(*args) options = Hash === args.last ? args.pop : {} options[:verbose] ||= Rake.application.options.trace || false - fu_check_options options, :verbose, :classpath + fu_check_options options, :verbose, :classpath, :properties 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}", :verbose=>options[:verbose] + java "junit.textui.TestRunner", test, :classpath=>classpath, :properties=>options[:properties], + :name=>"#{test}", :verbose=>options[:verbose] failed rescue failed << test end end @@ -297,11 +301,11 @@ # Java.rjb { AntProject.new(options) } # end def rjb() if block_given? RjbWrapper.instance.load - yield + yield RjbWrapper.instance else RjbWrapper.instance end end @@ -321,19 +325,19 @@ # # Extracts the classpath from the options, expands it by calling artifacts, invokes # each of the artifacts and returns an array of paths. def classpath_from(options) classpath = (options[:classpath] || []).collect - artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.map(&:to_s) + 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 ||= artifacts(JUNIT_REQUIRES).each { |task| task.invoke }.map(&:to_s) + @junit_artifacts ||= Buildr.artifacts(JUNIT_REQUIRES).each { |task| task.invoke }.map(&:to_s) end end # See Java#java. @@ -358,9 +362,8 @@ end end end - class Project - include Java - end + include Java + end