Include Other Taskfiles
Learn how to include other Taskfiles in your main Taskfile to modularize and reuse tasks.
Including Other Taskfiles
This feature allows you to include other Taskfiles in your main Taskfile, which is useful for modularizing and reusing tasks across different projects or directories. What you need to do is just using the includes
keyword in your Taskfile.
Here is the scenario, in your root directory, you have a Taskfile.yml
that need to include other Taskfiles from app
directory and docker-task.yml
file.
In this case, you can run the <namespace>:<task>
to execute the tasks from the included Taskfiles.
Include other Taskfile tasks in tasks and variables of included Task
You can also include other Taskfile tasks in your Taskfile tasks. This is useful when you want to reuse tasks from other Taskfiles without duplicating the code.
- You can use
default
keyword to set the default task for the included Taskfile, then you can override the variable values in thevars
keyword. Refer here
OS-Specific Taskfiles
You can also include Taskfiles based on the operating system by using special variable.
Include other taskfile to run on different directory
Now, we know that included Taskfiles are run in the current directory, even the Taskfile is included from another directory. But you can actually run the Taskfile tasks in another directory by using the dir
keyword.
- If the directory does not exist, Task will create it.
Optional includes
Optional includes allow you to include Taskfiles that may not exist, without causing an error if they are missing.
Internal includes
Internal includes allow you to include utility tasks that are not intended to be run directly by users.
Flatten includes
Make sure the tasks do not have same name across the included Taskfiles, otherwise it will provide an error.
Flatten includes allow you to include tasks from other Taskfiles without the need to specify the namespace.
Exclude Tasks from Includes
You can exclude specific tasks from being included by using the exclude
keyword.
Vars of included Taskfiles
You can also use the vars
keyword to pass variables to the included Taskfiles. This is useful when you want to customize the behavior of the included tasks.
- You can use
default
keyword to set the default task for the included Taskfile, then you can override the variable values in thevars
keyword. Refer here
Namespace aliases (Shortform)
You can also use the alias
keyword to create short aliases for namespaces. This is useful when you want to reduce the typing effort.