You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by bo...@apache.org on 2010/09/18 03:23:08 UTC

svn commit: r998386 - in /buildr/trunk: CHANGELOG lib/buildr/core/application.rb lib/buildr/packaging/artifact.rb spec/packaging/artifact_spec.rb

Author: boisvert
Date: Sat Sep 18 01:23:08 2010
New Revision: 998386

URL: http://svn.apache.org/viewvc?rev=998386&view=rev
Log:
BUILDR-212 Update support for SNAPSHOT artifacts (Timo Rantalaiho and Izzet Mustafa)

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/lib/buildr/core/application.rb
    buildr/trunk/lib/buildr/packaging/artifact.rb
    buildr/trunk/spec/packaging/artifact_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=998386&r1=998385&r2=998386&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Sat Sep 18 01:23:08 2010
@@ -1,6 +1,7 @@
 1.4.2 (pending)
 * Added:  BUILDR-415 Ability to exclude tests from command line
 * Added:  BUILDR-495 Document twitter on Buildr's homepage
+* Added:  BUILDR-212 Update support for SNAPSHOT artifacts (Timo Rantalaiho and Izzet Mustafa)
 * Added:  Integration test to show how to change the war packaging spec.
 * Added:  Integration test to show how to use junit 3.
 * Added:  Integration test to show how to get ahold of parent project

Modified: buildr/trunk/lib/buildr/core/application.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/core/application.rb?rev=998386&r1=998385&r2=998386&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/core/application.rb (original)
+++ buildr/trunk/lib/buildr/core/application.rb Sat Sep 18 01:23:08 2010
@@ -357,6 +357,18 @@ module Buildr
             puts "Buildr #{Buildr::VERSION}#{RUBY_PLATFORM[/java/] ? " (JRuby #{JRUBY_VERSION})" : ""}"
             exit 0
           }
+        ],
+        ['--offline', '-o', "Do not try to download anything",
+          lambda { |value|
+            trace 'Working in offline mode; snapshot will not be updated.'
+            options.work_offline = true
+          }
+        ],
+        ['--update-snapshots', '-u', "Force updating all dependencies whose version contains SNAPSHOT",
+          lambda { |value|
+            trace 'Force update of SNAPSHOT artifacts.'
+            options.update_snapshots = true
+          }
         ]
       ]
     end

Modified: buildr/trunk/lib/buildr/packaging/artifact.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/packaging/artifact.rb?rev=998386&r1=998385&r2=998386&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/packaging/artifact.rb (original)
+++ buildr/trunk/lib/buildr/packaging/artifact.rb Sat Sep 18 01:23:08 2010
@@ -17,6 +17,7 @@
 require 'buildr/core/project'
 require 'buildr/core/transports'
 require 'buildr/packaging/artifact_namespace'
+require 'fileutils'
 
 
 module Buildr
@@ -151,7 +152,7 @@ module Buildr
           pom.install
         end
         mkpath File.dirname(in_local_repository)
-        cp name, in_local_repository
+        cp name, in_local_repository, :preserve => false
         info "Installed #{name} to #{in_local_repository}"
       end
     end
@@ -335,7 +336,7 @@ module Buildr
         # if the artifact knows how to build itself (e.g. download from a different location),
         # so don't perform it if the task found a different way to create the artifact.
         task.enhance do
-          if !File.exist?(name)
+          if download_needed? task
             info "Downloading #{to_spec}"
             download
             pom.invoke rescue nil if pom && pom != self
@@ -414,7 +415,7 @@ module Buildr
       exact_success = remote.find do |repo_url|
         begin
           path = "#{group_path}/#{id}/#{version}/#{File.basename(name)}"
-          URI.download repo_url + path, name
+          download_artifact(repo_url + path)
           true
         rescue URI::NotFoundError
           false
@@ -439,7 +440,8 @@ module Buildr
         snapshot_url = current_snapshot_repo_url(repo_url)
         if snapshot_url
           begin
-            URI.download snapshot_url, name
+            download_artifact snapshot_url
+            true
           rescue URI::NotFoundError
             false
           end
@@ -470,6 +472,71 @@ module Buildr
     def fail_download(remote_uris)
       fail "Failed to download #{to_spec}, tried the following repositories:\n#{remote_uris.join("\n")}"
     end
+
+   protected
+  
+    # :call-seq:
+    #   needed?
+    #
+    # Validates whether artifact is required to be downloaded from repository
+    def needed?
+      return true if snapshot? && File.exist?(name) && old?
+      super      
+    end
+    
+  private
+  
+    # :call-seq:
+    #   download_artifact
+    #
+    # Downloads artifact from given repository, 
+    # supports downloading snapshot artifact with relocation on succeed to local repository 
+    def download_artifact(path)
+      download_file = "#{name}.#{Time.new.to_i}"
+      begin
+        URI.download path, download_file
+        if File.exist?(download_file)
+          FileUtils.mkdir_p(File.dirname(name))
+          FileUtils.mv(download_file, name)
+        end
+      ensure
+        File.delete(download_file) if File.exist?(download_file)
+      end
+    end
+    
+    # :call-seq:
+    #   :download_needed?
+    #
+    # Validates whether artifact is required to be downloaded from repository
+    def download_needed?(task)
+      return true if !File.exist?(name)
+
+      if snapshot?
+        return false if offline? && File.exist?(name)
+        return true if update_snapshot? || old?
+      end
+      
+      return false
+    end
+    
+    def update_snapshot?
+      Buildr.application.options.update_snapshots
+    end
+    
+    def offline?
+      Buildr.application.options.work_offline
+    end
+    
+    # :call-seq:
+    #   old?
+    #
+    # Checks whether existing artifact is older than period from build settings or one day 
+    def old?
+      settings = Buildr.application.settings
+      time_to_be_old = settings.user[:expire_time] || settings.build[:expire_time] || 60 * 60 * 24
+      File.mtime(name).to_i < (Time.new.to_i - time_to_be_old)
+    end
+
   end
 
 

Modified: buildr/trunk/spec/packaging/artifact_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/packaging/artifact_spec.rb?rev=998386&r1=998385&r2=998386&view=diff
==============================================================================
--- buildr/trunk/spec/packaging/artifact_spec.rb (original)
+++ buildr/trunk/spec/packaging/artifact_spec.rb Sat Sep 18 01:23:08 2010
@@ -13,9 +13,8 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-
 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers'))
-
+require 'fileutils'
 
 describe Artifact do
   before do
@@ -25,6 +24,7 @@ describe Artifact do
     @snapshot = artifact(@spec.merge({ :version=>'2.1-SNAPSHOT' }))
   end
 
+
   it 'should act as one' do
     @artifact.should respond_to(:to_spec)
   end
@@ -290,7 +290,7 @@ describe Repositories, 'remote' do
       and_return { fail URI::NotFoundError }
     URI.should_receive(:download).twice.with(uri(/2.1-SNAPSHOT\/maven-metadata.xml$/), duck_type(:write)).
       and_return { |uri, target, options| target.write(metadata) }
-    URI.should_receive(:download).twice.with(uri(/2.1-SNAPSHOT\/library-2.1-20071012.190008-8.(jar|pom)$/), /2.1-SNAPSHOT\/library-2.1-SNAPSHOT.(jar|pom)$/).
+    URI.should_receive(:download).twice.with(uri(/2.1-SNAPSHOT\/library-2.1-20071012.190008-8.(jar|pom)$/), /2.1-SNAPSHOT\/library-2.1-SNAPSHOT.(jar|pom).(\d){1,}$/).
       and_return { |uri, target, options| write target }
     lambda { artifact('com.example:library:jar:2.1-SNAPSHOT').invoke }.
       should change { File.exist?(File.join(repositories.local, 'com/example/library/2.1-SNAPSHOT/library-2.1-SNAPSHOT.jar')) }.to(true)
@@ -461,7 +461,11 @@ end
 
 
 describe Buildr, '#artifact' do
-  before { @spec = { :group=>'com.example', :id=>'library', :type=>'jar', :version=>'2.0' } }
+  before do 
+    @spec = { :group=>'com.example', :id=>'library', :type=>'jar', :version=>'2.0' }
+    @snapshot_spec = 'group:id:jar:1.0-SNAPSHOT'
+    write @file = 'testartifact.jar' 
+  end
 
   it 'should accept hash specification' do
     artifact(:group=>'com.example', :id=>'library', :type=>'jar', :version=>'2.0').should respond_to(:invoke)
@@ -548,6 +552,57 @@ describe Buildr, '#artifact' do
     Buildr.application.send(:load_artifact_ns)
     artifact(:j2ee).to_s.pathmap('%f').should == 'geronimo-spec-j2ee-1.4-rc4.jar'
   end
+  
+  it 'should try to download snapshot artifact' do
+    run_with_repo
+    snapshot = artifact(@snapshot_spec)
+    
+    URI.should_receive(:download).at_least(:twice).and_return { |uri, target, options| write target }
+    FileUtils.should_receive(:mv).at_least(:twice)
+    snapshot.invoke
+  end
+
+  it 'should not try to update snapshot in offline mode if it exists' do
+    run_with_repo
+    snapshot = artifact(@snapshot_spec)
+    write snapshot.to_s
+    Buildr.application.options.work_offline = true
+    URI.should_receive(:download).exactly(0).times
+    snapshot.invoke
+  end
+  
+  it 'should download snapshot even in offline mode if it doesn''t exist' do
+    run_with_repo
+    snapshot = artifact(@snapshot_spec)
+    Buildr.application.options.work_offline = true
+    URI.should_receive(:download).exactly(2).times
+    snapshot.invoke
+  end
+  
+  it 'should update snapshots if --update-snapshots' do
+    run_with_repo
+    snapshot = artifact(@snapshot_spec)
+    Buildr.application.options.update_snapshots = true
+    
+    URI.should_receive(:download).at_least(:twice).and_return { |uri, target, options| write target }
+    FileUtils.should_receive(:mv).at_least(:twice)
+    snapshot.invoke
+  end
+  
+  it 'should update snapshot if it''s older than 24 hours' do
+    run_with_repo
+    snapshot = artifact(@snapshot_spec)
+    write snapshot.to_s
+    time = Time.at((Time.now - (60 * 60 * 24) - 10 ).to_i)
+    File.utime(time, time, snapshot.to_s)
+    URI.should_receive(:download).at_least(:once).and_return { |uri, target, options| write target }
+    snapshot.invoke
+  end
+  
+  def run_with_repo
+    repositories.remote = 'http://example.com'
+  end
+  
 end
 
 
@@ -638,6 +693,7 @@ describe Buildr, '#install' do
   before do
     @spec = 'group:id:jar:1.0'
     write @file = 'test.jar'
+    @snapshot_spec = 'group:id:jar:1.0-SNAPSHOT'
   end
 
   it 'should return the install task' do
@@ -660,7 +716,27 @@ describe Buildr, '#install' do
     write artifact(@spec).to_s # install a version of the artifact
     old_mtime = File.mtime(artifact(@spec).to_s)
     sleep 1; write @file       # make sure the "from" file has newer modification time
-    lambda { install.invoke }.should change { File.exist?(artifact(@spec).to_s) and old_mtime < File.mtime(artifact(@spec).to_s) }.to(true)
+    lambda { install.invoke }.should change { modified?(old_mtime, @spec) }.to(true)
+  end
+
+  it 'should re-install snapshot artifact when "from" is newer' do
+    install artifact(@snapshot_spec).from(@file)
+    write artifact(@snapshot_spec).to_s # install a version of the artifact
+    old_mtime = File.mtime(artifact(@snapshot_spec).to_s)
+    sleep 1; write @file       # make sure the "from" file has newer modification time
+    lambda { install.invoke }.should change { modified?(old_mtime, @snapshot_spec) }.to(true)
+  end
+  
+  it 'should download snapshot to temporary location' do
+    repositories.remote = 'http://example.com'
+    snapshot = artifact(@snapshot_spec)
+    same_time = Time.new
+    download_file = "#{Dir.tmpdir}/#{File.basename(snapshot.name)}#{same_time.to_i}"
+    
+    Time.should_receive(:new).twice.and_return(same_time)
+    URI.should_receive(:download).at_least(:twice).and_return { |uri, target, options| write target }
+    FileUtils.should_receive(:mv).at_least(:twice)
+    snapshot.invoke
   end
 
   it 'should install POM alongside artifact' do
@@ -929,3 +1005,7 @@ XML
     transitive(@transitive).should eql(artifacts(@transitive, @complex, @simple - [@provided]))
   end
 end
+
+def modified?(old_mtime, spec)
+  File.exist?(artifact(spec).to_s) && old_mtime < File.mtime(artifact(spec).to_s)
+end