Ruby-on-rails 如何在 rails 中通过命令行运行 rake 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19262433/
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-02 22:50:46  来源:igfitidea点击:

How to run a rake task by command line in rails

ruby-on-railsrubyrake

提问by co2f2e

I have defined a rake task as follows in a file called file_locker_task.rake

我在一个名为的文件中定义了一个 rake 任务,如下所示 file_locker_task.rake

namespace :myspace do
   task :process => :environment do
      FileLocker.lock_files   
   end
end

How do I execute this rake task from the command line? I tried:

如何从命令行执行这个 rake 任务?我试过:

rake myspace:processand rake processbut both are throwing an error like this:

rake myspace:processrake process但两者都抛出这样的错误:

rake aborted!
Don't know how to build task 'process'

回答by Dogweather

  1. Run rake -T -Afrom your Rails home directory to see all the tasks that rake knows about. Yours must be in that list for rake to run it.
  2. By default, in a Rails app, rake looks in the lib/tasksdirectory and its subdirectories for your .rakefiles. Check that. (I suspect this is the problem.)
  1. rake -T -A从 Rails 主目录运行以查看 rake 知道的所有任务。您的必须在该列表中,以便 rake 运行它。
  2. 默认情况下,在 Rails 应用程序中,rake 在lib/tasks目录及其子目录中查找您的.rake文件。检查那个。(我怀疑这就是问题所在。)

回答by hawk

According to docs

根据文档

Any ruby file (including other rakefiles) can be included with a standard Ruby requirecommand.

任何 ruby​​ 文件(包括其他 rakefiles)都可以包含在标准的 Rubyrequire命令中。

-

——

Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (i.e. the same directory that contains the main Rakefile). Also, rails projects may include additional rake files in the lib/tasks directory.

额外的 rake 文件(文件扩展名为“.rake”)可以放在位于项目顶层的 rakelib 目录中(即包含主 Rakefile 的同一目录)。此外,rails 项目可能在 lib/tasks 目录中包含额外的 rake 文件。