Location: Rake Documents / User_guide / 04 -- Frequently Asked Questions Language: en

Chapter 4—Frequently Asked Questions

How can I call one Rake task from inside another task?

Generally, if you want invoke one task from another task, the proper way to do that is to include the task to be invoked as a prerequisite of the task doing the invoking.

For example:

  task :primary => [:secondary]

  task :secondary do
    puts "Doing Secondary Task" 
  end

However, there are certain rare occasions where you want to invoke a task from within the body of a primary task. You could do it like this:

  task :primary do
    Rake::Task[:secondary].invoke
  end

  task :secondary do
    puts "Doing Secondary Task" 
  end

Keep the following in mind:

If the second and third point above are not to your liking, then perhaps you should consider making the secondary task a regular Ruby method and just calling it directly. Something like this:

  task :primary do
    secondary_task
  end

  task :secondary do
    secondary_task
  end

  def secondary_task
    puts "Doing Secondary Task" 
  end