Ruby-on-rails Rails 如何运行 rake 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5641747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:55:01  来源:igfitidea点击:

Rails how to run rake task

ruby-on-railsrubyrake

提问by Rails beginner

How do I run this rake file in terminal/console?

如何在终端/控制台中运行这个 rake 文件?

my statistik.rake in lib/tasks

我在 lib/tasks 中的 statistik.rake

desc "Importer statistikker"
namespace :reklamer do
  task :iqmedier => :environment do
    ...
  end
  task :euroads => :environment do
    ...
  end
  task :mikkelsen => :environment do
    ...
  end
  task :orville => :environment do
    ...
  end
end

回答by Andrew Marshall

You can run Rake tasks from your shell by running:

您可以通过运行以下命令从 shell 运行 Rake 任务:

rake task_name

To run from from Ruby (e.g., in the Rails console or another Rake task):

从 Ruby 运行(例如,在 Rails 控制台或其他 Rake 任务中):

Rake::Task['task_name'].invoke

To run multiple tasks in the same namespace with a single task, create the following new task in your namespace:

要使用单个任务在同一命名空间中运行多个任务,请在命名空间中创建以下新任务:

task :runall => [:iqmedier, :euroads, :mikkelsen, :orville] do
  # This will run after all those tasks have run
end

回答by Luke W

Rake::Task['reklamer:orville'].invoke

or

或者

Rake::Task['reklamer:orville'].invoke(args)

回答by CharlieMezak

Have you tried rake reklamer:iqmedier?

你试过rake reklamer:iqmedier吗?

My custom rake tasks are in the lib directory, not in lib/tasks. Not sure if that matters.

我的自定义 rake 任务在 lib 目录中,而不是在 lib/tasks 中。不确定这是否重要。

回答by Prem

Sometimes Your rake tasks doesn't get loaded in console, In that case you can try the following commands

有时您的 rake 任务不会在控制台中加载,在这种情况下,您可以尝试以下命令

require "rake"
YourApp::Application.load_tasks
Rake::Task["Namespace:task"].invoke

回答by Ruto Collins

If you aren't sure how to run a rake task, first find out first what tasks you have and it will also list the commands to run the tasks.

如果您不确定如何运行 rake 任务,请首先找出您拥有的任务,它还会列出运行任务的命令。

Run rake --taskson the terminal.

rake --tasks在终端上运行。

It will list the tasks like the following:

它将列出如下任务:

rake gobble:dev:prime             
rake gobble:dev:reset_number_of_kits                                    
rake gobble:dev:scrub_prod_data

You can then run your task with: rake gobble:dev:primeas listed.

然后,您可以使用以下命令运行您的任务:rake gobble:dev:prime如所列。

回答by hari

In rails 4.2 the above methods didn't work.

在 rails 4.2 中,上述方法不起作用。

  1. Go to the Terminal.
  2. Change the directory to the location where your rake file is present.
  3. run rake task_name.
  4. In the above case, run rake iqmedier - will run only iqmedir task.
  5. run rake euroads - will run only the euroads task.
  6. To Run all the tasks in that file assign the following inside the same file and run rake all

    task :all => [:iqmedier, :euroads, :mikkelsen, :orville ] do #This will print all the tasks o/p on the screen 
    end
    
  1. 前往航站楼。
  2. 将目录更改为您的 rake 文件所在的位置。
  3. 运行 rake task_name。
  4. 在上述情况下,运行 rake iqmedier - 将仅运行 iqmedir 任务。
  5. 运行 rake euroads - 将只运行 euroads 任务。
  6. 要运行该文件中的所有任务,请在同一文件中分配以下内容并运行 rake all

    task :all => [:iqmedier, :euroads, :mikkelsen, :orville ] do #This will print all the tasks o/p on the screen 
    end