Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access project version within elixir application

Tags:

I have an elixir project with a defined version. How can I access this from within the running application.

in mix.exs

  def project do     [app: :my_app,      version: "0.0.1"]   end 

I would like to be access this version number in the application so I can add it to the returned message. I looking for something in the env hash like the following

__ENV__.version # => 0.0.1 
like image 514
Peter Saxton Avatar asked Oct 06 '15 10:10

Peter Saxton


2 Answers

Mix.Project itself provides access to all project keywords defined in mix.exs using its config/0 (api doc) function. For concise access it might be wrapped into a function:

@version Mix.Project.config[:version] def version(), do: @version 
like image 59
0x0me Avatar answered Sep 23 '22 16:09

0x0me


Here's a similar approach to retrieve the version string. It also relies on the :application module, but is maybe a bit more straightforward:

{:ok, vsn} = :application.get_key(:my_app, :vsn) List.to_string(vsn) 
like image 27
Patrick Oscity Avatar answered Sep 21 '22 16:09

Patrick Oscity