Author: vborja
Date: Sun Apr 6 20:36:38 2008
New Revision: 645356
URL: http://svn.apache.org/viewvc?rev=645356&view=rev
Log:
BUILDR-59
Modified:
incubator/buildr/trunk/lib/buildr/java/packaging.rb
incubator/buildr/trunk/spec/java_packaging_spec.rb
Modified: incubator/buildr/trunk/lib/buildr/java/packaging.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/packaging.rb?rev=645356&r1=645355&r2=645356&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/packaging.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/packaging.rb Sun Apr 6 20:36:38 2008
@@ -40,35 +40,29 @@
end
end
- # WithManifest.from_file(path) => [Hash]
- #
- # Return an array of hashes built from the file manifest.
- # Each hash is a manifest section.
+ # from_file(path) => Array of hashes (sections of manifest)
#
# The array returned by this function can be feed to
# WithManifest#manifest_lines_from
# to obtain the formatted manifest content.
def from_file(file)
Zip::ZipFile.open(file.to_s) do |zip|
- begin
-
zip.file.read('META-INF/MANIFEST.MF').split(MANIFEST_SECTION_SEP).
- reject { |s| s.chomp == "" }.map do |section
- section.split(MANIFEST_LINE_SEP).inject([]) { |merged, line|
- if line[0] == 32
- merged.last << line[1..-1]
- else
- merged << line
- end
- merged
- }.map { |line| line.split(/:\s+/) }.
- inject({}) { |map, (name, value)| map.merge(name => value)
}
- end
- rescue Errno::ENOENT
- [{}] # manifest with first section empty
+ return [{}] unless zip.get_entry('META-INF/MANIFEST.MF')
+ zip.read('META-INF/MANIFEST.MF').split(MANIFEST_SECTION_SEP).
+ reject { |s| s.chomp == "" }.map do |section|
+ section.split(MANIFEST_LINE_SEP).inject([]) { |merged, line|
+ if line[0] == 32
+ merged.last << line[1..-1]
+ else
+ merged << line
+ end
+ merged
+ }.map { |line| line.split(/:\s+/) }.
+ inject({}) { |map, (name, value)| map.merge(name => value) }
end
end
end
-
+
def manifest_lines_from(arg)
case arg
when Hash
@@ -88,6 +82,19 @@
end
end
+ # update_manifest(file) { |manifest_sections| ... }
+ def update_manifest(file)
+ sections = from_file(file.to_s)
+ result = yield sections
+ Zip::ZipFile.open(file.to_s) do |zip|
+ zip.get_output_stream('META-INF/MANIFEST.MF') do |out|
+ out.write WithManifest.manifest_lines_from(sections).join("\n")
+ out.write "\n"
+ end
+ end
+ result
+ end
+
private
def manifest_wrap_at_72(arg)
#return arg.map { |line| manifest_wrap_at_72(line)
}.flatten.join("\n") if Array === arg
@@ -402,20 +409,17 @@
def component_clone(component)
file(path_to(component[:path],
component[:artifact].to_s.pathmap('%f')) => component[:artifact]) do |task|
- mkpath task.pathmap('%d'), :verbose => false
+ mkpath task.to_s.pathmap('%d'), :verbose => false
cp component[:artifact].to_s, task.to_s, :verbose => false
- sections = WithManifest.from_file(component[:artifact])
- class_path = sections.first['Class-Path'].to_s.split
- included_libs = class_path.map { |fn| File.basename(fn) }
- included_libs += package.path('WEB-INF/lib').sources.map { |fn|
File.basename(fn) }
- # Include all other libraries in the classpath.
- class_path += libs_classpath(component).reject { |path|
included_libs.include?(File.basename(path)) }
- sections.first['Class-Path'] = class_path.join(' ')
- Zip::ZipFile.open(task.to_s) do |zip|
- zip.get_output_stream('META-INF/MANIFEST.MF') do |out|
- out.write WithManifest.manifest_lines_from(sections).join('\n')
- out.write '\n'
+ WithManifest.update_manifest(task) do |sections|
+ class_path = sections.first['Class-Path'].to_s.split
+ included_libs = class_path.map { |fn| fn.pathmap('%f') }
+ Zip::ZipFile.foreach(task.to_s) do |entry|
+ included_libs << entry.name.pathmap('%f') if entry.file? &&
entry.name =~ /^WEB-INF\/lib\/[^\/]+$/
end
+ # Include all other libraries in the classpath.
+ class_path += libs_classpath(component).reject { |path|
included_libs.include?(File.basename(path)) }
+ sections.first['Class-Path'] = class_path.join(' ')
end
end
end
@@ -500,7 +504,7 @@
# return a FileTask to build the ear application.xml file
def descriptor
return @descriptor if @descriptor
- descriptor_path = path_to(:target, "#{id}/META-INF/application.xml")
+ descriptor_path = path_to('META-INF/application.xml')
@descriptor = file(descriptor_path) do |task|
puts "Creating EAR Descriptor: #{task.to_s}" if
Rake.application.options.trace
mkpath File.dirname(task.name), :verbose=>false
Modified: incubator/buildr/trunk/spec/java_packaging_spec.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/java_packaging_spec.rb?rev=645356&r1=645355&r2=645356&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/java_packaging_spec.rb (original)
+++ incubator/buildr/trunk/spec/java_packaging_spec.rb Sun Apr 6 20:36:38 2008
@@ -564,7 +564,7 @@
File.open('tmp.zip', 'wb') do |tmp|
tmp.write ear.file.read(package)
end
- sections = Buildr::Packaging::Java::WithManifest.from_file(package)
+ sections = Buildr::Packaging::Java::WithManifest.from_file('tmp.zip')
yield sections.first['Class-Path'].to_s.split(' ')
end
end
@@ -606,40 +606,32 @@
inspect_ear { |files| files.should include('ejb/foo-1.0.jar') }
end
- it 'should accept an artifact as non-lib component, without updating its
manifest' do
- define 'bar', :version => '1.0' do
- write 'hello', 'world'
- package(:zip).include('hello')
- package(:zip).manifest = {'Class-Path' => 'bar'}
- package(:zip).invoke
- artifact('hello:world:jar:1.0').from(package(:zip).to_s)
+ it 'should not modify original artifact for its components' do
+ define 'one', :version => '1.0' do
+ write 'src/main/resources/one.txt', '1'
package(:jar)
end
- define 'foo', :version => '1.0' do
- package(:ear).add project(:bar).package(:jar)
- package(:ear).add :ejb => 'hello:world:jar:1.0'
- end
- inspect_ear { |files| files.should include('ejb/world-1.0.jar',
'lib/bar-1.0.jar') }
- inspect_classpath 'ejb/world-1.0.jar'do |classpath|
- classpath.should == ['bar'] # should not be updated by EarTask
+
+ define 'two', :version => '1.0' do
+ write 'src/main/resources/two.txt', '2'
+ package(:jar)
end
- end
-
- it 'should accept an artifact clone with component type' do
- define 'foo', :version=>'1.0' do
- write 'src/main/resources/foo', 'true'
- artifact("foo:bar:jar:1.0").from(package(:jar, :id => 'muu').to_s)
- artifact("foo:baz:jar:1.0").from(package(:jar, :id => 'moo').to_s)
- package(:ear).add 'foo:baz:jar:1.0'
- package(:jar, :id =>
:miu).merge(artifact('foo:bar:jar:1.0')).include('**/*')
- package(:ear).add :ejb=> package(:jar, :id => :miu)
- end
- inspect_ear { |files| files.should include('ejb/miu-1.0.jar',
'lib/baz-1.0.jar') }
- inspect_classpath 'ejb/miu-1.0.jar'do |classpath|
- classpath.should include('../lib/baz-1.0.jar')
+
+ define 'foo', :version => '1.0' do
+ package(:ear).add project(:one).package(:jar)
+ package(:ear).add :ejb => project(:two).package(:jar)
+ end
+
+ inspect_ear { |files| files.should include('lib/one-1.0.jar',
'ejb/two-1.0.jar') }
+
+
Buildr::Packaging::Java::WithManifest.from_file(project('one').package(:jar)).first['Class-Path'].should
be_nil
+
Buildr::Packaging::Java::WithManifest.from_file(project('two').package(:jar)).first['Class-Path'].should
be_nil
+
+ inspect_classpath 'ejb/two-1.0.jar' do |classpath|
+ classpath.should include('../lib/one-1.0.jar')
end
end
-
+
it 'should map JARs to /lib directory' do
define 'foo', :version=>'1.0' do
package(:ear) << package(:jar)