Class: Warbler::Traits::War
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
#config
Class Method Summary
collapse
Instance Method Summary
collapse
#apply_pathmaps
Methods included from RakeHelper
extended, included
#add_init_load_path, #add_main_rb, included, #initialize, #jruby_jars, #update_gem_path
Class Method Details
.conflicts ⇒ Object
24
25
26
|
# File 'lib/warbler/traits/war.rb', line 24
def self.conflicts
[ Traits::Jar ]
end
|
Instance Method Details
#add_executables(jar) ⇒ Object
118
119
120
121
122
|
# File 'lib/warbler/traits/war.rb', line 118
def add_executables(jar)
webserver = WEB_SERVERS[config.webserver.to_s]
webserver.add(jar)
add_runnables jar, webserver.main_class || 'WarMain'
end
|
#add_gemjar(jar) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/warbler/traits/war.rb', line 124
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.
90
91
92
93
|
# File 'lib/warbler/traits/war.rb', line 90
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_runnables(jar, main_class = 'WarMain') ⇒ Object
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/warbler/traits/war.rb', line 107
def add_runnables(jar, main_class = 'WarMain')
main_class = main_class.sub('.class', '') unless config.manifest_file
manifest = Warbler::Jar::DEFAULT_MANIFEST.chomp + "Main-Class: #{main_class}\n"
jar.files['META-INF/MANIFEST.MF'] = StringIO.new(manifest)
end
[ 'JarMain', 'WarMain', main_class ].uniq.each do |klass|
jar.files["#{klass}.class"] = jar.entry_in_jar(WARBLER_JAR, "#{klass}.class")
end
end
|
#add_webxml(jar) ⇒ Object
Add web.xml and other WEB-INF configuration files from
config.webinf_files to the war file.
97
98
99
100
101
102
103
104
105
|
# File 'lib/warbler/traits/war.rb', line 97
def add_webxml(jar)
config.webinf_files.each do |wf|
if wf =~ /\.erb$/
jar.files[apply_pathmaps(config, wf, :webinf)] = jar.expand_erb(wf, config)
else
jar.files[apply_pathmaps(config, wf, :webinf)] = wf
end
end
end
|
39
40
41
|
# File 'lib/warbler/traits/war.rb', line 39
def after_configure
update_gem_path(DEFAULT_GEM_PATH)
end
|
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/warbler/traits/war.rb', line 28
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_files ⇒ Object
74
75
76
77
78
|
# File 'lib/warbler/traits/war.rb', line 74
def default_jar_files
require 'jruby-jars' unless defined? JRubyJars.core_jar_path
require 'jruby-rack' unless defined? JRubyJars.jruby_rack_jar_path
FileList[JRubyJars.core_jar_path, JRubyJars.stdlib_jar_path, JRubyJars.jruby_rack_jar_path]
end
|
#default_pathmaps ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/warbler/traits/war.rb', line 43
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_files ⇒ Object
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/warbler/traits/war.rb', line 63
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_config ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/warbler/traits/war.rb', line 55
def default_webxml_config
c = WebxmlOpenStruct.new
c.public.root = '/'
c.jndi = nil
c.ignored = %w(jndi booter)
c
end
|
#empty_jar ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/warbler/traits/war.rb', line 160
def empty_jar
@empty_jar ||= begin
t = Tempfile.new(["empty", "jar"])
path = t.path
t.close!
ZipSupport.create(path) do |zipfile|
zipfile.mkdir("META-INF")
zipfile.get_output_stream("META-INF/MANIFEST.MF") {|f| f << ::Warbler::Jar::DEFAULT_MANIFEST }
end
at_exit { File.delete(path) }
path
end
end
|
#move_jars_to_webinf_lib(jar, selector = nil) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/warbler/traits/war.rb', line 138
def move_jars_to_webinf_lib(jar, selector = nil)
return unless selector selector = /.*/ if selector == true default_jar_paths = default_jar_files
default_jars = default_jar_paths.map { |file| File.basename(file) }
jar.files.keys.select { |k| k =~ /^WEB-INF\/.*\.jar$/ }.each do |k|
if k.start_with?('WEB-INF/lib/') if default_jars.include? k.sub('WEB-INF/lib/', '')
jar.files.delete(k) unless selector =~ File.basename(k)
end
next
end
next unless selector =~ File.basename(k)
next if default_jar_paths.include?(jar.files[k])
name = k.sub('WEB-INF', '')[1..-1].gsub(/[\/\\]/, '-')
jar.files["WEB-INF/lib/#{name}"] = jar.files[k]
jar.files[k] = empty_jar
end
end
|
#update_archive(jar) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/warbler/traits/war.rb', line 80
def update_archive(jar)
add_public_files(jar)
add_webxml(jar)
move_jars_to_webinf_lib(jar, config.move_jars_to_webinf_lib)
add_runnables(jar) if config.features.include?("runnable")
add_executables(jar) if config.features.include?("executable")
add_gemjar(jar) if config.features.include?("gemjar")
end
|