Class: Warbler::Traits::War

Inherits:
Object
  • Object
show all
Includes:
Warbler::Trait
Defined in:
lib/warbler/traits/war.rb

Overview

The War trait sets up the layout and generates web.xml for the war project.

Defined Under Namespace

Classes: WebxmlOpenStruct

Constant Summary collapse

DEFAULT_GEM_PATH =
'/WEB-INF/gems'

Instance Attribute Summary

Attributes included from Warbler::Trait

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Warbler::Trait

#add_init_load_path, #add_main_rb, included, #initialize, #update_gem_path

Class Method Details

.detect?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/warbler/traits/war.rb', line 18

def self.detect?
  Traits::Rails.detect? || Traits::Merb.detect? || Traits::Rack.detect?
end

Instance Method Details

#add_executables(jar) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/warbler/traits/war.rb', line 99

def add_executables(jar)
  winstone_type = ENV["WINSTONE"] || "winstone-lite"
  winstone_version = ENV["WINSTONE_VERSION"] || "0.9.10"
  winstone_path = "net/sourceforge/winstone/#{winstone_type}/#{winstone_version}/#{winstone_type}-#{winstone_version}.jar"
  winstone_jar = File.expand_path("~/.m2/repository/#{winstone_path}")
  unless File.exist?(winstone_jar)
    # Not always covered in tests as these lines may not get
    # executed every time if the jar is cached.
    puts "Downloading #{winstone_type}.jar" #:nocov:
    mkdir_p File.dirname(winstone_jar)      #:nocov:
    require 'open-uri'                      #:nocov:
    maven_repo = ENV["MAVEN_REPO"] || "http://repo2.maven.org/maven2" #:nocov:
    open("#{maven_repo}/#{winstone_path}") do |stream| #:nocov:
      File.open(winstone_jar, "wb") do |f| #:nocov:
        while buf = stream.read(4096) #:nocov:
          f << buf                    #:nocov:
        end                           #:nocov:
      end                             #:nocov:
    end                               #:nocov:
  end

  jar.files['META-INF/MANIFEST.MF'] = StringIO.new(Warbler::Jar::DEFAULT_MANIFEST.chomp + "Main-Class: WarMain\n")
  jar.files['WarMain.class'] = jar.entry_in_jar("#{WARBLER_HOME}/lib/warbler_jar.jar", 'WarMain.class')
  jar.files['WEB-INF/winstone.jar'] = winstone_jar
end

#add_gemjar(jar) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/warbler/traits/war.rb', line 125

def add_gemjar(jar)
  gem_jar = Warbler::Jar.new
  gem_path = Regexp::quote(config.relative_gem_path)
  gems = jar.files.select{|k,v| k =~ %r{#{gem_path}/} }
  gems.each do |k,v|
    gem_jar.files[k.sub(%r{#{gem_path}/}, '')] = v
  end
  jar.files["WEB-INF/lib/gems.jar"] = "tmp/gems.jar"
  jar.files.reject!{|k,v| k =~ /#{gem_path}/ || k == "WEB-INF/tmp/gems.jar"}
  mkdir_p "tmp"
  gem_jar.add_manifest
  gem_jar.create("tmp/gems.jar")
end

#add_public_files(jar) ⇒ Object

Add public/static assets to the root of the war file.



82
83
84
85
# File 'lib/warbler/traits/war.rb', line 82

def add_public_files(jar)
  config.public_html.exclude *(config.excludes.to_a)
  config.public_html.map {|f| jar.add_with_pathmaps(config, f, :public_html) }
end

#add_webxml(jar) ⇒ Object

Add web.xml and other WEB-INF configuration files from config.webinf_files to the war file.



89
90
91
92
93
94
95
96
97
# File 'lib/warbler/traits/war.rb', line 89

def add_webxml(jar)
  config.webinf_files.each do |wf|
    if wf =~ /\.erb$/
      jar.files[jar.apply_pathmaps(config, wf, :webinf)] = jar.expand_erb(wf, config)
    else
      jar.files[jar.apply_pathmaps(config, wf, :webinf)] = wf
    end
  end
end

#after_configureObject



33
34
35
# File 'lib/warbler/traits/war.rb', line 33

def after_configure
  update_gem_path(DEFAULT_GEM_PATH)
end

#before_configureObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/warbler/traits/war.rb', line 22

def before_configure
  config.gem_path      = DEFAULT_GEM_PATH
  config.pathmaps      = default_pathmaps
  config.webxml        = default_webxml_config
  config.webinf_files  = default_webinf_files
  config.java_libs     = default_jar_files
  config.public_html   = FileList["public/**/*"]
  config.jar_extension = 'war'
  config.init_contents << "#{config.warbler_templates}/war.erb"
end

#default_jar_filesObject



68
69
70
71
72
# File 'lib/warbler/traits/war.rb', line 68

def default_jar_files
  require 'jruby-jars'
  require 'jruby-rack'
  FileList[JRubyJars.core_jar_path, JRubyJars.stdlib_jar_path, JRubyJars.jruby_rack_jar_path]
end

#default_pathmapsObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/warbler/traits/war.rb', line 37

def default_pathmaps
  p = OpenStruct.new
  p.public_html  = ["%{public/,}p"]
  p.java_libs    = ["WEB-INF/lib/%f"]
  p.java_classes = ["WEB-INF/classes/%p"]
  p.application  = ["WEB-INF/%p"]
  p.webinf       = ["WEB-INF/%{.erb$,}f"]
  p.gemspecs     = ["#{config.relative_gem_path}/specifications/%f"]
  p.gems         = ["#{config.relative_gem_path}/gems/%p"]
  p
end

#default_webinf_filesObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/warbler/traits/war.rb', line 57

def default_webinf_files
  webxml = if File.exist?("config/web.xml")
    "config/web.xml"
  elsif File.exist?("config/web.xml.erb")
    "config/web.xml.erb"
  else
    "#{WARBLER_HOME}/web.xml.erb"
  end
  FileList[webxml]
end

#default_webxml_configObject



49
50
51
52
53
54
55
# File 'lib/warbler/traits/war.rb', line 49

def default_webxml_config
  c = WebxmlOpenStruct.new
  c.public.root = '/'
  c.jndi = nil
  c.ignored = %w(jndi booter)
  c
end

#update_archive(jar) ⇒ Object



74
75
76
77
78
79
# File 'lib/warbler/traits/war.rb', line 74

def update_archive(jar)
  add_public_files(jar)
  add_webxml(jar)
  add_executables(jar) if config.features.include?("executable")
  add_gemjar(jar) if config.features.include?("gemjar")
end