Module: Java::Commands

Defined in:
lib/buildr/java/commands.rb

Overview

JDK commands: java, javac, javadoc, etc.

Class Method Summary collapse

Class Method Details

.java(*args, &block) ⇒ Object

:call-seq:

java(class, *args, options?)

Runs Java with the specified arguments.

Each argument should be provided as separate array value, e.g.

java("-jar", "yuicompressor-2.4.2.jar", "--type","css",
     "src/main/webapp/styles/styles-all.css",
     "-o", "src/main/webapp/styles/styles-all-min.css")

The last argument may be a Hash with additional options:

  • :dir – The working directory from which to execute task..

  • :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.

  • :pathing_jar – If true, forces the use of a “pathing” jar, false disables. Nil

    will default to using a "pathing" jar under windows with long classpaths.
    See http://stackoverflow.com/questions/201816/how-to-set-a-long-java-classpath-in-msdos-windows
    


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/buildr/java/commands.rb', line 51

def java(*args, &block)
  options = Hash === args.last ? args.pop : {}
  options[:verbose] ||= trace?(:java)
  rake_check_options options, :classpath, :java_args, :properties, :name, :verbose, :dir, :pathing_jar

  name = options[:name]
  if name.nil?
    name = "java #{args.first}"
  end

  cmd_args = []
  if options[:dir]
    cmd_args << "cd '#{options[:dir]}' && "
  end
  cmd_args << path_to_bin('java')
  cp = classpath_from(options[:classpath])

  unless cp.empty?
    if options[:pathing_jar] == true
      paths = cp.map do |c|
        File.directory?(c) && !c.end_with?('/') ? "#{c}/" : c.to_s
      end
      manifest = Buildr::Packaging::Java::Manifest.new([{'Class-Path' => paths.map{|p| CGI.escape(p)}.join(" ")}])
      tjar = Tempfile.new(%w[javacmd .jar])
      Zip::OutputStream.open(tjar.path) do |zos|
        zos.put_next_entry('META-INF/MANIFEST.MF')
        zos.write manifest.to_s
        zos.write "\n"
      end
      tjar.close

      cmd_args << '-classpath' << tjar.path
    else
      cmd_args << '-classpath' << cp.join(File::PATH_SEPARATOR)
    end
  end
  options[:properties].each { |k, v| cmd_args << "-D#{k}=#{v}" } if options[:properties]
  cmd_args += (options[:java_args] || (ENV['JAVA_OPTS'] || ENV['JAVA_OPTIONS']).to_s.split).flatten
  cmd_args += args.flatten.compact

  tmp = nil
  begin
    unless Buildr.application.options.dryrun
      info "Running #{name}" if name && options[:verbose]
      block = lambda { |ok, res| fail "Failed to execute #{name}, see errors above" unless ok } unless block
      sh(*cmd_args) do |ok, ps|
        block.call ok, ps
      end
    end
  ensure
    unless tmp.nil?
      tmp.close
      tmp.unlink
    end
  end
end

.javac(*args, &block) ⇒ Object

:call-seq:

javac(*files, options)

Runs Javac with the specified arguments.

The last argument may be a Hash with additional options:

  • :output – Target directory for all compiled class files.

  • :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.

  • :processor_path – Annotation processor path. These are all expanded into artifacts, and all tasks are invoked.

  • :processor_options – Annotation processor options.

  • :javac_args – Any additional arguments to pass (e.g. -extdirs, -encoding)

  • :name – Shows this name, otherwise shows the working directory.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/buildr/java/commands.rb', line 121

def javac(*args, &block)
  options = Hash === args.last ? args.pop : {}
  rake_check_options options, :classpath, :sourcepath, :output, :javac_args, :name, :processor, :processor_path, :processor_options

  processor = !!options[:processor]
  processor_options = options[:processor_options] || {}

  files = args.flatten.each { |f| f.invoke if f.respond_to?(:invoke) }.map(&:to_s).
    collect { |arg| File.directory?(arg) ? FileList["#{File.expand_path(arg)}/**/*.java"] : File.expand_path(arg) }.flatten
  name = options[:name] || Dir.pwd

  cmd_args = []
  cmd_args << path_to_bin('javac')
  cp = classpath_from(options[:classpath])
  cmd_args << '-classpath' << cp.join(File::PATH_SEPARATOR) unless cp.empty?
  cmd_args << '-sourcepath' << [options[:sourcepath]].flatten.join(File::PATH_SEPARATOR) if options[:sourcepath]
  if processor
    processor_path = classpath_from(options[:processor_path])
    cmd_args << '-processorpath' << processor_path.join(File::PATH_SEPARATOR) unless processor_path.empty?
    processor_options.each do |k, v|
      cmd_args << "-A#{k}=#{v}"
    end
  else
    cmd_args << '-proc:none'
  end
  cmd_args << '-d' << File.expand_path(options[:output].to_s) if options[:output]
  cmd_args += options[:javac_args].flatten if options[:javac_args]
  Tempfile.open('javac') do |tmp|
    tmp.write files.join(' ')
    cmd_args << "@#{tmp.path}"
  end
  unless Buildr.application.options.dryrun
    mkdir_p options[:output] if options[:output]
    info "Compiling #{files.size} source files in #{name}"
    block = lambda { |ok, res| fail 'Failed to compile, see errors above' unless ok } unless block
    sh(*cmd_args) do |ok, ps|
      block.call ok, ps
    end
  end
end

.javadoc(*args, &block) ⇒ Object

:call-seq:

javadoc(*files, options, &block)

Runs Javadocs with the specified files and options.

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.

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.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/buildr/java/commands.rb', line 178

def javadoc(*args, &block)
  options = Hash === args.last ? args.pop : {}
  fail 'No output defined for javadoc' if options[:output].nil?
  options[:output] = File.expand_path(options[:output].to_s)
  cmd_args = []
  cmd_args << path_to_bin('javadoc')
  cmd_args << '-d' << options[:output]
  cmd_args << (trace?(:javadoc) ? '-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 Hash
        value.each { |k,v| cmd_args << "-#{key}" << k.to_s << v.to_s }
      else
        cmd_args += Array(value).map { |item| ["-#{key}", item.to_s] }.flatten
      end
    end
  [: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
  files = args.each {|arg| arg.invoke if arg.respond_to?(:invoke)}.collect {|arg| arg.is_a?(Project) ? arg.compile.sources.collect{|dir| Dir["#{File.expand_path(dir.to_s)}/**/*.java"]} : File.expand_path(arg.to_s) }
  cmd_args += files.flatten.uniq.map(&:to_s)
  name = options[:name] || Dir.pwd
  unless Buildr.application.options.dryrun
    info "Generating Javadoc for #{name}"
    trace(cmd_args.join(' '))
    block = lambda { |ok, res| fail 'Failed to generate Javadocs, see errors above' unless ok } unless block
    sh(*cmd_args) do |ok, ps|
      block.call ok, ps
    end
  end
end

.path_to_bin(name = nil) ⇒ Object

:call-seq:

path_to_bin(cmd?) => path

Returns the path to the specified Java command (with no argument to java itself).



223
224
225
226
227
228
# File 'lib/buildr/java/commands.rb', line 223

def path_to_bin(name = nil)
  home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.'
  bin = File.expand_path(File.join(home, 'bin'))
  fail 'JAVA_HOME environment variable does not point to a valid JRE/JDK installation.' unless File.exist? bin
  File.expand_path(File.join(bin, name.to_s))
end