Class: Buildr::Packaging::Java::EarTask

Inherits:
JarTask show all
Defined in:
lib/buildr/java/packaging.rb

Overview

Extend the JarTask to create an EAR file.

The following component types are supported by the EARTask:

  • :war – A J2EE Web Application

  • :ejb – An Enterprise Java Bean

  • :jar – A J2EE Application Client.

  • :lib – An ear scoped shared library (for things like logging,

    spring, etc) common to the ear components
    

The EarTask uses the “Mechanism 2: Bundled Optional Classes” as described on [2]. All specified libraries are added to the EAR archive and the Class-Path manifiest entry is modified for each EAR component. Special care is taken with WebApplications, as they can contain libraries on their WEB-INF/lib directory, libraries already included in a war file are not referenced by the Class-Path entry of the war in order to avoid class collisions

EarTask supports all the same options as JarTask, in additon to these two options:

  • :display_name – The displayname to for this ear on application.xml

  • :map – A Hash used to map component type to paths within the EAR.

    By default each component type is mapped to a directory with the same name,
    for example, EJBs are stored in the /ejb path.  To customize:
                      package(:ear).map[:war] = 'web-applications' 
                      package(:ear).map[:lib] = nil # store shared libraries on root of archive
    

EAR components are added by means of the EarTask#add, EarTask#<<, EarTask#push methods Component type is determined from the artifact’s type.

package(:ear) << project('coolWebService').package(:war)

The << method is just an alias for push, with the later you can add multiple components at the same time. For example..

package(:ear).push 'org.springframework:spring:jar:2.6', 
                             projects('reflectUtils', 'springUtils'),
                             project('coolerWebService').package(:war)

The add method takes a single component with an optional hash. You can use it to override some component attributes.

You can override the component type for a particular artifact. The following example shows how you can tell the EarTask to treat a JAR file as an EJB:

# will add an ejb entry for the-cool-ejb-2.5.jar in application.xml
package(:ear).add 'org.coolguys:the-cool-ejb:jar:2.5', :type=>:ejb
# A better syntax for this is: 
package(:ear).add :ejb=>'org.coolguys:the-cool-ejb:jar:2.5'

By default, every JAR package is assumed to be a library component, so you need to specify the type when including an EJB (:ejb) or Application Client JAR (:jar).

For WebApplications (:war)s, you can customize the context-root that appears in application.xml. The following example also specifies a different directory inside the EAR where to store the webapp.

package(:ear).add project(:remoteService).package(:war), 
                           :path=>'web-services', :context_root=>'/Some/URL/Path'
1

java.sun.com/j2ee/sdk_1.2.1/techdocs/guides/ejb/html/Overview5.html#10106

2

java.sun.com/j2ee/verified/packaging.html

Constant Summary collapse

SUPPORTED_TYPES =
[:war, :ejb, :jar, :rar, :lib]

Instance Attribute Summary collapse

Attributes inherited from ZipTask

#compression_level

Instance Method Summary collapse

Methods inherited from JarTask

#with

Methods inherited from ZipTask

#entries, #entry

Methods inherited from ArchiveTask

#clean, #contain?, #empty?, #exclude, #include, #invoke_prerequisites, #merge, #needed?, #path, #root, #with

Methods inherited from Rake::FileTask

#contain?, #empty?, #exist?

Constructor Details

#initialize(*args) ⇒ EarTask

Returns a new instance of EarTask.



390
391
392
393
394
395
396
397
398
399
400
# File 'lib/buildr/java/packaging.rb', line 390

def initialize(*args)
  super
  @dirs = Hash.new { |h, k| k.to_s }
  @libs, @components = [], []
  prepare do
    @components.each do |component|
      path(component[:path]).include(component[:clone] || component[:artifact])
    end
    path('META-INF').include(descriptor)
  end
end

Instance Attribute Details

#dirsObject

Map from component type to path inside the EAR.



388
389
390
# File 'lib/buildr/java/packaging.rb', line 388

def dirs
  @dirs
end

#display_nameObject

The display-name entry for application.xml



386
387
388
# File 'lib/buildr/java/packaging.rb', line 386

def display_name
  @display_name
end

Instance Method Details

#add(*args) ⇒ Object Also known as: push

Add an artifact to this EAR.



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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/buildr/java/packaging.rb', line 403

def add(*args)
  options = Hash === args.last ? args.pop.clone : {}
  args.flatten!
  args.map! do |pkg|
    case pkg
    when Project
      pkg.packages.select { |pp| JarTask === pp && SUPPORTED_TYPES.include?(pp.type) }
    when Rake::FileTask
      pkg # add the explicitly provided file
    when Hash
      Buildr.artifact(pkg)
    when String
      begin
        Buildr.artifact(pkg)
      rescue # not an artifact spec, it must me a filename
        file(pkg)
      end
    else 
      raise "Invalid EAR component #{pkg.class}: #{pkg}"
    end
  end
  args.flatten!
  args.compact!
  if args.empty?
    raise ":type must not be specified for type=>component argument style" if options.key?(:type)
    raise ":as must not be specified for type=>component argument style" if options.key?(:as)
    comps = {}
    options.delete_if { |k, v| comps[k] = v if SUPPORTED_TYPES.include?(k) }
    raise "You must specify at least one valid component to add" if comps.empty?
    comps.each { |k, v| add(v, {:as => k}.merge(options)) }
  else
    args.each do |artifact|
      type = options[:as] || options[:type]
      unless type
        type = artifact.respond_to?(:type) ? artifact.type : artifact.to_s.pathmap('%x').to_sym
        type = :lib if type == :jar
      end
      raise "Unknown EAR component type: #{type}. Perhaps you may explicity tell what component type to use." unless
        SUPPORTED_TYPES.include?(type)
      component = options.merge(:artifact => artifact, :type => type, 
        :id=>artifact.respond_to?(:to_spec) ? artifact.id : artifact.to_s.pathmap('%n'),
        :path=>options[:path] || dirs[type].to_s)
      component[:clone] = component_clone(component) unless :lib == type
      # update_classpath(component) unless :lib == type || Artifact === artifact
      @components << component
    end
  end
  self
end