You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by as...@apache.org on 2008/02/08 01:01:57 UTC

svn commit: r619691 - in /incubator/buildr/trunk: lib/java/packaging.rb spec/java_packaging_spec.rb

Author: assaf
Date: Thu Feb  7 16:01:54 2008
New Revision: 619691

URL: http://svn.apache.org/viewvc?rev=619691&view=rev
Log:
BUILDR-45

Modified:
    incubator/buildr/trunk/lib/java/packaging.rb
    incubator/buildr/trunk/spec/java_packaging_spec.rb

Modified: incubator/buildr/trunk/lib/java/packaging.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/packaging.rb?rev=619691&r1=619690&r2=619691&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/packaging.rb (original)
+++ incubator/buildr/trunk/lib/java/packaging.rb Thu Feb  7 16:01:54 2008
@@ -296,31 +296,54 @@
         # Add an artifact to this EAR.
         def add(*args)
           options = Hash === args.last ? args.pop.clone : {}
-          if artifact = args.shift
-            type = options[:type]
-            unless type
-              type = artifact.respond_to?(:type) ? artifact.type : artifact.pathmap('%x').to_sym
-              type = :lib if type == :jar
+          args.flatten!
+          args.map! do |pkg|
+            case pkg
+            when Project
+              pkg.packages.select { |pp| JarTask === pp && SUPPORTED_TYPES.include?(pp.type) }
+            when Rake::FileTask
+              pkg # add the explicitly provided file
+            when Hash
+              Buildr.artifact(pkg)
+            when String
+              begin
+                Buildr.artifact(pkg)
+              rescue # not an artifact spec, it must me a filename
+                file(pkg)
+              end
+            else 
+              raise "Invalid EAR component #{pkg.class}: #{pkg}"
+            end
+          end
+          args.flatten!
+          args.compact!
+          if args.empty?
+            raise ":type must not be specified for type=>component argument style" if options.key?(:type)
+            raise ":as must not be specified for type=>component argument style" if options.key?(:as)
+            comps = {}
+            options.delete_if { |k, v| comps[k] = v if SUPPORTED_TYPES.include?(k) }
+            raise "You must specify at least one valid component to add" if comps.empty?
+            comps.each { |k, v| add(v, {:as => k}.merge(options)) }
+          else
+            args.each do |artifact|
+              type = options[:as] || options[:type]
+              unless type
+                type = artifact.respond_to?(:type) ? artifact.type : artifact.to_s.pathmap('%x').to_sym
+                type = :lib if type == :jar
+              end
               raise "Unknown EAR component type: #{type}. Perhaps you may explicity tell what component type to use." unless
                 SUPPORTED_TYPES.include?(type)
+              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
+              @components << component
             end
-          else
-            type = SUPPORTED_TYPES.find { |type| options[type] }
-            artifact = Buildr.artifact(options[type])
           end
-
-          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
-          @components << component
-          self
-        end
-
-        def push(*artifacts)
-          artifacts.flatten.each { |artifact| add artifact }
           self
         end
+        
+        alias_method :push, :add
         alias_method :<<, :push
 
       protected

Modified: incubator/buildr/trunk/spec/java_packaging_spec.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/java_packaging_spec.rb?rev=619691&r1=619690&r2=619691&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/java_packaging_spec.rb (original)
+++ incubator/buildr/trunk/spec/java_packaging_spec.rb Thu Feb  7 16:01:54 2008
@@ -649,9 +649,49 @@
     inspect_ear { |files| files.should include('jar/foo-1.0.jar') }
   end
 
+  it 'should add multiple components at a time using the type=>component style' do
+    define 'bar', :version => '1.5' do
+      package(:war) # must be added as a webapp
+      package(:jar) # must be added as a shared lib
+      package(:zip) # this one should be excluded
+    end
+    define 'baz', :version => '1.5' do
+      package(:jar, :id => 'one')
+      package(:jar, :id => 'two')
+    end
+    define 'foo', :version => '1.0' do 
+      package(:ear).add :lib => project('baz'),
+                        :war => project('bar').package(:war),
+                        :ejb => project('bar').package(:jar)
+    end
+    inspect_ear do |files| 
+      files.should include(*%w{ lib/one-1.5.jar lib/two-1.5.jar war/bar-1.5.war ejb/bar-1.5.jar  })
+      files.should_not satisfy { files.any? { |f| f =~ /\.zip$/ } }
+    end
+  end
+
+  it 'should add all EAR supported packages when given a project argument' do
+    define 'bar', :version => '1.5' do
+      package(:war) # must be added as a webapp
+      package(:jar) # must be added as a shared lib
+      package(:zip) # this one should be excluded
+    end
+    define 'baz', :version => '1.5' do
+      package(:war)
+      package(:jar)
+    end
+    define 'foo', :version => '1.0' do 
+      package(:ear).add projects(:bar, :baz)
+    end
+    inspect_ear do |files| 
+      files.should include('war/bar-1.5.war', 'lib/bar-1.5.jar', 'lib/baz-1.5.jar', 'war/baz-1.5.war')
+      files.should_not satisfy { files.any? { |f| f =~ /\.zip$/ } }
+    end
+  end
+
   it 'should complain about unknown component type' do
     define 'foo', :version=>'1.0' do
-      lambda { package(:ear).add package(:zip) }.should raise_error(RuntimeError, /unknown ear component type/i)
+      lambda { package(:ear).add package(:zip) }.should raise_error(RuntimeError, /ear component/i)
     end
   end