Class: Bee::Task::Java

Inherits:
Package
  • Object
show all
Defined in:
lib/bee_task_java.rb

Overview

Package for Java tasks.

Constant Summary collapse

SCOPES =

Classpath scopes.

['compile', 'runtime', 'test']

Instance Method Summary collapse

Instance Method Details

#classpath(params) ⇒ Object

Resolve dependencies and generate a classpath for a given dependencies file. Synchronize dependencies with local repository if necessary (default location is ~/.java/dependencies directory).

  • file: Dependencies file to parse (‘dependencies.yml’, ‘project.xml’ or ‘pom.xml’ depending on type). Defaults to ‘dependencies.yml’.

  • type: the dependencies type: ‘bee’, ‘maven1’ or ‘maven2’. Defaults to ‘bee’.

  • scope: the scope of the classpath to build (‘compile’, ‘test’ and so on, as defined in dependencies file).

  • directories: directory or list of directories to add to classpath.

  • classpath: the property name to set with classpath.

  • dependencies: the property name to set with the list of dependencies files.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/bee_task_java.rb', line 453

def classpath(params)
  params_desc = {
    :file         => { :mandatory => false, :type => :string,
                       :default => 'dependencies.yml'},
    :type         => { :mandatory => false, :type => :string,
                       :default => 'bee' },
    :scope        => { :mandatory => false, :type => :string,
                       :default => 'compile' },
    :directories  => { :mandatory => false, :type => :string_or_array },
    :classpath    => { :mandatory => false,  :type => :string },
    :dependencies => { :mandatory => false,  :type => :string }
  }
  check_parameters(params, params_desc)
  file         = params[:file]
  type         = params[:type]
  scope        = params[:scope]
  directories  = params[:directories]
  classpath    = params[:classpath]
  dependencies = params[:dependencies]
  if not SCOPES.include?(scope)
    scopes = SCOPES.map{ |s| "'#{s}'"}.join(', ')
    error "Unknown scope '#{scope}', must be one of #{scopes}"
  end
  puts "Building CLASSPATH for dependencies '#{file}' and scope '#{scope}'..."
  if type == 'bee'
    resolver = Bee::Task::BeeDependencyResolver.new(file, scope, @verbose)
  elsif type == 'maven1'
    resolver = Bee::Task::MavenDependencyResolver.new(file, scope, @verbose)
  elsif type == 'maven2'
    resolver = Bee::Task::Maven2DependencyResolver.new(file, scope, @verbose)
  else
    error "Unknown type, must be 'bee', 'maven1' or 'maven2'"
  end
  if classpath
    path = resolver.classpath
    path += ':' + directories.join(File::PATH_SEPARATOR) if directories
    @build.context.set_property(classpath, path)
  end
  if dependencies
    deps = resolver.dependencies
    deps += directories if directories
    @build.context.set_property(dependencies, deps)
  end
end

#jar(params) ⇒ Object

Generate Jar file.

  • src: directory or list of directories to include to the archive.

  • includes: glob or list of globs for files to include in the Jar file.

  • excludes: glob or list of globs for files to exclude from the Jar file.

  • manifest: manifest file to include in generated Jar file (optional).

  • dest: the Jar file to generate.

  • options: custom options to use on command line (optional).

Example

- java.jar:
    src: "#{build_dir}/classes"
    dest: "#{build_dir}/myjar.jar"


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bee_task_java.rb', line 137

def jar(params)
  # check parameters
  params_desc = {
    :src      => { :mandatory => false, :type => :string_or_array },
    :includes => { :mandatory => false, :type => :string_or_array },
    :excludes => { :mandatory => false, :type => :string_or_array },
    :manifest => { :mandatory => false, :type => :string },
    :dest     => { :mandatory => true,  :type => :string },
    :options  => { :mandatory => false, :type => :string }
  }
  check_parameters(params, params_desc)
  src      = params[:src]
  includes = params[:includes]
  excludes = params[:excludes]
  manifest = params[:manifest]
  dest     = params[:dest]
  options  = params[:options]
  error "jar 'src' or 'includes' parameter must be specified" unless
    src or includes
  error "jar 'manifest' parameter must be an existing file" unless 
    not manifest or File.exists?(manifest)
  # select directories to include in the Jar file
  directories = []
  if src
    dirs = Array(src)
    for dir in dirs
      directories += Dir.glob(dir)
    end
  end
  # select files to include in the Jar file
  files = []
  if includes
    files = filter_files(nil, includes, excludes)
    files.map! { |file| "\"#{file}\"" }
  end
  # run command line
  puts "Processing Jar archive '#{dest}'"
  command = "jar c"
  command += "v" if @verbose
  command += "m" if manifest
  command +="f "
  command += "\"#{manifest}\" " if manifest
  command += "\"#{dest}\" "
  command += "#{options} " if options
  if directories.length > 0
    for dir in directories
      command += "-C \"#{dir}\" ."
    end
  end
  if files.length > 0
    command += "#{files.join(' ')}"
  end
  puts "Running command '#{command}'" if @verbose
  ok = system(command)
  error "Error generating Jar archive" unless ok
end

#java(params) ⇒ Object

Run a Java class.

  • main: main Java class to run.

  • classpath: the classpath as defined on javac command line (optional, if dependencies not set).

  • dependencies: the list of files to include into classpath (optional, if classpath not set).

  • jar: Jar file to launch.

  • server: run server version of the VM, must be true or false (defaults to false).

  • properties: system properties to pass to virtual machine, must be a hash, such as { foo: bar } (optional).

  • assertions: enables assertions, must be true or false (defaults to true).

  • verbose: tells if we should generate verbose output, must be true or false (defaults to false).

  • options: custom options to use on command line (optional).

  • arguments: arguments to pass on Java program command line.

Example

- java.java:
    main: test/Test
    classpath: build/classes


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/bee_task_java.rb', line 218

def java(params)
  # check parameters
  params_desc = {
    :main         => { :mandatory => false, :type => :string },
    :classpath    => { :mandatory => false, :type => :string },
    :dependencies => { :mandatory => false, :type => :string_or_array },
    :jar          => { :mandatory => false, :type => :string },
    :server       => { :mandatory => false, :type => :boolean,
                       :default => false },
    :properties   => { :mandatory => false, :type => :hash },
    :assertions   => { :mandatory => false, :type => :boolean,
                       :default => true },
    :verbose      => { :mandatory => false, :type => :boolean,
                       :default => false },
    :options      => { :mandatory => false, :type => :string },
    :arguments    => { :mandatory => false, :type => :string }
  }
  check_parameters(params, params_desc)
  main         = params[:main]
  classpath    = params[:classpath]
  dependencies = params[:dependencies]
  jar          = params[:jar]
  server       = params[:server]
  properties   = params[:properties]
  assertions   = params[:assertions]
  verbose      = params[:verbose]
  options      = params[:options]
  arguments    = params[:arguments]
  error "jar 'classpath', 'dependencies' or 'jar' parameters must be set" if
    not classpath and not dependencies and not jar
  # build classpath
  error "Only one of classpath or dependencies may be set" if
    classpath and dependencies
  path = classpath if classpath
  path = build_classpath(dependencies) if dependencies
  # run command line
  if main
    puts "Running java class '#{main}'"
  else
    puts "Running jar file '#{jar}'"
  end
  command = "java "
  command += "-server " if server
  if properties
    for key in properties.keys
      command += "-D#{key}=#{properties[key]} "
    end
  end
  command += "-enableassertions " if assertions
  command += "-verbose " if verbose
  command += "#{options} " if options
  command += "-jar #{jar} " if jar
  command += "-classpath #{path} " if path
  command += "\"#{main}\"" if main
  command += " #{arguments}" if arguments
  puts "Running command '#{command}'" if @verbose
  ok = system(command)
  error "Error running java" unless ok
end

#javac(params) ⇒ Object

Compile Java source files.

  • src: directory or list of directories containing Java source files to compile.

  • dest: destination directory for generated class files.

  • classpath: the classpath as defined on javac command line (optional, if dependencies not set).

  • dependencies: the list of files to include into classpath (optional, if classpath not set).

  • deprecation: tells if we should show deprecation description, should be true or false (defaults to false).

  • encoding: specify encoding for source files (defaults to platform encoding).

  • debug: tells if we should generate debug information, must be true or false (defaults to false).

  • nowarn: tells if we should ignore warnings, must be true or false (defaults to false).

  • verbose: tells if we should generate verbose output, must be true or false (defaults to false).

  • options: custom options to use on command line (optional).

Example

- java.javac:
    src:       "test"
    dest:      "#{build_dir}/test_classes"
    classpath: ["#{build_dir}/classes", "lib/*.jar"]


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bee_task_java.rb', line 59

def javac(params)
  # check parameters
  params_desc = {
    :src          => { :mandatory => true,  :type => :string_or_array },
    :dest         => { :mandatory => true,  :type => :string },
    :classpath    => { :mandatory => false, :type => :string },
    :dependencies => { :mandatory => false, :type => :string_or_array },
    :deprecation  => { :mandatory => false, :type => :boolean,
                       :default => false },
    :encoding     => { :mandatory => false, :type => :string },
    :debug        => { :mandatory => false, :type => :boolean,
                       :default => false },
    :nowarn       => { :mandatory => false, :type => :boolean,
                       :default => false },
    :verbose      => { :mandatory => false, :type => :boolean,
                       :default => false },
    :options      => { :mandatory => false, :type => :string }
  }
  check_parameters(params, params_desc)
  src          = params[:src]
  dest         = params[:dest]
  classpath    = params[:classpath]
  dependencies = params[:dependencies]
  deprecation  = params[:deprecation]
  encoding     = params[:encoding]
  debug        = params[:debug]
  nowarn       = params[:nowarn]
  verbose      = params[:verbose]
  options      = params[:options]
  # build the list of java source files
  src = Array(src)
  files = []
  for dir in src
    files += Dir.glob("#{dir}/**/*.java")
  end
  files.map! { |file| "\"#{file}\"" }
  files.uniq!
  # build classpath
  error "Only one of classpath or dependencies may be set" if
    classpath and dependencies
  path = classpath if classpath
  path = build_classpath(dependencies) if dependencies
  # make destination directory if it doesn't exist
  FileUtils.makedirs(dest) if not File.exists?(dest)
  error "javac 'dest' parameter must be a writable directory" unless
    File.directory?(dest) and File.writable?(dest)
  # run javac command
  puts "Compiling #{files.length} Java source file(s)"
  command = "javac "
  command += "-classpath #{path} " if path
  command += "-d \"#{dest}\" "
  command += "-deprecation " if deprecation
  command += "-encoding #{encoding} " if encoding
  command += "-g " if debug
  command += "-nowarn " if nowarn
  command += "-verbose " if verbose
  command += "#{options} " if options
  command += "#{files.join(' ')}"
  puts "Running command '#{command}'" if @verbose
  ok = system(command)
  error "Error compiling Java source files" unless ok
end

#javadoc(params) ⇒ Object

Generate Java documentation. Parameters is a hash with following entries:

  • src: directory or list of directories for Java source files to document.

  • includes: glob or list of globs for files to document.

  • excludes: glob or list of globs for files to exclude from documentation.

  • dest: destination directory for generated documentation.

  • verbose: tells if we should generate verbose output, must be true or false (defaults to false).

  • options: custom options to use on command line (optional).

Example

- java.javadoc:
    src: :src
    dest: "#{build_dir}/api"


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/bee_task_java.rb', line 296

def javadoc(params)
  # check parameters
  params_desc = {
    :src      => { :mandatory => false, :type => :string_or_array },
    :includes => { :mandatory => false, :type => :string_or_array },
    :excludes => { :mandatory => false, :type => :string_or_array },
    :dest     => { :mandatory => true,  :type => :string },
    :verbose  => { :mandatory => false, :type => :boolean,
                   :default => false },
    :options  => { :mandatory => false, :type => :string }
  }
  check_parameters(params, params_desc)
  src      = params[:src]
  includes = params[:includes]
  excludes = params[:excludes]
  dest     = params[:dest]
  verbose  = params[:verbose]
  options  = params[:options]
  error "javadoc 'src' or 'includes' parameter must be specified" unless
    src or includes
  # select files to document
  files = []
  if src
    dirs = Array(src)
    for dir in dirs
      files += Dir.glob("#{dir}/**/*.java")
    end
  end
  if includes
    files = filter_files(nil, includes, excludes)
  end
  files.map! { |file| "\"#{file}\"" }
  # run command line
  puts "Running javadoc on #{files.length} Java source file(s)"
  command = "javadoc -d \"#{dest}\" "
  command += "-quiet " if not verbose
  command += "#{options} " if options
  command += "#{files.join(' ')}"
  puts "Running command '#{command}'" if @verbose
  ok = system(command)
  error "Error running javadoc" unless ok
end

#junit(params) ⇒ Object

Run Junit tests.

  • src: directory or list of directories of Java source files for tests to run.

  • includes: glob or list of globs for tests to run within source directory(ies) (defaults to ‘**/*Test.java’).

  • excludes: glob or list of globs for tests to exclude within source directory(ies) (optional).

  • classpath: the list of directories and files to include into classpath. Note that this should include Jar file for JUnit, class files for classes under test and unit tests themselves.

  • skip: tells if we should skip test. Optional, defaults to false.

  • options: custom options to use on command line (optional).

  • version: the JUnit version (defaults to 4 and later). This may be important for 3 and earlier versions where runner is in ‘junit.runner’ package instead of ‘org.junit.runner’. Note that for JUnit 3 and earlier, JUnit test runner will run once for each test class.

Example

- java.junit:
    src:       :test_src
    classpath: [:junit_jar, :classes, :test-classes]


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/bee_task_java.rb', line 363

def junit(params)
  # check parameters
  params_desc = {
    :src          => { :mandatory => true,  :type => :string_or_array },
    :includes     => { :mandatory => false, :type => :string_or_array,
                       :default => '**/*Test.java' },
    :excludes     => { :mandatory => false, :type => :string_or_array },
    :classpath    => { :mandatory => false, :type => :string },
    :dependencies => { :mandatory => false, :type => :string_or_array },
    :skip         => { :mandatory => false, :type => :boolean,
                       :default => false },
    :options      => { :mandatory => false, :type => :string },
    :version      => { :mandatory => false, :type => :string,
                       :default => '4' }
  }
  check_parameters(params, params_desc)
  src          = params[:src]
  includes     = params[:includes]
  excludes     = params[:excludes]
  classpath    = params[:classpath]
  dependencies = params[:dependencies]
  skip         = params[:skip]
  options      = params[:options]
  version      = params[:version]
  for dir in src
    error "junit 'src' directory was not found" unless
      File.exists?(dir) and File.directory?(dir)
  end
  if not skip
    # build classpath
    error "Only one of classpath or dependencies may be set" if
      classpath and dependencies
    path = classpath if classpath
    path = build_classpath(dependencies) if dependencies
    # select test files to run
    files = []
    for dir in src
      files += filter_files(dir, includes, excludes)
    end
    files.uniq!
    classes = files.map do |file|
      file = file[0, file.rindex('.')] if File.extname(file).length > 0
      file.gsub!(/\//, '.')
      "\"#{file}\""
    end
    # run command line
    major_version = version.split('.')[0].to_i
    if major_version > 3
      main_class = 'org.junit.runner.JUnitCore'
      puts "Running JUnit on #{files.length} test file(s)"
      command = "java "
      command += "#{options} " if options
      command += "-classpath #{path} " if path
      command += "#{main_class} #{classes.join(' ')}"
      puts "Running command '#{command}'" if @verbose
      ok = system(command)
      error "Error running JUnit" unless ok
    else
      main_class = 'junit.textui.TestRunner'
      for test_class in classes
        puts "Running JUnit on '#{test_class}' test class"
        command = "java "
        command += "#{options} " if options
        command += "-classpath #{path} " if path
        command += "#{main_class} #{test_class}"
        puts "Running command '#{command}'" if @verbose
        ok = system(command)
        error "Error running JUnit" unless ok
      end
    end
    # run command line
  else
    puts "Skipping unit tests!!!"
  end
end