Mix tasks are helper tasks in Elixir projects.
In this post, I’ll create an empty project and a “Hello World!”
I can create a new project with
1
|
|
Now, inside the new mix_task_example
project directory, running
1
|
|
shows which mix tasks are available and in this newly created project there are already more than 30.
Now I create the directory for tasks with
1
|
|
and create a file with the name of the task, e.g. mix_task_example.salute.ex
with the following contents:
1 2 3 4 5 6 7 |
|
This far, the task is not yet available, I need to compile the project
1
|
|
And now I can run it with
1 2 |
|
The task doesn’t show up in the listing with mix help
.
To make it show up I need to add @shortdoc
1 2 3 4 5 6 7 8 9 |
|
If the tasks are part of a library, they will be available to project that
include it as a dependency, so it’s important to use the library name
(e.g. MixTaskExample
) in the module name in order to keep tasks in
a collective namesapce.
As a final touch, let’s accept a parameter
1 2 3 4 5 6 7 8 9 |
|
1 2 3 |
|
A working mix task!
The code for this example can be found at https://github.com/joeyates/mix_task_example