lib/java/packaging.rb in buildr-0.20.0 vs lib/java/packaging.rb in buildr-0.21.0
- old
+ new
@@ -21,10 +21,11 @@
#
# The manifest attribute specifies how to create the MANIFEST.MF file.
# * A hash of manifest properties (name/value pairs).
# * An array of hashes, one for each section of the manifest.
# * A string providing the name of an existing manifest file.
+ # * A file task can be used the same way.
# * Proc or method called to return the contents of the manifest file.
# * False to not generate a manifest file.
#
# The meta-inf attribute lists one or more files that should be copied into
# the META-INF directory.
@@ -48,45 +49,43 @@
def []=(key, value) #:nodoc:
if key.to_sym == :manifest
self.manifest = value
elsif key.to_sym == :meta_inf
- self.meta_inf = value
+ self.meta_inf = [value].flatten
else
super key, value
end
value
end
def prerequisites() #:nodoc:
super + [ String === manifest ? file(manifest) : nil ].compact +
- meta_inf.map { |file| String === file ? file(file) : file }
+ [meta_inf].flatten.map { |file| String === file ? file(file) : file }
end
protected
def create(zip) #:nodoc:
- meta_inf.map(&:to_s).uniq.each { |file| zip.add "META-INF/#{File.basename(file)}", file }
+ [meta_inf].flatten.map(&:to_s).uniq.each { |file| zip.add "META-INF/#{File.basename(file)}", file }
unless manifest == false
zip.file.open("META-INF/MANIFEST.MF", "w") do |output|
- output.write MANIFEST_HEADER
+ output << MANIFEST_HEADER
if manifest
case manifest
when Hash
- output.write manifest.map { |pair| pair.join(": ") }.sort.join("\n")
+ output << manifest.map { |pair| pair.map(&:to_s).join(": ") }.sort.join("\n")
when Array
- manifest.each do |section|
- output.write "\n"
- output.write section.map { |pair| pair.join(": ") }.sort.join("\n")
- end
+ output << manifest.reject { |section| section.empty? }.map { |section|
+ section.map { |pair| pair.join(": ") }.sort.join("\n").concat("\n")
+ }.join("\n")
when Proc, Method
- output.write manifest.call
- when String
- output.write File.read(manifest)
+ output << manifest.call
+ when String, Task
+ output << File.read(manifest.to_s)
end
end
- output.write "\n"
end
end
super zip
end
@@ -122,40 +121,47 @@
end
class Project
+ # Options accepted by #package method for all package types.
+ PACKAGE_OPTIONS = [:group, :id, :version, :type, :classifier]
+
+ # The project's identifier. Same as the project name, with colons replaced by dashes.
+ # The ID for project foo:bar is foo-bar.
+ attr_reader :id
+ def id()
+ name.gsub(":", "-")
+ end
+
# Group used for packaging. Inherited from parent project. Defaults to the top-level project name.
attr_accessor :group
inherited_attr(:group) { |project| project.name }
+
# Version used for packaging. Inherited from parent project.
attr_accessor :version
inherited_attr :version
- # Manifest used for packaging. Inherited from parent project. The default value
- # is a hash that includes the Build-By, Build-Jdk and Implementation-Version values.
- # The later is taken from the project's version number.
+
+ # Manifest used for packaging. Inherited from parent project. The default value is a hash that includes
+ # the Build-By, Build-Jdk, Implementation-Title and Implementation-Version values.
+ # The later are taken from the project's comment (or name) and version number.
attr_accessor :manifest
inherited_attr :manifest do |project|
manifest = { "Build-By"=>ENV['USER'], "Build-Jdk"=>Java.version }
+ manifest["Implementation-Title"] = self.comment || self.name
manifest["Implementation-Version"] = project.version if project.version
manifest
end
+
# Files to always include in the package META-INF directory. The default value include
# the LICENSE file if one exists in the project's base directory.
attr_accessor :meta_inf
inherited_attr(:meta_inf) do |project|
license = project.file("LICENSE")
File.exist?(license.to_s) ? [license] : []
end
- # The project's identifier. Same as the project name, with colons replaced by dashes.
- # The ID for project foo:bar is foo-bar.
- attr_reader :id
- def id()
- name.gsub(":", "-")
- end
-
# :call-seq:
# package(type, options?) => task
#
# Defines and returns a package created by this project.
#
@@ -219,20 +225,20 @@
#
# If you want to add additional packaging types, implement a method with the name package_as_[type]
# that accepts two arguments, the file name and a hash of options. The method must yield to the
# block with the package only when first called to define the package, and must return the package
# from each call.
- def package(type, options = nil)
+ def package(type = :jar, options = nil)
options = options.nil? ? {} : options.dup
options[:id] ||= self.id
options[:group] ||= self.group
options[:version] ||= self.version
options[:type] = type
file_name = path_to("target", Artifact.hash_to_file_name(options))
packager = method("package_as_#{type}") rescue
- fail("Do not know how to create a package of type #{:type}")
+ fail("Don't know how to create a package of type #{type}")
packager.call(file_name, options) do |package|
# Make it an artifact using the specifications, and tell it how to create a POM.
package.extend ActsAsArtifact
package.send :apply_spec, options
package.pom.enhance do |pom|
@@ -297,29 +303,33 @@
protected
def package_as_jar(file_name, options) #:nodoc:
unless Rake::Task.task_defined?(file_name)
+ fu_check_options options, *PACKAGE_OPTIONS + [:manifest, :meta_inf, :include]
Java::Packaging::JarTask.define_task(file_name).tap do |jar|
- jar.manifest = options[:manifest] || manifest.merge("Implementation-Title"=>self.comment)
+ jar.manifest = options.has_key?(:manifest) ? options[:manifest] : manifest
jar.meta_inf = options[:meta_inf] || meta_inf
if options[:include]
jar.include options[:include]
else
# Can only decide on this once we're done configuring the compile task.
enhance { jar.include compile.target, :as=>"." }
end
yield jar
end
+ else
+ fu_check_options options, *PACKAGE_OPTIONS
end
file(file_name)
end
def package_as_war(file_name, options) #:nodoc:
unless Rake::Task.task_defined?(file_name)
+ fu_check_options options, *PACKAGE_OPTIONS + [:manifest, :meta_inf, :classes, :libs, :include]
Java::Packaging::WarTask.define_task(file_name).tap do |war|
- war.manifest = options[:manifest] || manifest.merge("Implementation-Title"=>self.comment)
+ war.manifest = options.has_key?(:manifest) ? options[:manifest] : manifest
war.meta_inf = options[:meta_inf] || meta_inf
# Add libraries in WEB-INF lib, and classes in WEB-INF classes
if options[:classes]
war.with :classes=>options[:classes]
else
@@ -338,21 +348,26 @@
elsif File.exist?(path_to("src/main/webapp"))
war.include path_to("src/main/webapp"), :as=>"."
end
yield war
end
+ else
+ fu_check_options options, *PACKAGE_OPTIONS
end
file(file_name)
end
def package_as_zip(file_name, options) #:nodoc:
unless Rake::Task.task_defined?(file_name)
+ fu_check_options options, *PACKAGE_OPTIONS + [:include]
ZipTask.define_task(file_name).tap do |zip|
if options[:include]
zip.include options[:include]
end
yield zip
end
+ else
+ fu_check_options options, *PACKAGE_OPTIONS
end
file(file_name)
end
end