mix 自定义Task

Elixir

Posted by zhulinpinyu on December 21, 2016

Thanks: http://elixirschool.com/lessons/basics/mix-tasks/

创建Project, 命名为custom_mix_task

mix new custom_mix_task

添加一个函数为lib/custom_mix_task.ex

defmodule CustomMixTask do

  @doc """
  Output's `Hello, World!` everytime.
  """
  def hello do
    IO.puts "Hello, World!"
  end
end

定义mix task lib/mix/tasks/hi.ex

defmodule Mix.Tasks.Hi do
  use Mix.Task

  @shortdoc "Simply runs the CustomMixTask.hello/0 command."
  def run(_) do
    CustomMixTask.hello
  end
end

编译代码

mix compile

查看新建的task

mix help

...
mix hi
...

运行task

mix hi
# "Hello, World!"