How to run rake task?

26.4k views Asked by At

I have an existing .rake file. It used to have one task and I added one more task to the existing rake file.

But when I try to run, it throws an error:

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

abc.rake file:

namespace abcd
  namespace abcde
    task pqr do
      ------------------
    end

    task mno do ( new task which I added)
     ---------------------
    end
  end
end

But when I used command: rake abcd:abcde:mno -- it showed above error

So I used rake -T -A , I am able to see the rake task abcd:abcde:pqr but am unable to see the other one.

I am new to rails. Please help me out.

Thanks in advance.

4

There are 4 answers

2
Raza Hussain On

Here is test code for you:

create a file abcde.rake under ../lib/tasks/abcde.rake

namespace :abcde do
  desc 'pqr pqr pqr pqr'
  task :pqr => :environment do
    puts 'pqr'
  end
  desc 'mno mno mno mno'
  task :mno => :environment do
     puts 'mno'
  end
end

Then run this command

rake -T

output of above command is:

rake abcde:mno                                   # mno mno mno mno
rake abcde:pqr                                   # pqr pqr pqr pqr

Run

rake abcde:mno #mno
0
Sean On

Step 1: Have you tried prepending the command with 'bundle exec'? Ex: ~$ bundle exec rake abcd:abcde:mno

If you have multiple versions in your gem file you'll often need to run bundle exec to run the command using your current project directory's gems.

(Optional) Step 2: If that fails try specifying the environment like this Ex: ~$ bundle exec rake abcd:abcde:mno RAILS_ENV=development

0
AudioBubble On

Here what you exactly want.....

Inside lib/tasks create file abcd.rake

Write the following code inside abcd.rake file

namespace :abcd do
  namespace :abcde do

     task :pqr do
       puts 'Inside PQR'
     end

     task :new_added_task do
       puts 'Inside New Added Task'
     end

     task :mno => [:new_added_task]  do
       puts 'Inside Mno'
     end

   end
end

Now try following commands....

rake abcd:abcde:pqr           #  Output => Inside PQR 

rake abcd:abcde:mno           #  Output => Inside New Added Task
                              #            Inside Mno

rake abcd:abcde:new_added_task  # Output => Inside New Added Task

To view all tasks run command

rake -T -A 

It will show all tasks along with your own created tasks...

rake abcd:abcde:mno                                 # 
rake abcd:abcde:new_added_task                      # 
rake abcd:abcde:pqr                                 # 
.............
0
Victor On

In Rails 6.x you have to use rails instead of rake. The example from user400617 above

namespace :abcd do
  namespace :abcde do

     task :pqr do
       puts 'Inside PQR'
     end

     task :new_added_task do
       puts 'Inside New Added Task'
     end

     task :mno => [:new_added_task]  do
       puts 'Inside Mno'
     end

   end
end

would be run like this

rails abcd:abcde:pqr           #  Output => Inside PQR 

rails abcd:abcde:mno           #  Output => Inside New Added Task
                              #            Inside Mno

rails abcd:abcde:new_added_task  # Output => Inside New Added Task

Took me a while to figure this out.