LoginSignup
2
2

More than 5 years have passed since last update.

rake taskを上書きする

Posted at

Rakeタスクを追加するとき、同じ名前でタスクを追加してしまうと、タスクが2つ存在することになりうまく動かないことがあります。

namespace :hoge do
  desc "print bar"
  task :task do
    p "bar"
  end
end

このようなtaskが定義されているとします。

namespace :hoge do
  desc "print bar"
  task :task do
    p "bar"
  end
end

namespace :hoge do
  desc "print foo"
  task :task do
    p "foo"
  end
end

この状態でrake -Tを行うと、

$ rake -T
rake hoge:task  # print bar / print foo

となり、2つタスクが定義されているようになってしまいます。
これを解消するには、 clearを使ってtaskを消してから再定義する必要があいrます。

namespace :hoge do
  desc "print bar"
  task :task do
    p "bar"
  end
end

Rake::Task["hoge:task"].clear
namespace :hoge do
  desc "print foo"
  task :task do
    p "foo"
  end
end
$ rake -T
rake hoge:task  # print foo

これで新しく定義したtaskが動くようになります。
capistranoの内容を上書きする際にも使えます。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2