lib/java/packaging.rb in buildr-0.16.0 vs lib/java/packaging.rb in buildr-0.18.0

- old
+ new

@@ -1,49 +1,54 @@ module Buildr module Java module Packaging - MANIFEST_HEADER = "Manifest-Version: 1.0\nCreated-By: Ruby Build System" + MANIFEST_HEADER = "Manifest-Version: 1.0\nCreated-By: Buildr\n" class JarTask < ZipTask attr_accessor :manifest + attr_accessor :meta_inf def initialize(*args) super @manifest = true end def []=(key, value) if key.to_sym == :manifest self.manifest = value + elsif key.to_sym == :meta_inf + self.meta_inf = value else super key, value end value end protected def create(zip) - if manifest - zip.mkdir "META-INF" + zip.mkdir "META-INF" + meta_inf.each { |file| zip.add "META-INF/#{File.basename(file)}", file } if meta_inf + unless manifest == false zip.file.open("META-INF/MANIFEST.MF", "w") do |output| output.write MANIFEST_HEADER - case manifest - when Hash - output.write manifest.map { |pair| pair.join(": ") }.join("\n") - when Array - manifest.each do |section| - output.write "\n" - output.write section.map { |pair| pair.join(": ") }.join("\n") + if manifest + case manifest + when Hash + output.write manifest.map { |pair| pair.join(": ") }.sort.join("\n") + when Array + manifest.each do |section| + output.write "\n" + output.write section.map { |pair| pair.join(": ") }.sort.join("\n") + end + when Proc, Method + output.write manifest.call + when String + output.write File.read(manifest) end - when Proc, Method - output.write manifest.call - when String - output.write File.read(manifest) - when true # Default behavior end output.write "\n" end end super zip @@ -54,11 +59,11 @@ class WarTask < JarTask def []=(key, value) case key.to_sym when :libs - self.include value, :path=>"WEB-INF/lib" + self.include artifacts(value), :path=>"WEB-INF/lib" when :classes self.include value, :path=>"WEB-INF/classes" else super key, value end @@ -77,17 +82,26 @@ # jar "lib-1.0.jar"=>["target/classes", "MANIFEST,MF"] def jar(file) Packaging::JarTask.define_task(file) end - class Project # Group used for packaging. Inherited from parent project. inherited_attr :group # Version used for packaging. Inherited from parent project. inherited_attr :version + # Manifest used for packaging. Inherited from parent project. + inherited_attr :manifest do |project| + manifest = { "Build-By"=>ENV['USER'], "Build-Jdk"=>Java.version } + manifest["Implementation-Version"] = project.version if project.version + manifest + end + # Files to always include in the package META-INF directory. + inherited_attr :meta_inf do |project| + meta_inf = ["DISCLAIMER", "LICENSE", "NOTICE"].map { |f| path_to(f) if File.exist?(path_to(f)) }.compact + end # The project ID is the project name, and for a sub-project the # parent project ID followed by the project name, separated with a # hyphen. For example, "foo" and "foo-bar". def id() @@ -95,11 +109,11 @@ end inherited_attr :webapp_src_dir do File.join(src_dir, "main", "webapp") end def package(*args) if Hash === args.last - options = args.pop.clone + options = args.pop.dup else options = {} end options[:type] = args.shift.to_s if Symbol === args.first fail "No packaging type specified" unless options[:type] @@ -115,18 +129,18 @@ packager = method("package_as_#{options[:type]}") rescue fail("Do not know how to create a package of type #{options[:type]}") package = packager.call(file, options) or fail("Do not know how to create a package of type #{options[:type]}") - task("package").enhance [package] - package.enhance [task("build")] + task "package"=>package + package.enhance [ task("build")] - task "install"=>[ - file_create(File.dirname(repositories.locate(package))) { |task| mkpath task.name, :verbose=>false }, - file(repositories.locate(package)=>package) { |task| cp package.name, task.name }, - package.pom - ] + task "install"=>(file(repositories.locate(package)=>package) { |task| + mkpath File.dirname(task.name), :verbose=>false + cp package.name, task.name + }) + task "install"=>package.pom task "uninstall" do |task| verbose(Rake.application.options.trace) do [ package, package.pom ].map { |artifact| repositories.locate(artifact) }. each { |file| rm file if File.exist?(file) } @@ -145,33 +159,52 @@ end protected def package_as_jar(file, options) + unless Rake::Task.task_defined?(file) + returning(Java::Packaging::JarTask.define_task(file)) do |task| + package_extend task, options + task.enhance [path_to(:java_target_dir)] + task.include path_to(:java_target_dir, "*") + task.manifest = manifest.merge("Implementation-Title"=>self.comment) + task.meta_inf = meta_inf + end + end returning(Java::Packaging::JarTask.define_task(file)) do |task| - package_extend task, options - task.include path_to(:java_target_dir, "*") task.include options[:include] if options[:include] task.manifest = options[:manifest] if options[:manifest] end end def package_as_war(file, options) + unless Rake::Task.task_defined?(file) + returning(Java::Packaging::WarTask.define_task(file)) do |task| + package_extend task, options + task.include path_to(:webapp_src_dir, "*") + task.with :classes=>path_to(:java_target_dir, "**") + task.manifest = manifest.merge("Implementation-Title"=>self.comment) + task.meta_inf = meta_inf + end + end # Add anything we find in webapp sources directory. returning(Java::Packaging::WarTask.define_task(file)) do |task| - package_extend task, options - task.include path_to(:webapp_src_dir, "*") task.include options[:include] if options[:include] # Add libraries in WEB-INF lib, and classes in WEB-INF classes task.with :libs=>options[:libs].collect if options[:libs] - task.with :classes=>(options[:classes] || path_to(:java_target_dir, "**")) unless options[:classes] == false + task.with :classes=>options[:classes] if options[:classes] + task.manifest = options[:manifest] if options[:manifest] end end def package_as_zip(file, options) + unless Rake::Task.task_defined?(file) + returning(ZipTask.define_task(file)) do |task| + package_extend task, options + task.include path_to(:java_target_dir, "*") + end + end returning(ZipTask.define_task(file)) do |task| - package_extend task, options - task.include path_to(:java_target_dir, "*") task.include options[:include] if options[:include] end end def package_extend(task, spec)