您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

121 行
3.7 KiB

10 年前
10 年前
  1. ## Why Rebar3?
  2. Rebar was a great step forward for Erlang development but with time has proven to be fragile to change. Rebar3 builds on certain components of rebar which have benefited from years of community collaboration within a new core which is more extendable.
  3. ## Creating a New Project
  4. ```shell
  5. $ rebar3 new release myrelease
  6. ===> Writing apps/myrelease/src/myrelease_app.erl
  7. ===> Writing apps/myrelease/src/myrelease_sup.erl
  8. ===> Writing apps/myrelease/src/myrelease.app.src
  9. ===> Writing rebar.config
  10. ===> Writing relx.config
  11. ===> Writing config/sys.config
  12. ===> Writing config/vm.args
  13. ===> Writing .gitignore
  14. ===> Writing LICENSE
  15. ===> Writing README.md
  16. ```
  17. ## Working on an Existing Rebar3 Project
  18. First, checkout the project and change directories to the project root.
  19. ```shell
  20. $ git clone git://github.com/tsloughter/minasan.git
  21. $ cd minansan
  22. ```
  23. Now we can use rebar3 to fetch all dependencies and build both the dependencies and the project app or apps.
  24. ```shell
  25. $ rebar3 compile
  26. ===> Fetching gproc
  27. ===> Fetching ranch
  28. ===> Fetching cowboy
  29. ===> Fetching cowlib
  30. ===> Compiling gproc
  31. /home/tristan/Devel/minasan/_deps/gproc/src/gproc_dist.erl:23: Warning: behaviour gen_leader undefined
  32. ===> Compiling cowlib
  33. ===> Compiling ranch
  34. ===> Compiling cowboy
  35. ===> Compiling minasan
  36. ```
  37. ## Adding Dependencies
  38. Dependencies are listed in `rebar.config` file under the `deps` key:
  39. ```erlang
  40. {deps, [
  41. {cowboy, ".*", {git, "git://github.com/ninenines/cowboy.git", {tag, "1.0.0"}}}
  42. ]}.
  43. ```
  44. Now you can add the dep to one of your project's application's `.app.src` file under applications:
  45. ```erlang
  46. {application, <APPNAME>,
  47. [{description, ""},
  48. {vsn, "<APPVSN>"},
  49. {registered, []},
  50. {modules, []},
  51. {applications, [
  52. kernel
  53. ,stdlib
  54. ,cowboy
  55. ]},
  56. {mod, {<APPNAME>_app, []}},
  57. {env, []}
  58. ]}.
  59. ```
  60. ## Rebar3 Conventions
  61. Rebar3 is entirely based on building OTP applications and releases.
  62. * Directories starting with underscores, e.g. `_deps`, are expected to not be checked in version control.
  63. * Project apps you are working on exist under `apps/` or `lib/`, or is a single app project with `src/` in the root directory.
  64. * `rebar.lock` and `rebar.config` go in the root of the project.
  65. * Tests go in `tests/`.
  66. ## rebar.config vs rebar.lock
  67. `rebar.lock` contains the exact reference id to a commit that was used to build the project. Committing this file allows you to specify a branch in `rebar.config` for a dependency and still have reproducable builds because if the `rebar.lock` file exists when a rebar3 project is being built the contents of deps in rebar.config are ignored.
  68. ## Checkout Dependencies
  69. Often while developing you find yourself working on mulitple applications from separate repos together. To simplify this process `rebar3` will look in the directory `_checkouts` for applications to override a dependency.
  70. For example, you are working on project `app1` which depends on `app2`. You want to have your modifications to `app2` used by `app1`:
  71. ```shell
  72. [app1] $ pwd
  73. /home/user/code/app1
  74. [app1] $ cat rebar.config
  75. {deps, [
  76. {app2, "", {git, "git://github.com:user/app2.git", {branch, "master"}}}
  77. ]}.
  78. [app1] $ ls -l _checkouts
  79. lrwxrwxrwx app2 -> /home/user/code/app2
  80. ```
  81. Since a symlink to `app2` exists in `_checkouts` there will not be a fetch of `app2` by `rebar3` when the project is built.
  82. ## Tests
  83. Rebar3 has the concept of test dependencies. These dependencies will only be fetched when a rebar3 command that runs tests is run by the user.
  84. ```erlang
  85. {test_deps, [
  86. {meck, ".*", {git, "https://github.com/eproxus/meck.git", {tag, "0.8"}}}
  87. ]}.
  88. ```
  89. ```shell
  90. $ rebar ct
  91. ===> Fetching meck
  92. ===> Compiling meck
  93. ===> Compiling minasan
  94. ```