在Rake中组合多个任务时清理任务

dex*_*ter 4 rake ruby-on-rails rakefile ruby-on-rails-3

我在使用以下依赖项定义的rake中有一个构建任务:

desc 'Builds the App'
task :rebuild_dev => ["solr:start", "db:drop", "db:create", "db:migrate", "spec", "solr:stop"]
Run Code Online (Sandbox Code Playgroud)

第一个任务"solr:start"启动Solr Indexing服务器.现在,如果构建失败(可能在规范测试中失败),则不执行"solr:stop"任务.并且服务器没有停止.

是否有任何方法可以指定清理任务或始终运行的任务,即使其中一个从属任务失败?在我的情况下,要始终确保"solr:stop"执行...

shi*_*ara 7

您只需要使用Ruby的ensure系统

desc "Builds the App"
task :rebuild_dev do
  begin
    ["solr:start", "db:drop", "db:create", "db:migrate", "spec"].each do |t|
      Rake::Task[t].execute
    end
  ensure
    Rake::Task["solr:stop"].execute
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回复.需要注意的一点是,最好是对任务执行调用而不是执行.Coz调用也将执行依赖项. (2认同)