This tip describes how to write a rake task in a gem, then use it in another Ruby gem or Rails app.
Create a rake task in a gem
lib/tasks directory in your gem. Create a rake task in that directory with the same syntax it would have in the Rakefile. The file should have the
Now include the rake task in the Rakefile for the gem by using
import. For example:
Use the rake task in another gem
In the second gem, create a
Rakefile for the project and load it using the
load method. As an example, we can load the “downloader” rake file from the corenlp gem 1 with this snippet:
spec = Gem::Specification.find_by_name 'corenlp'
Verify the task shows up when running
rake -T (make sure the task has a description!) and can be invoked as expected.
Use the rake task in a Rails app
Sharing rake tasks between Ruby apps keeps your code DRY. If you found this tip useful let me know! Thanks for checking it out.