2012-05-23 114 views

回答

2

在要从中运行任务的目录中创建一个名为rakefile的文件。 此代码将让这个如果你只需要输入“回扣” my_default_task将运行:

task :default => 'my_default_task' 

    task :my_default_task do 
    puts "Now I am doing the task that Tempus wants done when he/she types 'rake' in the console." 
    end 

    task :my_not_default_task do 
    puts "This isn't the default task." 
    end 

但是,如果你输入rake my_not_default_task,然后my_default_task不会跑。如果你希望它无论跑这里是一两件事可以做:

task :default => 'my_default_task' 

task :my_default_task do 
    puts "This is the default task" 
end 

task :my_not_default_task do 
    puts "This isn't the default task." 
end 

Rake::Task['my_default_task'].invoke 

此代码中的最后一行确保my_default_task当你调用其他一些任务甚至可以运行,因此,如果您键入rake my_not_default_taskmy_default_task“也将跑。

编辑: 当你与轨工作,你可以把上述任务的文件中lib/tasks文件夹的.rake和铁轨的延伸会自动地运行它们,当你做rake

Jason Seifer has a real nice tutorial on rake

+0

当然,但我怎么做,即使我调用Rails相关的任务,我的任务会跑? – Geo

+0

@Tempus我编辑了答案来回答你关于Rails中Rake的问题。 – vlasits