How to get app version from mix.exs?

I’m wondering how I can retrieve an application’s version (as specified in its mix.exs)? I’d like to have that value available for logging.

Thanks!

1 Like
Application.spec(app_name)[:vsn]
9 Likes

This can also be written as Application.spec(app_name, :vsn), slightly shorter and if you mistype the key you will get a FunctionClauseError (could be better or worse depending on your use case)

4 Likes

I stumbled over this question while searching for a generic way to get app name and version for a container based build template.

You are able to get project information from your mix.exs using Mix.Project.config() which returns something like:

[
  build_embedded: false,
  build_per_environment: true,
  build_scm: Mix.SCM.Path,
  config_path: "config/config.exs",
  consolidate_protocols: true,
  default_task: "run",
  deps_path: "deps",
  erlc_paths: ["src"],
  erlc_include_path: "include",
  erlc_options: [],
  lockfile: "mix.lock",
  preferred_cli_env: [],
  app: :my_shiny_app,
  version: "0.1.0",
  elixir: "~> 1.12",
  elixirc_paths: ["lib"],
  compilers: [:gettext, :yecc, :leex, :erlang, :elixir, :app],
  start_permanent: false,
  aliases: [
    setup: ["deps.get"],
    "assets.deploy": ["esbuild default --minify", "phx.digest"]
  ],
  deps: [
    {:phoenix, "~> 1.6.0-rc.0", [override: true]},
    ...
  ]
]

I use this to get necessary project information during the build process without the requirement of prefilling environment variables like APP_NAME etc:

$ mix run --eval "Mix.Project.config()[:version] |> IO.puts()"
0.1.0
$ mix run --eval "Mix.Project.config()[:app] |> IO.puts()"
my_shiny_app
1 Like