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

时间:2022-08-03 02:32:27

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

我在名为file_locker_task.rake的文件中定义了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:process and rake process but both are throwing an error like this:

rake myspace:进程和rake进程,但它们都抛出了这样的错误:

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

2 个解决方案

#1


23  

  1. Run rake -T -A from 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. 从Rails主目录运行rake -T -A,查看rake知道的所有任务。你的必须在那个列表里,用来运行它。
  3. By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
  4. 默认情况下,在Rails应用程序中,rake在lib/tasks目录及其子目录中查找.rake文件。检查。(我怀疑这就是问题所在。)

#2


-1  

According to docs

根据文档

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

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

-

- - - - - -

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文件。

#1


23  

  1. Run rake -T -A from 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. 从Rails主目录运行rake -T -A,查看rake知道的所有任务。你的必须在那个列表里,用来运行它。
  3. By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
  4. 默认情况下,在Rails应用程序中,rake在lib/tasks目录及其子目录中查找.rake文件。检查。(我怀疑这就是问题所在。)

#2


-1  

According to docs

根据文档

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

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

-

- - - - - -

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文件。