Author: vborja
Date: Sun Apr 6 20:36:09 2008
New Revision: 645354
URL: http://svn.apache.org/viewvc?rev=645354&view=rev
Log:
EarTask creates copies of its non-lib components, so that
modifying their manifest can be safe.
Modified:
incubator/buildr/trunk/lib/buildr/core/util.rb
incubator/buildr/trunk/lib/buildr/java/packaging.rb
incubator/buildr/trunk/spec/java_packaging_spec.rb
Modified: incubator/buildr/trunk/lib/buildr/core/util.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/core/util.rb?rev=645354&r1=645353&r2=645354&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/core/util.rb (original)
+++ incubator/buildr/trunk/lib/buildr/core/util.rb Sun Apr 6 20:36:09 2008
@@ -14,6 +14,7 @@
# the License.
require 'rbconfig'
+require 'pathname'
module Buildr
@@ -114,6 +115,11 @@
else
Rake::EARLY
end
+ end
+
+ def relative_path(to, from = ".")
+ to, from = File.expand_path(to, "/"), File.expand_path(from, "/")
+ Pathname.new(to).relative_path_from(Pathname.new(from)).to_s
end
end
end
Modified: incubator/buildr/trunk/lib/buildr/java/packaging.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/packaging.rb?rev=645354&r1=645353&r2=645354&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/packaging.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/packaging.rb Sun Apr 6 20:36:09 2008
@@ -28,14 +28,74 @@
# Adds support for MANIFEST.MF and other META-INF files.
module WithManifest #:nodoc:
- def self.included(base)
- base.class_eval do
- alias :initialize_without_manifest :initialize
- alias :initialize :initialize_with_manifest
+ MANIFEST_HEADER = ['Manifest-Version: 1.0', 'Created-By: Buildr']
+ MANIFEST_LINE_SEP = /\r\n|\n|\r[^\n]/
+ MANIFEST_SECTION_SEP = /(#{MANIFEST_LINE_SEP}){2}/
+
+ class << self
+ def included(base)
+ base.class_eval do
+ alias :initialize_without_manifest :initialize
+ alias :initialize :initialize_with_manifest
+ end
end
- end
- MANIFEST_HEADER = ['Manifest-Version: 1.0', 'Created-By: Buildr']
+ # WithManifest.from_file(path) => [Hash]
+ #
+ # Return an array of hashes built from the file manifest.
+ # Each hash is a manifest section.
+ #
+ # 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
+ end
+ end
+ end
+
+ def manifest_lines_from(arg)
+ case arg
+ when Hash
+ arg.map { |name, value| "#{name}: #{value}" }.sort.
+ map { |line| manifest_wrap_at_72(line) }.flatten
+ when Array
+ arg.map { |section|
+ name = section.has_key?('Name') ? ["Name: #{section['Name']}"]
: []
+ name + section.except('Name').map { |name, value| "#{name}:
#{value}" }.sort + ['']
+ }.flatten.map { |line| manifest_wrap_at_72(line) }.flatten
+ when Proc, Method
+ manifest_lines_from(arg.call)
+ when String
+ arg.split("\n").map { |line| manifest_wrap_at_72(line) }.flatten
+ else
+ fail 'Invalid manifest, expecting Hash, Array, file name/task or
proc/method.'
+ end
+ end
+
+ private
+ def manifest_wrap_at_72(arg)
+ #return arg.map { |line| manifest_wrap_at_72(line)
}.flatten.join("\n") if Array === arg
+ return arg if arg.size < 72
+ [ arg[0..70], manifest_wrap_at_72(' ' + arg[71..-1]) ]
+ end
+
+ end
# Specifies how to create the manifest file.
attr_accessor :manifest
@@ -59,8 +119,8 @@
# Tempfiles gets deleted on garbage collection, so we're going
to hold on to it
# through instance variable not closure variable.
Tempfile.open 'MANIFEST.MF' do |@manifest_tmp|
- lines = String === manifest || Rake::Task === manifest ?
manifest_lines_from(File.read(manifest.to_s)) :
- manifest_lines_from(manifest)
+ lines = String === manifest || Rake::Task === manifest ?
+ WithManifest.manifest_lines_from(File.read(manifest.to_s)) :
WithManifest.manifest_lines_from(manifest)
@manifest_tmp.write((MANIFEST_HEADER + lines).join("\n"))
@manifest_tmp.write "\n"
path('META-INF').include @manifest_tmp.path, :as=>'MANIFEST.MF'
@@ -69,33 +129,6 @@
end
end
- private
-
- def manifest_lines_from(arg)
- case arg
- when Hash
- arg.map { |name, value| "#{name}: #{value}" }.sort.
- map { |line| manifest_wrap_at_72(line) }.flatten
- when Array
- arg.map { |section|
- name = section.has_key?('Name') ? ["Name: #{section['Name']}"] :
[]
- name + section.except('Name').map { |name, value| "#{name}:
#{value}" }.sort + ['']
- }.flatten.map { |line| manifest_wrap_at_72(line) }.flatten
- when Proc, Method
- manifest_lines_from(arg.call)
- when String
- arg.split("\n").map { |line| manifest_wrap_at_72(line) }.flatten
- else
- fail 'Invalid manifest, expecting Hash, Array, file name/task or
proc/method.'
- end
- end
-
- def manifest_wrap_at_72(arg)
- #return arg.map { |line| manifest_wrap_at_72(line)
}.flatten.join("\n") if Array === arg
- return arg if arg.size < 72
- [ arg[0..70], manifest_wrap_at_72(' ' + arg[71..-1]) ]
- end
-
end
class ::Buildr::ZipTask
@@ -305,7 +338,7 @@
@libs, @components = [], []
prepare do
@components.each do |component|
- path(component[:path]).include(component[:artifact])
+ path(component[:path]).include(component[:clone] ||
component[:artifact])
end
path('META-INF').include(descriptor)
end
@@ -354,7 +387,8 @@
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)
- update_classpath(component) unless :lib == type || Artifact ===
artifact
+ component[:clone] = component_clone(component) unless :lib ==
type
+ # update_classpath(component) unless :lib == type || Artifact
=== artifact
@components << component
end
end
@@ -366,12 +400,32 @@
protected
+ 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
+ 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'
+ end
+ end
+ end
+ end
+
def associate(project)
@project = project
end
def path_to(*args) #:nodoc:
- @project.path_to(:target, :ear, *args)
+ @project.path_to(:target, :ear, name.pathmap('%n'), *args)
end
alias_method :_, :path_to
@@ -404,8 +458,7 @@
to = File.join(to[:path].to_s, File.basename(to[:artifact].to_s)) if
to.kind_of?(Hash)
from = from[:path].to_s if from.kind_of?(Hash)
to, from = File.expand_path(to, "/"), File.expand_path(from, "/")
- require 'pathname'
- Pathname.new(to).relative_path_from(Pathname.new(from)).to_s
+ Util.relative_path(to, from)
end
# Classpath of all packages included as libraries (type :lib).
Modified: incubator/buildr/trunk/spec/java_packaging_spec.rb
URL:
http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/java_packaging_spec.rb?rev=645354&r1=645353&r2=645354&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/java_packaging_spec.rb (original)
+++ incubator/buildr/trunk/spec/java_packaging_spec.rb Sun Apr 6 20:36:09 2008
@@ -18,9 +18,6 @@
require File.join(File.dirname(__FILE__), 'packaging_helper')
-MANIFEST_LINE_SEP = /\r\n|\n|\r[^\n]/
-MANIFEST_SECTION_SEP = /(#{MANIFEST_LINE_SEP}){2}/
-
describe Project, '#manifest' do
it 'should include user name' do
ENV['USER'] = 'MysteriousJoe'
@@ -70,22 +67,7 @@
def inspect_manifest
package = project('foo').package(@packaging)
package.invoke
- Zip::ZipFile.open(package.to_s) do |zip|
- sections =
zip.file.read('META-INF/MANIFEST.MF').split(MANIFEST_SECTION_SEP).
- reject { |s| s.chomp == "" }.map do |section|
- section.split(MANIFEST_LINE_SEP).each { |line| line.length.should < 72
}.
- inject([]) { |merged, line|
- if line[0] == 32
- merged.last << line[1..-1]
- else
- merged << line
- end
- merged
- }.map { |line| line.split(/: /) }.
- inject({}) { |map, (name, value)| map.merge(name=>value) }
- end
- yield sections
- end
+ yield Buildr::Packaging::Java::WithManifest.from_file(package)
end
it 'should include default header when no options specified' do
@@ -127,7 +109,7 @@
package_with_manifest 'Foo'=>1, :bar=>'Bar'
package = project('foo').package(@packaging)
package.invoke
- Zip::ZipFile.open(package.to_s) { |zip|
zip.file.read('META-INF/MANIFEST.MF').should =~ /#{MANIFEST_LINE_SEP}$/ }
+ Zip::ZipFile.open(package.to_s) { |zip|
zip.file.read('META-INF/MANIFEST.MF').should =~
/#{Buildr::Packaging::Java::WithManifest::MANIFEST_LINE_SEP}$/ }
end
it 'should break hash manifest lines longer than 72 characters using
continuations' do
@@ -582,21 +564,8 @@
File.open('tmp.zip', 'wb') do |tmp|
tmp.write ear.file.read(package)
end
- Zip::ZipFile.open('tmp.zip') do |zip|
- first_section =
zip.file.read('META-INF/MANIFEST.MF').split(MANIFEST_SECTION_SEP).
- reject { |s| s.chomp == "" }.first.
- split(MANIFEST_LINE_SEP).each { |line| line.length.should < 72 }.
- inject([]) { |merged, line|
- if line[0] == 32
- merged.last << line[1..-1]
- else
- merged << line
- end
- merged
- }.map { |line| line.split(/: /) }.
- inject({}) { |map, (name, value)| map.merge(name=>value) }
- yield first_section['Class-Path'].to_s.split(' ')
- end
+ sections = Buildr::Packaging::Java::WithManifest.from_file(package)
+ yield sections.first['Class-Path'].to_s.split(' ')
end
end